C Decision Making - Looping

The While Statement

while (test condition) 
{ 
    body of the loop 
}

Here the given test condition is evaluated and if the condition is true then the body of the loop is executed. After the execution of the body, the test condition is once again evaluated and if it is true, the body is executed once again.
This process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the loop. On exit, the program continues with the statements immediately after the body of the loop.
The body of the loop may have one or more statements.
# include < stdio.h > //include the stdio.h file 
void main() // Start of your program 
{ 
     int n, i=0; //Declare and initialize the variables 
	 printf(“Enter the upper limit number”); //Message to the user 
	 scanf(“%d”, &n); //read and store the number 
	 while(I < = n) // While statement with condition 
	 { // Body of the loop 
	     printf(“\t%d”,I); // print the value of i 
		 I++;// increment I to the next natural number. 
	} 
}


Program description

In the above program the looping concept is used to generate n natural numbers.
Here n and I are declared as integer variables and I is initialized to value zero.
A message is given to the user to enter the natural number till where he wants to generate the numbers. The while loop then checks whether the value of I is less than n
i.e., the user entered number if it is true then the control enters the loop body and prints the value of I using the printf statement and increments the value of I to the next natural number this process repeats till the value of I becomes equal to or greater than the number given by the user.


 

Make Comments..!!


Oops!! No posts from user.

Download Android App