Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 158 - CoreGPT(Podar)_Flipbook_C8
P. 158

    for(i = 1, j = 10; i <= 10; i++, j--)

                          {
                              cout<< "i = " << i << ", j = " << j << endl;
                          }

                          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.

                      Program 3: To display the table of 5.
                                                                                             5 × 1 = 5
                      #include<iostream.h>                                                   5 × 2 = 10
                                                                                             5 × 3 = 15
                      #include<conio.h>
                                                                                             5 × 4 = 20
                      void main()                                                            5 × 5 = 25
                      {                                                                      5 × 6 = 30
                                                                                             5 × 7 = 35
                          clrscr();
                                                                                             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;
                      }

                    156     Premium Edition-VIII

| 153 154 155 156 157 158 159 160 161 162 163 |