/ Unlabelled / C - While Loop in C

C - While Loop in C

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 

  1. Variable Initialization
  2. Test Expression or Condition
  3. Variable Increment/Decrement
I will explain all these processing steps in syntax. so, you understand each.

C While Loop Syntax 

Below I shared the syntax of while loop and then flowchart for visual view.

variable initialization //eg. i=1;
while( test expression ) {
     //All Statements
     // Variable Increment/Decrement eg. i++;       
}




While loop flowchart

Here I shared flowchart of while loop 


while loop flowchart



Example of 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 the previous tutorial, you learn for loop in c. In this tutorial, You learn about while loop in c and in the next tutorial learn about do while loop in c 


C - While Loop in C C - While Loop in C Reviewed by Veer Bhadra Singh on April 12, 2020 Rating: 5

No comments:

Powered by Blogger.