Learning C/C++ Step-By-Step - Page 5

05. Step-by-Step C/C++ --- C Programming - Looping Statements

  1. Branching Statement - goto
  2. Looping Statements
    for
    while
    do..while

 

1. Branching Statement

goto
It transfers the control pointer from one place to another in the current program.

  Syntax:
 goto <label>;

Note: Label name must be defined with colon(:) and it should not exceed more than 32 characters in length.

  Eg.

abc:
     printf(“Hello”);
     goto abc;

/* 01. A demonstration program to illustrate goto statement */

/* 19_goto.c */
#include <stdio.h>
int main()
{
     abc: /* Label name */

          printf("\nHello");
     goto abc; /* branching statement */
     return 0;
}

/* 07. Continuous execution will be stopped with a carry varaible and a conditional statement */
/* Find the difference between the last program and this, note all the differences in this program*/

/* 20_goto.c */
#include <stdio.h>
int main()
{
     int i = 1;
     abc:
          printf("\nHello");
          i ++;
          if ( i<= 10 ) /* Take care of this statement */
     goto abc;
     return 0;
}

 

2. Looping Statements

for
An iterative statement to execute a statement block for a number of times.

  Syntax:

for(<initialization> ; <condition> ; <step value>)
{
     <st. block>
}

  Eg.

for(I=1;I<=10; I++)
     printf(“\n%d”,i);

  Eg.

for(I=1, j = 0; I<10; I+=2, j+=2)
     printf(“%d %d\n”, i, j);

/* 08. To print a message 5 times */

/* 21_for.c */
#include <stdio.h>
int main()
{
     int i;
     for(i = 1; i <= 5; i++ )
          printf("\nHello");
     return 0;
}

/* 09. To print a message with it's count upto 5 times */

/* 22_hello.c */
#include <stdio.h>
int main()
{
     int i;
     for(i = 1; i <= 5; i++ )
          printf("\nHello - %d", i);
     return 0;
}

/* 10. To print 1 to 10 natural numbers */

/* 23_nat.c */
#include <stdio.h>
int main()
{
     int i;
     for(i = 1; i <= 10; i++ )
          printf("\n%d", i); /* Eleminating message */
     return 0;
}

/* 11. To print second multiplication table */
/* Note : Compare it, with the last program */

/* 24_table.c */
#include <stdio.h>
int main()
{
     int i;
     for(i = 1; i <= 20; i++ )
          printf("\n%d * 2 = %d", i, i * 2);
     return 0;
}

/* 12. To print a multiplication table for the given number */

/* 25_tablen.c */
#include <stdio.h>
#include <conio.h>
int main()
{
     int i, t; /* a new variable 't' */
     clrscr();
     printf("Which table to print :"); scanf("%d", &t);
     for(i = 1; i <= 20; i++ )
          printf("\n%d * %d = %d", i, t, i * t);
     return 0;
}

/* 13. To print a multiplication table for the given number */
/* Note : Compare it, with the last program */

/* 26_tablen.c */
#include <stdio.h>
int main()
{
     int i, t;
     clrscr();
     for(t = 1; t <= 20; t++) /* One more for loop */
          for(i = 1; i <= 20; i++ )
               printf("\n%d * %d = %d", i, t, i * t);
     return 0;
}

/* 14. To print numbers in triangle form */
/* Note : Compare it, with the last program */

/* 27_tri.c */
#include <stdio.h>
int main()
{
     int i, j;
     clrscr();
     for( i = 1 ; i<= 5; i++ )
     {
          for( j = 1; j <= 5; j++ )
               printf("%4d", j);
          printf("\n");
     }
     return 0;
}

Few more examples of for loops:

  /* Infinite Loop */
for ( ; ; )
{
     printf("\nHello");
}
/* Print 1-5 numbers */
for ( i=1; i<=5; )
{
     printf("\n%d", i++);
}
  /* Explicit Loop break*/
for ( i=1; ; )
{
     printf("\n%d", i++);
     if ( i> 5 ) break;
}
int i = 1;
for ( ;i<=5 ; )
{
     printf("\n%d", i++);
}

while
An iterative statement to execute a statement block until the given condition is satisfied.

do.. while
This iterative statement executes statement block at the begin and then it checks the condition validity. If the condition is true it executes the statement block again and vice versa.

  Syntax:

while( < condition > )
{
      <st. block>;
}

Syntax:

do
{
      <st. block>
} while(<condition>);

Eg.
The following example displays natural numbers from 1 to 10.
 

int main()
{
      int i=1;
      while( i<=10)
      {
            printf(“\n%d”,i);
            i++;
      }
      return 0;
}

int main()
{
      int i=1;
      do
      {
            printf(“\n%d”,i);
            i++;
      }while(i<=10);
      return 0;
}

  It checks the condition first and executes the block next , So you should have an initial value for the condition It executes the block first and checks the condition next , You can determine the initial value in the st.block.
More Examples
 

/* 15. To print 1 to 5 numbers */
/* Note : It's a reference program */

/* 28_while.c */
#include <stdio.h>
int main()
{
     int i;
     i = 1; /* Initial value is 1 */
     while( i<= 10 )
      /* True i is less than or equal to 10 at first */
     {
          printf("\n%d", i);
          i ++;
     }
     return 0;
}

/* 16. To print 1 to 5 numbers */

/* 29_dowhile.c */
#include <stdio.h>
int main()
{
     int i;
     i = 1; /* Initial value is 1 */
     do
     {
          printf("\n%d", i);
          i ++;
     }while( i<=10 );
     /* True, i is less than or equal to 10 at Second */
     return 0;
}

 

/* 17. Demonstration of while */
/* Note : If the initial value is 100 what was the output?, Check it. */

/* 30_demow.c */
#include <stdio.h>
int main()
{
     int i;
     i = 1;
     while( i<= 10 )
     {
          printf("\n%d", i);
          i++;
     }
     return 0;
}

/* 18. Demonstration of do */
/* Note : If the initial value is 100 what was the output?, Check it. */

/* 31_demod.c */
#include <stdio.h>
int main()
{
     int i;
     i = 1;
     do
     {
          printf("\n%d", i);
          i++;
     }while( i<= 10 );
     return 0;
}

Share this page:

1 Comment(s)