Page 86 - Viva ICSE Computer Studies 8 : E-book
P. 86

First Step: An initialisation happens fi rst and only one time, which means that the initialisation
                    part of the loop only executes once.
                    Second Step: Condition in For loop is evaluated on each iteration. If the condition is true
                    then the statements inside the For loop body get executed. Once the condition returns false,
                    the statements in the For loop do not execute and the control gets transferred to the next
                    statement in the program after the For loop.

                    Third Step: After every execution of For loop’s body, the increment/decrement part of For loop
                    executes that eventually updates the loop counter.
                    Fourth Step: After the third step, the control jumps to the          Think and Discuss
                    second step and the condition is re-evaluated.

                    Example:  Write a program to print the even numbers                  What is the unique feature of
                                                                                         For loop?
                    between 1 and 10.























                    While Loop

                    While loop statement in Java programming language repeatedly executes a target statement
                    as long as a given condition is ‘True’. It initially tests the condition before executing the loop
                    body. The condition can be any Boolean expression. While loop is a condition controlled loop.
                    The loop will continue executing till the test condition is ‘True’. The control terminates when
                    the test condition is ‘False’.

                    Let’s understand the While loop with the syntax and an example.

                    Syntax:                                                                        Test
                                                                                                 condition
                     While(test condition)
                     {                                                                                If condition is
                     …………                                                                             true

                     Statements                                                                 Code block    If condition
                                                                                                statements
                     }                                                                                        is false








                     74
   81   82   83   84   85   86   87   88   89   90   91