Page 63 - Modular_V2.0_C++_Flikpbook
P. 63

}

                     getch();
                 }
                                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 initialise a variable inside the for loop. For example,

                      for(int i = 1, j = 5; i <= 10; i++)
                 In the above line, two variables i and j are declared and initialised inside the for loop.
                                                                                            DOSBox 0.74, Cpu speed: max
                 Program 3: To display the table of 5.
                                                                                        5 × 1 = 5
                 #include<iostream.h>
                                                                                        5 × 2 = 10
                 #include<conio.h>                                                      5 × 3 = 15
                                                                                        5 × 4 = 20
                 void main()
                                                                                        5 × 5 = 25
                 {
                                                                                        5 × 6 = 30
                     clrscr();                                                          5 × 7 = 35
                                                                                        5 × 8 = 40
                     for(int i = 1, j = 5; i <= 10; i++)
                                                                                        5 × 9 = 45
                     {                                                                  5 × 10 = 50
                         cout<< j << " x " << i << " = " << j * i << "\n";
                     }
                     getch();
                 }

                    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 expressions, not just conditions. The following are all valid expressions:

                      while(x = x + 1)   // Assigns a new value to x and loops indefinitely
                      while(n--) // Decrements n and continues looping while n is non-zero
                      while(count += i) //  Adds i to count and loops while the result is
                                                    non-zero


                                                                                                                  61
                                                                                                         Loops
   58   59   60   61   62   63   64   65   66   67   68