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

statements is the  statement block,  that  is to  be repeatedly  executed  until the  provided
                      condition becomes false.

                                 Tech Funda

                         Do not put a semicolon (;) after the closing parenthesis of a for loop. If you do,
                         the loop will keep running but will not execute any statements until the condition
                         becomes false.


                  Program 1: To print first 10 natural numbers.

                  #include<iostream.h>
                  #include<conio.h>                                                        DOSBox 0.74, Cpu speed: max 100%
                                                                                        1 2 3 4 5 6 7 8 9 10
                  void main()

                  {
                      int i;
                      clrscr();

                      for(i = 1; i <= 10; i++)
                      {

                          cout<< i << " ";
                      }

                      getch();
                  }
                  Using Comma (,) Operator with for Loop

                  C++ allows the use of the comma operator (,) to combine multiple expressions in a loop. When
                  two or more variables are used to control the loop, they are separated by the comma operator.
                  The comma operator allows: multiple initialisations, multiple conditions and multiple counter

                  variables.
                  Program 2: To use the comma operator in for loop.

                  #include<iostream.h>                                                     DOSBox 0.74, Cpu speed: max

                  #include<conio.h>                                                    i = 1, j = 10
                                                                                       i = 2, j = 9
                  void main()
                                                                                       i = 3, j = 8
                  {                                                                    i = 4, j = 7
                                                                                       i = 5, j = 6
                      clrscr();
                                                                                       i = 6, j = 5
                      int i, j;                                                        i = 7, j = 4
                                                                                       i = 8, j = 3
                      for(i = 1, j = 10; i <= 10; i++, j--)
                                                                                       i = 9, j = 2
                      {                                                                i = 10, j = 1

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


                   60
                          Touchpad MODULAR (Ver. 2.0)
   57   58   59   60   61   62   63   64   65   66   67