Page 57 - Modular_V1.1_Flipbook
P. 57

Tech



                     Funda

                                        Never use assignment operator in place of equality operator in the test
                                        condition as it may cause an infinite loop.




                 Variable Declaration Inside Loop

                 C++ allows you to declare and initialize a variable inside the for loop. For example,
                 for (int i = 1, j = 5; i <= 10; i++)

                 In the preceding line, two variables i and j are declared and initialized inside the for loop.
                 Program 3: To display the table of 5.
                 #include<iostream.h>                                                              Output:
                 #include<conio.h>                                                                 5 * 1 = 5

                 void main()                                                                       5 * 2 = 10
                 {                                                                                 5 * 3 = 15
                      for (int i = 1, j = 5; i <= 10; i++)                                         5 * 4 = 20
                      cout << j << “ * “ << i << “ = “ << j * i << “\n”;                           5 * 5 = 25

                      getch();                                                                     5 * 6 = 30
                 }                                                                                 5 * 7 = 35
                                                                                                   5 * 8 = 40

                  Tech                                                                             5 * 9 = 45
                                                                                                   5 * 10 = 50
                     Funda


                                        If you want to execute just one statement in the body of the while loop,
                                        you do not have to enclose the statement in braces.




                     THE WHILE LOOP

                 The while loop executes a set of statements repeatedly until a certain condition is met. The
                 syntax of the while loop is:

                 while (condition)
                 {
                 statements;
                 }

                 The  while  loop  can  accept  expression  and  not  just  conditions,  the  following  are  all  legal
                 expression:



                                                                                                         Loops    55
   52   53   54   55   56   57   58   59   60   61   62