UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to study while loop

How while loop is working in c program with syntax and example

by Krishna viswambharan


Posted on 18 Aug 2017 Category: C Views: 120

Edited on 25 Aug 2017



Familiarization of  'while loop'

We know that loops are used in programming is used to repeat the statements or a specific block of code.  There are three main loops in C

      • for loop
      • while loop
      • do-while loop

syntax of while loop is given as follows:-

while (testCondition) 
{
    //codes 
}

testCondition checks the condition is true or false before each loop.

Here is an example to understand the while loop

#include <stdio.h>
#include <conio.h>
void main()
{
	int count=1;
	while(count<=5)
	{
		printf(ā€œ%d \nā€,count);   //print the value
		count++;    //increment count
	}
    getch();
}

First, the variable count is initialized to 1, then it checks the condition. If the condition is true, then body of while loop gets executed.  Count value got incremented and check against the condition and repeats the flow until the condition turns false

Related Article

C program to study 'do-while' loop



Leave a Comment:


Advertisement