Flowchart illustrating the step-by-step execution of a while loop in C programming, showing the flow from initialization, through condition check, loop body execution, and variable updation.

The given diagram is the flowchart showing how a 'while' loop works in C programming.

1. Initialization: After step-by-step execution of the instructions, the program control enters the initialization part. In this step, the programmer declares or initializes the variable to some value.
Initialization of the variable happens outside the loop and it is not part of the 'while' loop, but it is essential to initialize it before the loop starts because it is required when the programmer uses a variable in the condition or test expression.

2. Condition: The condition is an expression that may evaluate to either true or false. This is one of the primary and most essential steps, as it decides whether the block of code in the 'while' loop will execute or not. The code inside the body of the 'while' loop will be executed if and only if the tested condition is true; otherwise, control jumps outside the loop and stops the execution.

3. Body: It is a block of code or the actual set of statements that will be executed repeatedly until the specified condition is true. This block of code or statements is known as the body of the loop. It can include variables, functions, expressions, etc.
The block of code or statements in our flowchart is "Code inside body of loop."

4. Updation:
Updation is not part of the syntax, but we have to define it explicitly in the body of the loop. It is an expression that changes the value of the loop variable in each step of execution or iteration.


while loop syntax in C Programming

Syntax of while loop in C

Let us study real-world syntax of while loop and then we will look detail into all parts of the while loop.

/* statements outside loop.
initilization */
  while(condition)
{
     /* code inside body of loop
     statements to be executed */
     /* update expression */
}
/* statement outside loop */


Explanation:
1. Control enters the 'while' loop.
2. The control jumps to the Condition inside the 'while' loop.
3. The condition is tested. Here, the condition tested will either be true or false.
  i. If the condition tested is true, then the flow goes into the Body of the loop.
  ii. If the condition tested is false, then the flow goes outside the loop.
4. If the condition tested is true, the statements inside the body of the loop get executed.
5. Updation takes place.
6. Control jumps back to step 2.
7. If the condition tested is false, the 'while' loop ends and the control jumps outside the loop.


write a C program to illustrate use of while loop

/* C program given below displays "Inside the While loop Body" 10 times */
#include <stdio.h>
int main()
{
/* Declaration and initialization expression */
int n = 1;
/* test the expression */
 while(n<=10)
 {
  printf("\n Inside the While loop Body");
  /*update expression */
  n+=1;  // n++
 }
return(0);
}
output:
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body
Inside the While loop Body


Explanation:
1. The program starts from the 'main()' function.
2. 'n' is declared and initialized to '1'.
3. The condition 'while(n<=10)' is tested. Since '1 <= 10' yields 'true'.
  3.1 "Inside the While loop Body" gets printed for the 1st time.
  3.2 Updation takes place: 'n+=1' executes, so 'n' becomes '2' (i.e., 'n=2').
4. The condition 'while(n<=10)' is tested. Since '2 <= 10' yields 'true'.
  4.1 "Inside the While loop Body" gets printed for the 2nd time.
  4.2 Updation takes place: 'n+=1' executes, so 'n' becomes '3' (i.e., 'n=3').
5. The condition 'while(n<=10)' is tested. Since '3 <= 10' yields 'true'.
  5.1 "Inside the While loop Body" gets printed for the 3rd time.
  5.2 Updation takes place: 'n+=1' executes, so 'n' becomes '4' (i.e., 'n=4').
... (This process continues until 'n' becomes '10'
6. The condition 'while(n<=10)' is tested. Since '10 <= 10' yields 'true'.
  6.1 "Inside the While loop Body" gets printed for the 10th time.
  7.2 Updation takes place: 'n+=1' executes, so 'n' becomes '11' (i.e., 'n=11').
8. The condition 'while(n<=10)' is tested. Since '11 <= 10' yields 'false'.
9. Control flow goes outside the loop and 'return 0;' stops program execution.