Page 51 - DCAP404 _Object Oriented Programming
P. 51

Object-oriented Programming




                    Notes          where expression-1 is  used to  initialize some parameter (called  an index) that controls  the
                                   looping action, expression-2 represents a condition that must be true for the loop to continue
                                   execution, and expression-3 is used to alter the  value of  the parameter initially assigned  by
                                   expression-1. Typically,  expression-1 is an assignment  expression, expression-2  is a  logical
                                   expression and expression-3 is a unary expression or an assignment expression.
                                   When the for statement is executed, expression-2 is evaluated and tested at the beginning of each
                                   pass through the loop, and expression-3 is evaluated at the  end of each pass.  Thus, the for
                                   statement is equivalent to
                                   Expression-1;

                                   while  (expression-2)
                                   {
                                          statement
                                          expression-3;
                                   }
                                   The looping action will continue as long as the value of expression-2 is not zero, that is as long
                                   as the logical condition represented by expression-2 is true.
                                   The program will display the consecutive duals using for loop.
                                   #  include  <  stdio.  h>
                                   main  (  )                  /*    display  the  numbers  0  through  9*/
                                   {
                                          int  digit

                                          for  (  digit  =  0  ,  digit  <  =  9;  +  +  digit)
                                                 printf  (  “  %  d\n”  ,  digit);
                                   }
                                   The first line of the for statement contains three expression, enclosed in parentheses. The first
                                   expression assigns an initial value 0 to the integer variable digit; the second expression continues
                                   the looping action as long as the current value of digit does not exceed 9 at the beginning of each
                                   pass, and the third expression increases the value of digit by 1 at the end of each pass through the
                                   loop.





                                      Task  The for statement is ideal when we know exactly how many times we need  to
                                     iterate, because it lets us easily declare, initialize, and change the value of loop variables
                                     after each iteration. Analyze and explain with an example.

                                   Self Assessment


                                   Fill in the blanks:
                                   9.  The ……………………….. is used to carry out a logical test and then take on of two possible
                                       actions, depending on the outcome of the test.

                                   10.  The …………………… statement  is used  to carry out operations,  in which a group of
                                       statements is executed repeatedly, till a condition is satisfied.




          44                                LOVELY PROFESSIONAL UNIVERSITY
   46   47   48   49   50   51   52   53   54   55   56