Page 47 - DCAP404 _Object Oriented Programming
P. 47

Object-oriented Programming




                    Notes          Output
                                   1
                                   1
                                   2

                                   3             As you can see, each number in the series is the
                                   4             sum of the two preceding numbers.
                                   5
                                   8

                                   13
                                   21
                                   34
                                   55

                                   89

                                   The Break Statement

                                   The break statement causes the program flow to exit the body of the while loop. The following
                                   program code illustrates the use of the break statement.
                                   #include  <iostream.h>

                                   int  main()
                                   {
                                          int  num1  =  1,  num2  =  1;
                                          cout  <<  num1  <<  end1;
                                          while  (num2  <  150)
                                          {
                                                 cout  <<  num2  <<  end1;

                                                 num2  +=  num1;
                                                 num1  =  num2  -  num1;
                                                 if  (num2  ==  89)
                                                 break
                                          }
                                          return  0;
                                   }
                                   The output of the previous program is:
                                   1

                                   1
                                   2





          40                                LOVELY PROFESSIONAL UNIVERSITY
   42   43   44   45   46   47   48   49   50   51   52