Learning Computer Programming using JAVA with 101 examples by Suchato Atiwong & hummat elchin

Learning Computer Programming using JAVA with 101 examples by Suchato Atiwong & hummat elchin

Author:Suchato, Atiwong & hummat, elchin [Suchato, Atiwong]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2020-03-20T16:00:00+00:00


Figure 114: A program coded to run five iterations of statements using a do-while loop

Initially, this program sets i to 1. This variable can be thought of as a counter of how many times the statements in the do-while block have already been executed. Each time this do-while block is entered, the program prints the iteration number from the variable i , which is increased by 1 at the end of every iteration (on line 9) before the program checks the boolean value ofi<=N , where N equals 5. The program will exit the do-while statement after the 5th iteration, at the end of which the value ofi is 6.

The following program performs the same task as the program above but with the use of awhile statement instead of thedo-while one.

public class WhileDemo1 1

{ 2 public static void main(String[] args) 3

{ 4 int i = 1; 5

final int N = 5; 6

while(i<=N){ 7 System.out.println("Iteration # "+i); 8 i++; 9 }; 10 System.out.println("Out of while loop when i="+i); 11 } 12

Figure 115: A program coded to run five iterations of statements using a while loop

Both programs in Example 53 produce the same outputs. The only difference between using a do-while statement and a while statement is that the statements in the do-while block is always executed at least once since the condition checking is done after the do-while block, while the checking is done prior to ever entering the while block. Thus, the statements in the while block may never be executed.

Example 54: q for quit

The programs in this example shows how to use keep prompting for character input from the user repeatedly until a specified character is entered. The first one does nothing much apart from waiting for a ‘q’ character to be entered.

import java.io.*; 1 public class WhileMenuDemo 2 { 3

public static void main(String[] args) throws IOException 4

{ 5 boolean done = false; 6

char command; 7 BufferedReader stdin = 8

new BufferedReader( 9 new InputStreamReader(System.in)); 10 while(!done){ 11 System.out.print("Enter a character (q to quit): "); 12 command = stdin.readLine().charAt(0); 13 if(command == 'q') done = true; 14 } 15 } 16 } 17

Figure 116: A program that keeps prompting for input

On line 6, a boolean variable called done is created and initialized to false . This variable is used in the condition checking of the while statement so that the statements in the while block will be iteratively executed as long as done is false . done has to be set to true at some point to avoid infinite loop (i.e. the situation when the iteration repeats forever!), and in this program, it is when the char value that the user enters equals 'q' , as on line 14.

The following program finds average of a number of values entered by the user. The program iteratively asks the user to enter each value at a time, until a character 'q' is entered.

import java.io.*; 1 public class Average1 2 { 3

public static void main(String[] args) throws IOException 4

{ 5 double sum =



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.