C language : Chapter 3: For loop

 For loop - for loop allows us to specify three things about the loop in a single statement.

  1. Setting the loop counter to initial value.
  2. Testing the loop to determine if the loop counter value has reached the number of repetitions desired.
  3. Increment the loop counter value each time, the program segment within the loop has executed.

Syntax:
For (initialize counter, test counter, increment counter)
{
Do this;
And this;
And this;
}


For loop structure

Below for loops are perfectly ok.
for(i=10;i;i--)
printf("%d",i);



please note:- i++ , i=i+1; i+=1 are same.

@program- Write down a program for 1 to 10.
method1

method2

Method3

Method4

Method5: Post increment operator


Method 6: Pre increment operator


Nesting of loops:
Loops can be nested into one another and within itself.
  1. for loop can be nested within for loop
  2. while loop can be nested within for loop
  3. for loop can be nested within while loop.

For loop within for loop


Multiple initializations within for loop.
The initialization statement within for loop can contain more than one statement separated by comma.
for ( i = 1, j = 2 ; j <= 10 ; j++ )
We can increment and decrement two or more variables at the same time within for loop.
for(r=1,c=1;r<=3;r++,c++) //multiple initialization and multiple increments

Do-while loop - The odd loop.
The loops that we have used so far executed the statements within them a finite no of times. However, in real life programming one comes across a situation when it is not known beforehand, how many time statements within loop have to be executed. This stituation can be programmed as below.
Important - there is a space before %c notice carefully. " %c",&another

The odd loop - for loop

The odd loop - using while loop.


The break statement.
We often come across the situation where we want to jump out of the loop instantly, without waiting to get back to the conditional test. The Keyword break allows us to do this. When break is encountered inside the loop, control automatically passes to the next statement outside the loop. A break is associated with if.


@program- break statement

The continue keyword:
In some programming situations, we want to take the control to the beginning of the loop, bypassing the statements inside the loop which have not yet been executed. The keyword continue allows us to do that. When continue is encountered inside the loop, control passes to the beginning of the loop. A continue is usually associated with if.




Difference between while and do-while loop
While loop - first test the condition before executing the statements inside the loop.
Do-while loop - it executes the statements inside the do loop first and then test the condition in while loop before executing the statements again inside the do loop.

Summary:- 
  1. There are three types of loops available in C
    1. While
    2. For
    3. do-while.
  2. The break statement takes the control to the next statement outside the loop.
  3. The continue keyword takes the control to the beginning of the loop bypassing the statements which have not yet been executed.
  4. A do-while loop is used to ensure that the statements within the loop should be executed atleast once before test is evaluated.
  5. The ++ operator increases the operand by 1, whereas -- operator decreases the operand by 1
  6. The += , -= , *=, /= , %= are compound assignment operators. They modify the value of the operand to the left of them.












Comments

Popular posts from this blog

C language : Chapter 2

C Language: Chapter 4 The case control structure.