C'Language - [Pre-tested Loops]

♠ Posted by Unknown in at 00:05

Pre-Tested [Entry Controlled] Loops :

while Loop:

The simplest of all the looping structure in c' is the while statement. The basic format of the while statement is :
Syntax:
         while  (condition)
           {
               body of loop
           }

The while is an "entry controlled loop" statement. The  test condition is evaluated and if the condition is True, then the body of the loop is executed. After 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 until the test condition finally becomes false and the control is transferred out of the loop. On exit, the program continues with the statement immediately after the body of the loop.

The body of the loop may have one or more statements. The braces are needed only if the body contains two or more statements. However it is a good practice to use braces even if the body has only statement.

Example :-

void main()
{
   int a;
   a=1;
  while (a<=10)
  {
    printf(" % d \n ", a);
      a=a+1;
  }
getch();

for Loop:

The for loop is another entry-controlled loop that provides a more loop control structure. The  general form of the for loop is.

Syntax:-

for(initialization; test-condition; increment)
 {
          body of the loop;
 }
         
The executed of the for loop statement is as follows:
1.    Initialization of the control variables is done first, using assignment statements such as i=1 and count=0. The variable i and count are known as loop control variables.
2.    The value of the control variable is tested using the test-condition. The test condition is a relational expression, such as i<10 that determines when the loop will exit if the condition is executed, otherwise the loop is execution continues with the statement that immediately follows loop.
3.    When the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now, the control variable is incremented using an assignment statement such as i=i+1 and the new value of the control variable is again tested to see whether is satisfied the loop condition.

Example:-
        main ()
        {
           int a;
           for (a=1; a<=10, a+1);
           {
                     printf(" %d\n " , a);
            }
            return;
        }

4 comments:

Thank you for sharing your awesome and valuable article this is the best blog for the students they can also learn.
https://lookobeauty.com/best-interior-designer-in-gurgaon/

Post a Comment