Page 258 - PowerPoint Presentation
P. 258

CAVITE STATE UNIVERSITY
                               TRECE MARTIRES CITY CAMPUS
                               Department of Information Technology            DCIT 111 - Advanced Programming

               Week 5: Elements of Programming Languages Continuation
               Objective: After the completion of the chapter, students will be able to:
                     Learn the fundamentals of High-level programming language
                     Perform different operations and instructions using High-level programming language
                     Create a simple program applying conditional statements, looping and array.
                     Write program codes in a piece of paper

               Repetition Statements
                       Java provides three repetition structures: while, do-while and for. These statements
               are also known as loops. We shall look into the syntaxes of these structures first and also
               learn how we can formulate algorithms using these loops.

               The while loops
                       Following  is  the  syntax  of  the  while  loop.  The  statement  in  the  while  blocks  are
               executed as long as the condition evaluates to true.
                     while (condition) {
                            statement;
                     }

                       For this loop to end, the condition should at some point of time become false. This is
               achieved using counters. Counters are simple variables which are incremented every time the
               body of the loop is executed. They are used to keep track of the number of times the loop is
               executed. The value of counter is used in the conditional expression to determine the end of
               the loop. Consider the following code below which is used to print the statement “Hi!” five times
               on the screen.
                  initialization;                           int i = 0;


                  while (condition) {                       while (i <5) {
                        // body                                    System.out.println(“Hi!);
                        increment / decrement;                     i++;
                  }                                         }

                       The value of the integer variable i is first set to zero. When control enter the while
               statement, the condition i<5 is evaluated. Since 0<5 is true, the body of the loop is executed.
               The statement “Hi!” is printed and i is incremented to 1. The condition is evaluated again, i<5
               or 1<5 is still true. The body of the loop is again executed. In this way, the while loop executes
               five times after which i becomes 5. Now, since the condition is 5<5 evaluates to false, control
               moves out of the while loop. In the above program, we can eliminate the i++; statement and
               include it in the while condition itself by using increment operator.
                       While loop is an entry control loop which means that the condition is checked before
               entering the body of the loop. Therefore, it might be possible that the body of the loop is not
               executed even once.

               The do while loop
                       Following  is  the  syntax  of  the  do  while  loop.  The  statement  in  the  do  blocks  are
               executed until it reaches the while condition. When the statement reached the condition, the
               program will stop executing.
                                              do {
                                                       statement;
                                              } while (condition);

                       Note that there is a semicolon after the while statement. Consider the following code
               below which is used to print the statement “Hi!” five times on the screen.



                                                                                                            34
   253   254   255   256   257   258   259   260   261   262   263