Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page4

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page16

chapter2page17

chapter2page18

chapter2page19

chapter2page20

chapter2page21

chapter2page22

chapter2page23

chapter2page24

chapter2page25

chapter2page26

chapter2page27

chapter2page28

chapter2page29

chapter2page30

chapter2page31

chapter2page33

chapter2page34

chapter2page32

chapter3

link one

link one

link one

link one

link one

link one

2.8.2 - Do-While Loops

The do-while loops are a variant of while loops. They are exactly the same with the while loops, except the loop control is made at the end of the loop. The structure of the do-while loops is as follows :

do {
body of the loop
} while (boolean-expression);

Note that the loop sentinel set the loop repetition flag at the end of the body of the loop. After the boolean expression used as loop sentinel, a semicolon is required.

Let us try a do-while loop by calculating the sum of the first 100 integers.

First we will make a flowchart of the program:

do-while loop flowhchart

2.8.2 image - 1 : do-while loop

Here is the program:

package preliminary;

public class DoWhile {

public static void main(String[] args){
int i = 0;
int sum = 0;
do  {
i++;
sum = sum + i;
} while (i < 100); // end do

System.out.println("The Sum Of The First 100 integers is :  " + sum);
System.out.println("i =   " + i);
 } // end main

} //end class DoWhile

The result of this program :

The Sum Of The First 100 integers is :  5050
The value of i is :  100

The program flow of he above example is exactly the same as in the example with while loop. Here, only the loop sentinel is evaluated at the end of the loop.

The do-while loops are not frequently used in favor of the while loops.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)