Download presentation
Presentation is loading. Please wait.
1
Lecture Notes – Week 3 Lecture-2
Chapter 5 (Loops)
2
Outline Introduction to loops Three forms of loops To do list while
do-while for To do list
3
Introduction to Loops Motivating example:
Example: Print "Welcome to Java!” 100 times. How would we do it? Write the following statement 100 times? System.out.println("Welcome to Java!"); That would be too tedious! We would need to write a loop.
4
Loops Loops are constructs that control repeated execution of a block of statements. int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; } A while loop starts with keyword while, followed by a Boolean expression for the condition, then followed by a statement body. We also need to declare/initialise a variable for counting.
5
while Statements } while (Boolean-expression) { statement(s);
int count = 0; while (count < 100) { System.out.println(”Welcome to Java!”); count++; } while (Boolean-expression) { statement(s); } 5 5
6
Trace while Loop Initialize count to 0 int count = 0;
while (count < 2) { System.out.println("Welcome to Java!"); count++; } 6 6 6
7
While (count < 2) is true
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } While (count < 2) is true (count is 0 at moment) 7 7 7 7
8
Print “Welcome to Java!”
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print “Welcome to Java!” 8 8 8 8 8
9
(count is increased to 1 now)
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 (count is increased to 1 now) 9 9 9 9 9 9
10
While (count < 2) is true
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } While (count < 2) is true (count is 1 at moment) 10 10 10 10 10
11
Print “Welcome to Java!”
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print “Welcome to Java!” 11 11 11 11 11 11
12
(count is increased to 2 now)
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 (count is increased to 2 now) 12 12 12 12 12 12 12
13
While (count < 2) is false
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } While (count < 2) is false (count is 2) 13 13 13 13 13 13
14
Exit the loop and execute the next statement after the loop.
Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Exit the loop and execute the next statement after the loop. 14 14 14 14 14 14 14 14
15
Increment and decrement operators
15
16
do-while Statements Now compare it to: do { statement(s);
} while (Boolean-expression); int count = 0; do { System.out.println(”Welcome to Java!”); count++; } while (count < 100); Now compare it to: int count = 0; while (count < 100) { System.out.println(”Welcome to Java!”); count++; } 16 16 16
17
for Statements for (initial-action; Boolean-expression; action-after) { statement(s); }; int i; for (i = 0; i < 100; i++) { System.out.println(”Welcome to Java!”); }; 17 17 17
18
Trace for Loop Declare variable i int i; for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!"); } ; 18 18 18
19
Trace for Loop int i; for (i = 0; i < 2; i++) { Initialise I to 0
System.out.println("Welcome to Java!"); } ; Initialise I to 0 (i is 0) 19 19 19 19
20
Trace for Loop int i; for (i = 0; i < 2; i++) { (i < 2) is true
System.out.println("Welcome to Java!"); } ; (i < 2) is true (i is 0) 20 20 20 20 20
21
Print “Welcome to Java!”
Trace for Loop int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Print “Welcome to Java!” 21 21 21 21 21 21 21
22
Trace for Loop int i; Increase i by 1 for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!"); } ; Increase i by 1 (i is increased to 1) 22 22 22 22 22 22 22
23
Trace for Loop int i; for (i = 0; i < 2; i++) { (i < 2) is true
System.out.println("Welcome to Java!"); } ; (i < 2) is true (i is 1) 23 23 23 23 23
24
Print “Welcome to Java!”
Trace for Loop int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Print “Welcome to Java!” 24 24 24 24 24 24 24
25
Trace for Loop int i; Increase i by 1 for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!"); } ; Increase i by 1 (i is increased to 2) 25 25 25 25 25 25 25
26
Trace for Loop int i; for (i = 0; i < 2; i++) { (i < 2) is false
System.out.println("Welcome to Java!"); } ; (i < 2) is false (i is 2) 26 26 26 26 26 26
27
Exit the loop and execute the next statement after the loop.
Trace for Loop int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Exit the loop and execute the next statement after the loop. 27 27 27 27 27 27 27
28
Exercise Write a program that displays the squares of the numbers from 0 to 15. Using For Loop While Loop Do-While Loop Output should be as :
29
To Do List Before Next Lecture
Read Week 3 lecture slides. Read and run Week 3 program examples. Selectively read those sections in Chapter 5 that cover the topics in this lecture. Attend your practical session in Week 4, consisting of a tutorial and a set of programming exercises. Glance through those sections in Chapter 6 that cover the topics in Week 4’s lecture.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.