Advertisement



< Prev
Next >



C - While Loop





C language provides us an important looping structure using which we could execute one or multiple statements within a loop until a condition is met. The condition of while loop is tested for the boolean value - true or false.






while loop example


/* C - while loop example */

#include<stdio.h>

int main() /*main method starts*/
{

int i=0;

while(i<5)
{
	printf("i = %d \n", i);
	i++;
}

return 0;
}   /* main method ends and program ends too */


Output


i = 0
i = 1
i = 2
i = 3
i = 4



Advertisement




While loop must have an iteration part in its body or it runs an endless loop


/* Endless while loop */

#include<stdio.h>

int main() //main method starts
{

int i=0;

while(i<5)
{
	printf("i = %d \n" + i);
}

return 0;
}  /*main method and program ends here */


Output


This program when executed, runs indefinitely in a loop because of the absence of incremental part to increment the value of i.



Please share this article -





< Prev
Next >
< Do-While Loop
Break Statement >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement