• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
C Programming - Decision Making -Looping
C Programming - Decision Making - Looping
In this tutorial you will learn about C Programming - Decision Making - Looping, TheWhile Statement, The Do while statement, The Break Statement, Continue statement andFor Loop.During looping a set of statements are executed until some conditions for termination of the loop is encountered. A program loop therefore consists of two segments one known as body of the loop and other is the control statement. The control statement tests certainconditions and then directs the repeated execution of the statements contained in the bodyof the loop.In looping process in general would include the following four steps1. Setting and initialization of a counter 2. Exertion of the statements in the loop3. Test for a specified conditions for the execution of the loop4. Incrementing the counter The test may be either to determine whether the loop has repeated the specified number of times or to determine whether the particular condition has been met.
The While Statement:
The simplest of all looping structure in C is the
while
statement. The general format of the while statement is:
while (test condition){body of the loop}
 
Here the given test condition is evaluated and if the condition is true then the body of theloop is executed. After the execution of the body, the test condition is once againevaluated and if it is true, the body is executed once again. This process of repeatedexecution of the body continues until the test condition finally becomes false and thecontrol is transferred out of the loop. On exit, the program continues with the statementsimmediately after the body of the loop. The body of the loop may have one or morestatements. The braces are needed only if the body contained two are more statementsExample program for generating ‘N’ Natural numbers using while loop:
# include < stdio.h > //include the stdio.h filevoid main() // Start of your program{int n, i=0; //Declare and initialize thevariables printf(“Enter the upper limit number”); //Message to theuserscanf(“%d”, &n); //read and store the numberwhile(I < = n) // While statement with condition{ // Body of the loopprintf(“\t%d”,I); // print the value of iI++; increment I to the next natural number.}}
In the above program the looping concept is used to generate n natural numbers. Here nand I are declared as integer variables and I is initialized to value zero. A message isgiven to the user to enter the natural number till where he wants to generate the numbers.The entered number is read and stored by the scanf statement. The while loop then checkswhether the value of I is less than n i.e., the user entered number if it is true then thecontrol enters the loop body and prints the value of I using the printf statement andincrements 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.
 
The Do while statement:
The do while loop is also a kind of loop, which is similar to the while loop in contrast towhile loop, the do while loop tests at the bottom of the loop after executing the body of the loop. Since the body of the loop is executed first and then the loop condition ischecked we can be assured that the body of the loop is executed at least once.Thesyntaxof the do while loop is:
Do{statement;}while(expression);
Here the statement is executed, then expression is evaluated. If the condition expressionis true then the body is executed again and this process continues till the conditionalexpression becomes false. When the expression becomes false. When the expression becomes false the loop terminates.To realize the usefulness of the do while construct consider the following problem. Theuser must be prompted to press Y or N. In reality the user can press any key other than yor n. IN such case the message must be shown again and the user should be allowed toenter one of the two keys, clearly this is a loop construct. Also it has to be executed atleast once. The following program illustrates the solution.
/* Program to illustrate the do while loop*/#include < stdio.h > //include stdio.h file to your programvoid main() // start of your program{char inchar; // declaration of the characterdo // start of the do loop{printf(“Input Y or N”); //message for the userscanf(“%c”, &inchar); // read and store the character}
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...