Docsity

LECTURE 02: LOOP STATEMENTS & ARRAYS, Schemes and Mind Maps of Java Programming

Partial preview of the text

Download LECTURE 02: LOOP STATEMENTS & ARRAYS and more Schemes and Mind Maps Java Programming in PDF only on Docsity!

LECTURE 02: LOOP STATEMENTS

& ARRAYS

• While loop

• Do-While loop

• For loop

• Arrays

PLAN

The while Statement

int sum = 0, number = 1;

while ( number <= 100 ) {

sum = sum + number;

number = number + 1;

These statements are executed as long as number is less than or equal to 100.

Syntax for the while

Statement

while ( number <= 100 ) {

sum = sum + number;

number = number + 1;

while ( )

Statement

(loop body)

Boolean Expression

Finding GCD

Direct Approach More Efficient Approach

Example: Testing Input Data

int age; Scanner scanner = new Scanner(System.in); System.out.print("Your Age (between 0 and 130): "); age = scanner.nextInt( ); while (age < 0 || age > 130 ) { System.out.println( "An invalid age was entered. Please try again."); System.out.print("Your Age (between 0 and 130): "); age = scanner.nextInt( ); }

Priming Read

Loop Pitfall - 1

Infinite Loops

Both loops will not terminate because the boolean expressions will never become false. int count = 1; while ( count != 10 ) { count = count + 2; }

int product = 0; while ( product < 500000 ) { product = product * 5; }

o An infinite loop often results in an overflow error. o An overflow error occurs when you attempt to assign a value larger than the maximum value the variable can hold. o In Java, an overflow does not cause program termination. With types float and double, a value that represents infinity is assigned to the variable. With type int, the value “wraps around” and becomes a negative value. Overflow

Loop Pitfall – 2a

int result = 0; double cnt = 1.0; while (cnt <= 10.0){ cnt += 1.0; result++; } System.out.println(result);

int result = 0; double cnt = 0.0; while (cnt <= 1.0){ cnt += 0.1; result++; } System.out.println(result);

Using Real Numbers

Loop 1 prints out 10, as expected, but Loop 2 prints out 11. The value 0.1 cannot be stored precisely in computer memory.

Loop Pitfall - 3

count = 1; while ( count < 10 ){

... count++; }

count = 0; while ( count <= 10 ){

... count++; }

count = 1; while ( count <= 10 ){

... count++; }

count = 0; while ( count < 10 ){

... count++; }

The goal of the code is to loop 10 times which (1) and (3) fails to archive

do while ( ) ; Syntax for the do-while Statement

do {

sum += number;

number++;

} while ( sum <= 1000000 );

Statement

(loop body)

Boolean Expression

o Loop-and-a-half repetition control can be used to test a loop’s terminating condition in the middle of the loop body. o It is implemented by using reserved words while, if, and break. Loop-and-a-Half Repetition Control

  • Be aware of two concerns when using the loop-and-a-half

control:

  • The danger of an infinite loop. The boolean expression of the while statement is true, which will always evaluate to true. If we forget to include an if statement to break out of the loop, it will result in an infinite loop.
  • Multiple exit points. It is possible, although complex, to write a correct control loop with multiple exit points (breaks). It is good practice to enforce the one-entry one-exit control flow. Pitfalls for Loop-and-a-Half Control

o Demo & practice while / do-while loop exercises

o Source: https://www.w3resource.com/java-exercises/conditional-

statement/index.php

Exercises