The for Loop, The break Statement, The continue Statement


Lecture 6

The for Loop

The for loop is the most popular looping control among all. The for loop allows us to specify three things in a single line:
a)    Setting a loop counter to an initial value.
b)    Testing the loop counter to determine whether its value has reached the number of repetitions desired.
c)    Increasing/decreasing the value of loop counter each time the program segment within the loop is executed.

The general form of for statement is as follows:

for (initialize counter; test counter; increment/decrement counter){
      do this;
      and this;
      and this;
}
For example, the program below prints 1 to 10.
main(){
      int counter;
      for (counter=1;counter<=10;counter++){
            printf (“%d\n”, counter);
      }
}


Let’s examine the structure of for more detail:
a)  Initially counter is set to 1. Check if it is less than or equal to 10. Yes it is.
b)  Move into the body of for. Print the variable counter.
c)  Increment counter by 1.
d)  counter is now 2. Check if it is less than or equal to 10. Yes it is.
e)  Move into the body of for. Print the variable counter.
f)  Increment counter by 1.
……………………………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………………………
counter is now 11. Check if it is less than or equal to 10. No, it is not.
End of the loop.

Now, let’s play with for loop for a while!

main(){
      int counter=1;
      for (;counter<=10;counter++){
            printf (“%d\n”, counter);
      }
}

This is valid. We have initialized counter to 1 before we came to for loop. But inside the for loop, the semi-colon is still required.

main(){
      int counter;
      for (counter=1;counter<=10;){
            printf (“%d\n”, counter);
            counter++;
      }
}

This is valid as well. We have incremented counter by 1 just before the end of the ending brace. It makes counter to 2 and again the control goes back to the for loop. But inside the for loop, the semi-colon is still required.

main(){
      int counter=1;
      for (;counter<=10;){
            printf (“%d\n”, counter);
            counter++;
      }
}

And valid so as this! We have initialized counter to 1. Then we have incremented the variable just as before. The for loop only contains the checking of condition. Both the semi-colons are required.

main(){
      int counter=1;
      for (;;){
            printf (“%d\n”, counter);
            counter++;
      }
}

This is an infinite for loop. As we have omitted the condition checking from the loop, it prints counter for infinite times.

for Loops: Notes and Observations

1.    The initialization, loop-continuation condition and increment/decrement can contain arithmetic expressions. Assume that x=2 and y=10. The statement-
for(j=x; j<=4*x*y; j=j+y/x)
is equivalent to-
for(j=2; j<=80; j=j+5)

2.    There can be a decrement as well (or you can say a negative increment). For example-

main(){
      int counter;
      for (counter=10;counter>=1;counter--)
            printf(“%d”, counter);
}
prints 10 9 8 7 6 5 4 3 2 1.

3.    If the loop continuation condition is initially false, the body portion of the loop is not performed.

main(){
      int i,loopcontrol=20,mark=0;
      for (i=1;i>=loopcontrol;i++){
      mark=1;
      }
      if(mark==0)
            printf (“The loop continuation condition is initially false”);
      else
            printf(“We have entered into the loop”);
     
}
the above program will all the time print The loop continuation condition is initially false as it never went into the body of for because the loop continuation condition is false (1 is not greater than or equal to 20).

Qs

We want to go 1 to 100 incremented by 1 (1 2 3 4       … 100)
We want to go 1 to 100 incremented by 3 (1 4 7 10      … 100)
We want to go to 1 from 100 decremented by 1 (100 99 98       … 1)
We want to go to 0 from 100 decremented by 5 (100 95 90       … 0)


Nested for Loops

while and for loops can be nested. A nested loop is loop inside a loop. Let us demonstrate the procedure.
main(){
      int i, j;
      for (i=1;i<=3;i++){
            for (j=1; j<=2;j++){
                  printf(“i=%d j=%d sum=%d\n”, i, j, i+j);
            }
      }
}
The output of the program is as follows-
i=1 j=1 sum=2
i=1 j=2 sum=3
i=2 j=1 sum=3
i=2 j=2 sum=4
i=3 j=1 sum=4
i=3 j=2 sum=5

Let’s Play

What will be the output?
#include<stdio.h>
#include<conio.h>

void main(){
      int i;
      clrscr();
      for(i=1;i<=10;printf("%d\n",i)){
            i++;
      }
      getch();
}
What will be the output?
#include<stdio.h>
#include<conio.h>

void main(){
      int i;
      clrscr();
      for(;i=0;){
            printf("Hello there\n");
      }
      getch();
}

The break Statement

We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if. As an example, let’s consider the following example.

#include<stdio.h>
#include<conio.h>

void main( )
{
int i;
clrscr();
for(i=0;i<10;i++){
      if(i==5)
            break;
      printf("%d",i);
}
getch();
}

The output of the program is 0 1 2 3 4. When the value of i is 5, the program continues inside the if where it finds break statement.  The program execution then goes to the end of for loop and then it continues executing the rest of the statements (getch() and the final curly brace).

The continue Statement

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 this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop.
A continue is usually associated with an if. As an example, let's consider the following program.
#include<stdio.h>
#include<conio.h>

void main( )
{
int i;
clrscr();
for(i=0;i<10;i++){
      if(i==5)
            continue;
      printf("%d",i);
}
getch();
}
The output of the above program would be 0 1 2 3 4 6 7 8 9.
The program prints 0 1 2 3 4 as up to this point, the if condition is false. Then when the value of i is 5, it goes into the if block and executes the continue statement. The program then omits the printf and increases i by one and then again does the rest- prints 6 7 8 9 as this time again the condition of if is false.




2 comments:

Unknown said...

This is great blog! Keep it up. aircon chemical cleaning

Unknown said...

This is great blog! Keep it up. aircon chemical wash price

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms