Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 160 - CoreGPT(Podar)_Flipbook_C8
P. 160

    The syntax of the do-while loop is:

                      do
                      {
                          statements;
                      }

                       while(condition);
                      Program 5: To display the first 10 natural numbers in reverse order.

                      #include<iostream.h>
                      #include<conio.h>
                      void main()
                      {                                     First 10 natural numbers in reverse order:
                                                            10 9 8 7 6 5 4 3 2 1
                          clrscr();
                          int n = 10;
                          cout<< "First 10 natural numbers in reverse order:\n";
                          do
                          {

                              cout<< n << " ";
                              n--;
                          }
                          while(n > 0);
                          getch();
                      }

                      This loop prints numbers starting from 10 and going down to 1, until the n becomes 0 which is
                      the terminating condition of the loop.
                      Nested Loop

                      If a loop executes inside another loop, it is known as nested loop. Both the loops will execute

                      depending on its conditional statement. But the inner loop must terminate before the outer
                      loop. The syntax to create a nested loop is:
                      for(... ...)                                // Outer loop

                      {
                          // Code inside the outer loop

                          for(... ...)                            // Inner loop
                          {

                                // Code inside the inner loop
                          }
                      }

                    158     Premium Edition-VIII

| 155 156 157 158 159 160 161 162 163 164 165 |