Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 157 - CoreGPT(Podar)_Flipbook_C8
P. 157

    condition is a conditional expression to decide whether to enter the loop or not.
                        update is a variable to change the value(s) of loop variable(s) after each iteration. Iteration is
                        the process of running a loop one time.
                        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>
                                                                                          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>
                     #include<conio.h>

                     void main()
                     {
                         clrscr();

                         int i, j;

                                                                                      Developing Programs in C++        155

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