ICS 103: Computer Programming in C

Handout-07

Topic: Control Structures (Repetition & Loop Statements)

 

Instructor: M. Waheed Aslam.

 

Objective:

·         To know use of increment operator ++, and decrement operator --.

·         To know syntax of wile, do-while, for statements (loops).

·         To know working of above loops with examples.

 

Use of increment operator ++, and decrement operator -- 

 

i++  means  i=i+1;              first, initial value of i will be assigned and after that, the value

i -- means  i=i-1;         of  i will be incremented / decremented accordingly 

 

Example:

#include<stdio.h>

main()

{

int i=2, r1 ;

r1=i++;

printf("Value of r1 is : %d",  r1);

}

Sample Output: Output:

 

++i  means  i=i+1;     Here, in the first step, the  initial value of i will be incremented

-- i means   i=i-1;          or decremented  and then the value of i will be assigned).

 

Example:

#include<stdio.h>

main()

{

int i=2, r1;

r1 = ++i;

printf("Value of r1 is : %d", r1);

}  

 

Looping Structures:

 

The while statement:

 

Syntax:       while (expression)

                       statement

 

A conditional loop is one whose continued execution depends on the value of a logical expression. In the above syntax, as long as the expression is true the statement (or statements if enclosed within braces) is executed. When the expression evaluates to false, the execution of the statement(s) stops and program continues with the other following statements.

  

Example:    Let number be an integer variable.

                             while (number > 100)

                             {    

                                  printf (“Enter a number\n”);

                                    scanf (“%d”, &number);   

                             }

 

In the above example:

·        As long as the entered value of number is greater than 100 the loop continues to ask to enter a value for number.

·        Once a number less than or equal to 100 is entered it stops asking.

·        If in the beginning itself the value of number is less than or equal to 100, then the loop is not executed at all.

 

Note: In the while loop the expression is tested before the loop body statements are entered. So, if the expression is false in the beginning the loop body is never entered.

 

do…while statement:

Syntax:                 do

                                  Statement

                             while (expression);

Here:

·        The expression is tested after the statement(s) are executed.

·        So, even if the expression is false in the beginning the loop body is entered at least once.

·        Here also like the while statement, as long as the expression is true the statement (or statements if enclosed within braces) is executed.

·        When the expression evaluates to false the execution of the statement(s) stops and program continues with the other following statements.

 

Example:    The example of while loop can be written with do…while loop as:

 

                             do

                             {             printf (“Enter a number\n”);

                                            scanf (“%d”, &number);   

                              }

                             while (number > 100);

 

In this example:

·        As long as the entered value of number is >100 the loop continues to ask to enter a value for number.

·        Once a number less than or equal to 100 is entered it stops asking.

 

Note: In the conditional looping using while and do…while the number of times the loop will be executed is not known at the start of the program.

for statement:

 

Syntax:                  for (expression 1;  expression 2;  expression 3)

                                      Statement

 

In the iterative looping using for loop:

·        the number of times the loop will be executed is known at the start of the program.

·        In the above syntax:

§        expression 1 is the initialization statement which assigns an initial value to the loop variable.

§        expression 2 is the testing expression where the loop variable value is checked. If it is true the loop body statement(s) are executed and if it is false the statements(s) are not executed.

§        expression 3 is usually an increment or decrement expression where the value of the loop variable is incremented or decremented. 

 

Example:   

                   for (count = 0 ;  count < 10 ;  count++)

                      {  

                        printf (“Enter a value\n”) ;

                        scanf (“%d”, &num) ; 

     num = num + 10 ;

     printf (“NUM = %d\n”, num) ;     

  }

In this example:

·        The integer variable count is first initialized by assigning it the value 0.

·        Then the value of count is checked if it is less than 10. If it is < 10:

§         the loop body asks to enter a value for num.

§        Then the value of count is incremented by 1 and again count is checked for less than 10:

o        If it is true the loop body is again executed and then the count is incremented by 1 and checked for < 10.

o       It keeps on doing like this until the count is >= 10. Then the loop stops. So, here the loop is executed 10 times.

                  

Note: The increment operation is written as ++ and decrement operation is written as --. The increment increases the value by 1 and decrement decreases the value by 1.

 

 

 

 

 

How to generate number from 1 to 10 Using different loops:

 

 for  

while

do-while

for(n=1; n<=10; ++n)

 {      

------

------

------

}

        

n=1;                        while(n<=10) 

 {   

------ 

------   

n=n+1;

}

n=1;                     

do

{

------

------

n=n+1;

} while (n <=10)

 

Useful Solved Examples using different loops:

Example 1:  for loop:

#include<stdio.h>

void main()

{

int num, square, limit;

printf("Please Input value of limit : ");

scanf("%d", &limit);

for (num=0; num < limit; ++num)

{

square = num*num;

printf("%5d %5d\n", num, square);

} // end of for

} // end of main

 

           

           

Example 2:

  /********************************************************/

Program Showing a Sentinel-Controlled for loop to compute

        the sum of a list of exam scores.

/********************************************************/

            #include <stdio.h>

        #define SENTINEL  -99

        int main(void)

        {

            int sum = 0,   /* sum of scores input so far  */

             score;     /* current score       */

             printf("Enter first score (or %d to quit)> ", SENTINEL);

            for(scanf("%d", &score); score != SENTINEL; scanf("%d",

                   &score))

                { sum += score;

                   printf("Enter next score (%d to quit)> ", SENTINEL);

                } // end of for loop

                printf("\nSum of exam scores is %d\n", sum);

 

                return (0);

        } // end of main

 

 


Example 3:  while loop:

/*Conditional loop Using while Statement */

#include <stdio.h>

#define RIGHT_SIZE 10

 

void main()

{

 int diameter;

printf("Please Input Balloon's diameter > ");

scanf("%d",&diameter);

 

while(diameter < RIGHT_SIZE)

{

printf("Keep blowing ! \n");

 

printf("Please Input new Balloon's  diameter > ");

scanf("%d", &diameter);

} // end of while loop

 

printf("Stop blowing ! \n");

} // end of main

 

 

Example 4: do-while loop:

/* do-while loop */

#include<stdio.h>

void main()

{int num;

do

{printf("Please Input Positive numbers between  ( < 1 or >= 10 ) :  ");

scanf("%d",&num);

} // end of do loop

while (num < 1 || num >=10) ;

printf("Dear , Sorry ! Your number is out of specified range in program");

} // end of main

 

 

 

Example 5: Use of Increment and Decrement operators:

/* Explanation on Increment , Decrement operators */

#include<stdio.h>

void main()

{int  n, m, p, i = 3, j = 9;

n = ++i *  --j;

printf("From above First given expression calculated n=%d i=%d j=%d\n", n, i, j);

m = i + j--;

printf("From above second given expression calculated n=%d i=%d j=%d\n",m ,i ,j);

p = i + j;

printf("From above Third given expression calculated n=%d i=%d j=%d\n", p, i, j);

} // end of main