Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while Loop o The for Loop o Nested Loops o The break and continue Statements o Labeled Loops MIT 12043, By S. Sabraz Nawaz
The Increment and Decrement Operators MIT 12043, By S. Sabraz Nawaz 3
The Increment and Decrement Operators There are numerous times where a variable must simply be incremented or decremented like number = number + 1; number = number 1; Java provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator -- Operator Operator Name Sample Expression Explanation ++ prefix increment ++a Increment a by 1, then use the new value of a in the expression in which a resides. ++ -- -- postfix increment prefix decrement postfix decrement a++ --b b-- Use the current value of a in the expression in which a resides, then increment a by 1. Decrement b by 1, then use the new value of b in the expression in which b resides. Use the current value of b in the expression in which b resides, then decrement b by 1. MIT 12043, By S. Sabraz Nawaz 4
Differences Between Prefix and Postfix An increment or decrement operator that s prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator An increment or decrement operator that s postfixed to (placed after) a variable is referred to as the postfix increment or postfix decrement operator When used in an expression: o prefix notation indicates that the variable will be incremented or decremented prior to the rest of the equation being evaluated o postfix notation indicates that the variable will be incremented or decremented after the rest of the equation has been evaluated MIT 12043, By S. Sabraz Nawaz 4-5
Differences Between Prefix and Postfix MIT 12043, By S. Sabraz Nawaz 6
Differences Between Prefix and Postfix MIT 12043, By S. Sabraz Nawaz 7
Looping / Repetition MIT 12043, By S. Sabraz Nawaz 8
Loops A repetition (or looping) statement allows you to specify that a program should repeat an action while some condition remains true. Two kinds of loops o Finite loop: The statements in the block may be executed any number of times, from zero to infinite number. o Infinite loop: A loop that continues forever. A loop consist of; o Body of the loop o Control statement MIT 12043, By S. Sabraz Nawaz 9
Looping Constructs in Java Java provides three different looping structures. o The while loop o The do while loop o The for loop MIT 12043, By S. Sabraz Nawaz 10
Loop Control Structures Entry Control loop Exit Control loop cond true loop body false loop body false cond true If condition is not satisfied body of the loop is never executed. The body of the loop is executed unconditionally for the 1st time. MIT 12043, By S. Sabraz Nawaz 11
Important Parts in a Loop Initialization: The variables tested in the condition must be initialized to some values. If the condition is false at the outset, the loop is never entered. Testing: The condition is tested before each iteration. If false, the program continues with the first statement after the loop. Change: At least one of the variables tested in the condition must change within the body of the loop. MIT 12043, By S. Sabraz Nawaz 12
while Loop MIT 12043, By S. Sabraz Nawaz 13
The while Loop The while loop is a pretest loop, which means that it will test the value of the condition prior to executing the loop. While the condition is true, the statements will execute repeatedly. MIT 12043, By S. Sabraz Nawaz 14
The while loop Flowchart boolean expression? true statement(s) false MIT 12043, By S. Sabraz Nawaz 15
The while Loop This is an entry-controlled loop The body of the loop initialization; while (condition) { statement1; statement2;... statementn; } condition is any logical expression, as in if MIT 12043, By S. Sabraz Nawaz 16
The while Loop Care must be taken to set the condition to false somewhere in the loop so the loop will end. Loops that do not end are called infinite loops. A while loop executes 0 or more times. If the condition is false, the loop will not execute. Example: The following program displays numbers from 1 to 10 on the screen MIT 12043, By S. Sabraz Nawaz 17
Loop Example 01 Write a program that uses a while loop to print the sum of integers from 1 to 10. MIT 12043, By S. Sabraz Nawaz 18
Infinite Loops In order for a while loop to end, the condition must become false. The following loop will not end: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); } The variable x never gets decremented so it will always be greater than 0. MIT 12043, By S. Sabraz Nawaz 19
Shorthand Assignment Operators
Shorthand Assignment Operators Java provides special shorthand assignment operators that simplify the coding of certain assignment statements. The assignment statement x = x + 10; can be written, using Java shorthand, as x += 10; The assignment statement x = x - 10; is the same as x -= 10; Like that x = x * 10 x *= 10 Like that x = x / 10 x /= 10 MIT 12043, By S. Sabraz Nawaz 21
Loop Example 02 The Example 01 is modified using shorthand assignment operator MIT 12043, By S. Sabraz Nawaz 22
do while Loop MIT 12043, By S. Sabraz Nawaz 23
The do-while Loop The do-while loop is a post-test loop, which means it will execute the loop prior to testing the condition MIT 12043, By S. Sabraz Nawaz 24
The do-while Loop Flowchart statement(s) boolean expression? true false MIT 12043, By S. Sabraz Nawaz 25
The do-while Loop This is an exit-controlled loop The code runs through the body of the loop at least once initialization; do { statement1; statement2;... statementn; } while ( condition ); if condition is false, the next iteration is not executed 26
Loop Example 03 Write a program that uses do-while loop to print the sum of squares of integers from 1 to 10.
Loop Example 04 In this example you can see that although the expression evaluates to false, the code inside the loop block is executed once; this is because do-while is exit controlled loop MIT 12043, By S. Sabraz Nawaz 28
for Loop MIT 12043, By S. Sabraz Nawaz 29
The for Loop The for loop is a pre-test loop The for loop allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code MIT 12043, By S. Sabraz Nawaz 30
The for Loop Flowchart boolean expression? true statement(s) update false MIT 12043, By S. Sabraz Nawaz 31
The for Loop for(initialization; condition; update) { statement1; statement2;... statementn; } 32
The Sections of The for Loop The initialization section of the for loop allows the loop to initialize its own control variable. The initialization section can initialize multiple variables. Variables declared in this section have scope only for the for loop The condition of the for statement acts in the same manner as the condition section of a while loop The update of the for loop is the last thing to execute at the end of each loop MIT 12043, By S. Sabraz Nawaz 33
Loop Example 05 The following example displays numbers from 1 to 10 MIT 12043, By S. Sabraz Nawaz 34
Loop Example 06 Write a program that uses for loop to print the sum of squares of integers from 1 to 10.
Loop Example 07 Display the even numbers between 0 and 20 MIT 12043, By S. Sabraz Nawaz 36
Nested Loops MIT 12043, By S. Sabraz Nawaz 37
Nested Loops Like if statements, loops can be nested. If a loop is nested, the inner loop will execute all of its iterations for each time the outer loop executes once. for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) loop statements; The loop statements in this example will execute 100 times. MIT 12043, By S. Sabraz Nawaz 38
Loop Example 08 Develop a 10 x 10 multiplication table MIT 12043, By S. Sabraz Nawaz 39
Summary of Loop Control Structures for for (n=1;n<=10;n++) {.... } while n = 1; while (n<=10) {.... n = n+1; } do while n = 1; do {.. n+=1; } while (n<=10);
break and continue MIT 12043, By S. Sabraz Nawaz 41
The break Statement - jumping out of a loop It is possible to force an immediate exit from a loop, bypassing any remaining code in the body of the loop and the loop s conditional test, by using the break statement When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop (It is considered bad form to use the break statement in this manner) MIT 12043, By S. Sabraz Nawaz 42
break Statement
Loop Break 02 Go through the code and understand what s going on The use of break statement MIT 12043, By S. Sabraz Nawaz 44
Loop Break 03 Go through the code and understand what s going on The use of break statement MIT 12043, By S. Sabraz Nawaz 45
The continue Statement - skipping a part of a loop The continue statement forces the next iteration of the loop to take place, skipping any code between itself and the conditional expression that controls the loop Like the break statement, the continue statement should be avoided because it makes the code hard to read and debug MIT 12043, By S. Sabraz Nawaz 46
continue Statement 47
Loop Continue 01 In the above example, the continue statement causes the next iteration of the for loop Variable i takes the value from 0 15 but only even numbers are displayed on the screen MIT 12043, By S. Sabraz Nawaz 48
Labeled Loops To jump outside a loop that is outside the current one OR to continue a loop that is outside the current one The continue statement terminates the inner loop when j = i and continues with the next iteration of the outer loop 49
End of Lesson MIT 12043, By S. Sabraz Nawaz 50