While loop in C

while loop structure is used to create iteration (loops) in your programs.

Defining while loop

The syntax of while loop is as follows:

1

2

3

4

while(testExpression)

{

//codes

}

where testExpression checks the condition is true or false before each loop.

Flowchart of while loop

How While loop Works?

The while loop evaluates the test expression.

If the test expression is true (nonzero), codes inside the body of while loop is executed. The test expression is evaluated again. The process goes on until the test expression is false.

When the test expression is false, the while loop is terminated.

Let’s see the simple example of while loop:

1

2

3

4

5

6

7

8

9

#include <stdio.h>

intmain()

{

intx=0;

while(x<10){

printf("The value of x is %d\n",x);

x++;

}//end while loop

}//end main function

When you run above program, the output will be following:

1

2

3

4

5

6

7

8

9

10

The value of x is 0

The value of x is 1

The value of x is 2

The value of x is 3

The value of x is 4

The value of x is 5

The value of x is 6

The value of x is 7

The value of x is 8

The value of x is 9

The while statement is summarized like this:

1

while(x<10){

The while loop uses a condition (in this case x < 10) that evaluates to either true or false. As long as the condition is true, the contents of the loop are executed.

Tip: The braces {} for any loop are required only when more than one statement is included in the loop’s body. If your while loop contains only one statement, no braces are required. To demonstrate, take a look at the following while loop, which does not require the use of braces.

1

2

while(x<10)

printf("The value of x is %d\n",x++);

In the preceding program, I incremented the variable x by 1 with the increment operator (++).

The increment operator (++) is very important for this loop. Without it, an endless loop will occur. In other words, the expression x < 10 will never evaluate to false, thus creating an infinite loop.

Infinite while loop

Infinite loops are loops that never end. They are created when a loop’s expression is never set to exit the loop.

Note: To exit an infinite loop, press Ctrl+C, which produces a break in the program. If this does not work, you may need to end the task.

1

2

3

4

5

6

7

8

9

10

11

#include <stdio.h>

intmain()

{

intvar=10;

while(var>=5)

{

printf("%d",var);

var++;

}

return0;

}

The loop would never end because of value >=5

Use of Logical operators in while loop

Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical operators in while loop. The following scenarios are valid :

1

while(num1<=10&&num2<=10)

– using AND(&&) operator, which means both the conditions should be true.

1

while(num1<=10||num2<=10)

– OR(||) operator, this loop will run until both conditions return false.

1

while(num1!=num2&&num1<=num2)

– Here we are using two logical operators NOT (!) and AND(&&).

1

while(num1!=10||num2>=num1)

Let’s see the simple example of while loop using logical operator:

In this example, we are testing multiple conditions using logical operator inside while loop.