Loop in simple way


Loop control statement:
Sometimes we want some part of our code to be executed more than once. We can either repeat the code in our program or use loops instead. It is obvious that if for example we need to execute some part of code for a hundred times it is not practical to repeat the code. Alternatively, we can use our repeating code inside a loop.A loop statement allows us to execute a statement or group of statements multiple times.


C programming language provides the following types of loops to handle looping requirements.
1.    While loop
2.    For loop
3.    Do-while loop

1. while loop
while loop is constructed of a condition or expression and a single command or a block of commands that must run. while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Flow Diagram

Figure: while loop


program

#include <stdio.h>
 int main ()
{
  int a = 10;
  while( a < 20 )
   {
      printf("value of a: %d\n", a);
      a++;
     }
getch();
}


2. for loop

for loop is something similar to While loop but it is more complex. for loop is constructed from a control statement that determines how many times the loop will run and a command section. Command section is either a single command or a block of commands. for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.



Syntax
for (initialization; condition; increment)
{
   statements;
}

·         The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
·         Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.
·         After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
·         The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.
Flow Diagram

Example
#include <stdio.h>
int main () {
   int a;
   for( a = 10; a < 20; a = a + 1 )
{
      printf("value of a: %d\n", a);
   }
   return 0;
}


Do-while loop:

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.
do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.
Syntax
The syntax of a do...while loop in C programming language is −
do {
   statement(s);
} while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.

Flow Diagram



Example
#include <stdio.h>
int main () {
   int a = 10;
   do {
      printf("value of a: %d\n", a);
      a = a + 1;
   }while( a < 20 );
   return 0;
}













0 comments:

Post a Comment