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

Program 7: To display the numbers from 1 to 10. The loop will exit when the value of the
                  number will become 6.

                  #include<iostream.h>
                  #include<conio.h>
                  void main()

                  {
                      int num = 10;
                      clrscr();
                      for(int i = 1; i <= num; i++)
                      {                                                          DOSBox 0.74, Cpu speed: max 100% cycles,

                          if(i == 6)                                          1 2 3 4 5
                                                                              Out of the loop
                              break;
                          cout<< i << " ";
                      }
                      cout<< "\nOut of the loop";

                      getch();
                  }
                  In the above output, you can see that the loop is terminated the numbers after 5 because of the
                  break statement.

                  The continue Statement

                  The  continue  statement  is used to  skip the  current  iteration  of the  loop.  When  a  continue
                  statement is encountered inside a loop, control of the program jumps to the beginning of the
                  loop  for  next  iteration, skipping the  execution  of  rest  of  the  statements of  the  loop  for  the
                  current iteration. The syntax of the continue statement is:
                       while(condition)

                       {
                            if(conditional_expression )
                            {
                                 continue;
                            }

                       }
                  Program 8: To display the numbers from 1 to 10 except the number 7.

                  #include<iostream.h>
                  #include<conio.h>
                  void main()
                  {
                      int num = 10;




                   66
                          Touchpad MODULAR (Ver. 2.0)
   63   64   65   66   67   68   69   70   71   72   73