Page 60 - Modular_V1.1_Flipbook
P. 60

int r;
                      cout << “Enter number of rows: “;
                      cin >> r;
                      for(int i = 1; i <= r; i++)

                      {
                          for(int j = 1; j <= i; j++)
                          {
                              cout << j << “ “;
                          }
                          cout << “\n”;
                      }
                      getch();
                  }

                      INFINITE LOOP

                  The loop that never ends is called infinite loop. If the condition given in a loop never becomes
                  false, then the loop will never terminate and run indefinitely. To prevent this, make sure the value
                  of the condition does change somewhere in the header or body of the loop so the condition can
                  eventually become false. For example:

                  int i = 10;
                  while (i <= 10)
                  {

                  cout<<”Hello”;
                  i--;
                  }
                  The preceding loop will run infinitely, because the value of the variable i will never become
                  greater than 10.


                    Tech



                      Funda

                                         An infinite loop can be stopped by pressing Ctrl + Break key.






                      DIFFERENCE BETWEEN WHILE AND DO-WHILE LOOPS
                  The following are the major differences between while and do-while loops.

                       while is pre-test (or top-test) loop. First the condition is tested, then only entry is possible
                       in loop body. But do-while is post-test (bottom-test) loop. Hence, the loop is executed at
                       least once.


                  58      Touchpad MODULAR (Version 1.1)-X
   55   56   57   58   59   60   61   62   63   64   65