In this tutorial, You learn about while loop in c programming, understand how while logic works to run a looping statement successfully. In the previous tutorial, we learned about for loop.
I think you know that a loop or iterative function is used to repeat the block of statements until the given test expression or test condition return false. and while loop also uses for the same work but it is also known as the entry control loop.
While Loop in C
Steps for while loop processing are
It contains three simple while loop processing steps are
- Variable Initialization
- Test Expression or Condition
- Variable Increment/Decrement
C While Loop Syntax
Below I shared while loop syntax and flowchart
variable initialization //eg. i=1;
while( test expression ) {
//All Statements
// Variable Increment/Decrement eg. i++;
}
While loop flowchart
Here I shared while loop flowchart
while loop flowchart |
Example while loop
I share an example of print a series of the number below to understand while loop
#include<stdio.h>
int main() {
int n;
printf("Enter a number ");
scanf("%d",&n);
int count=1; //variable initialization
while( count<=n ) { //test expression
printf("%d\n",count);
count++; // variable increment
}
return 0;
}
When the above code is compiled and executed, it produces the following output
Enter a number: 5
1
2
3
4
5
In this tutorial, You learn about while loop in c and flowchart in the next tutorial learn do while loop in c.
Happy Coding 😊