Title: The forloop and Nested loops
1The for-loop and Nested loops
2Outline
- The for Statement Syntax
- Semantics of the for Statement
- Nested Loops
- continue, break, and exit Statements
3- The for Statement
- The for statement is most commonly used to step
through an integer variable in equal increments - It begins with the keyword for, followed by three
expressions in parentheses that describe what to
do with one or more controlling variables - The first expression tells how the control
variable or variables are initialized or declared
and initialized before the first iteration - The second expression determines when the loop
should end, based on the evaluation of a Boolean
expression before each iteration - The third expression tells how the control
variable or variables are updated after each
iteration of the loop body
4- The for Statement
5- The for Statement Syntax
for ( ltinitializationgt ltboolean expressiongt
ltupdategt ) ltstatementgt // OR Block of Statments
6- The for Control Flow
7- Nested Loops
- Loops can be nested, just like other Java
structures - When nested, the inner loop iterates from
beginning to end for each single iteration of the
outer loop - for (Initializing Boolean_Expression Update)
- Block 1
- Block 1 can contain other loop statements as
follows -
- Block 1 ? for (Initializing Boolean_Expression
Update) - Block 2
8- Nested Loops
- Loops can be nested, just like other Java
structures - When nested, the inner loop iterates from
beginning to end for each single iteration of the
outer loop - int rowNum, columnNum
- for (rowNum 1 rowNum lt3 rowNum)
-
- for (columnNum 1 columnNum lt2
columnNum) -
- System.out.print(" row " rowNum "
column " columnNum) -
- System.out.println()
9- continue, break, and exit Statements
Class test public static void main( String
args) for (int i 0 i lt 10 i)
statement 1 statement 2 if( cond)
contine statement 3 statement 4
statement 5 statement 6
10 - continue, break, and exit Statements
Class test public static void main( String
args) for (int i 0 i lt 10 i)
statement 1 statement 2 if( cond)
break statement 3 statement 4
statement 5 statement 6
11 - continue, break, and exit Statements
Class test public static void main( String
args) for (int i 0 i lt 10 i)
statement 1 statement 2 if( cond)
System.exit(0) statement 3 statement 4
statement 5 statement 6
12For-loop examples
13Questions
- Write a Java program which computes the sum of
all the odd numbers between 0 and 100. - Write a Java program which reads 20 numbers using
a scanner and computes their average. - Write a Java program which reads unknown number
of integers using a scanner and counts the number
of odd numbers and the number of even numbers.
Assume the input integers are all positive. Use a
negative number as a sentinel.
November 20, 2009
ICS102 while do-while
13
14Q1 Solution
Write a Java program which computes the sum of
all the odd numbers between 0 and 100.
- int sum 0
- for( int n 1 n lt 100 n n 2)
- sum n
-
- System.out.println(The sum is sum)
November 20, 2009
ICS102 while do-while
14
15Q2 Solution
Write a Java program which reads 20 numbers using
a scanner and computes their average.
- Scanner kb new Scanner(System.in)
- double x
- double sum 0
- for (int cnt 0 cnt lt 20 cnt)
- System.out.println(Enter a number)
- x kb.nextDouble()
- sum x
-
- System.out.println(The Average is sum/cnt)
November 20, 2009
ICS102 while do-while
15
16Q3 Solution
Write a Java program which reads unknown number
of integers using a scanner and counts the number
of odd numbers and the count of even numbers.
Assume the input integers are all positive. Use
any negative number as a sentinel.
- Scanner kb new Scanner(System.in)
- int even_cnt 0
- int odd_cnt 0
- int n
- For()
- n kb.nextInt()
- if (n lt 0)
- break
- else if ( n2 0)
- even_cnt
- else
- odd_cnt
-
- System.out.println(Even even_count odd
odd_cnt)
November 20, 2009
ICS102 while do-while
16
17Nested-loop examples
18Questions
- 1. Write a java program which gives the following
output - 1
- 22
- 333
- 4444
- 55555
- Write a java program which prints all the prime
numbers less than 1000. -
19Q1 Solution
- Write a java program which gives the following
output - 1
- 22
- 333
- 4444
-
- for(int k 1 k lt 5 k)
- For ( int j 1 j ltk j)
- System.out.print(k)
- System.out.println()
-
20Q2 solution
- Write a java program which prints all the prime
numbers less than 1000. - int n, j
- for(int k 2 k lt 100 k)
- n 0
- j 2
- while(n 0 j lt k/2)
- if (kj 0) n
- j
-
- if( n 0) System.out.println(k)
-
21THE END
22Nested Loops Tracing
23Nested Loops
- Very simple for loop
- public class SimpleLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- System.out.println(i)
-
-
-
- What does it do?
24Nested Loops
- Very simple for loop
- public class SimpleLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- System.out.println(i)
-
-
-
- What does it do? Prints
1 2 3
25Nested Loops
- Very simple for loop
- public class SimpleLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- System.out.println(i)
-
-
-
- What if for every number below, want
multiplication table of value times 2, times 3,
etc?
1 2 3 2 4 6 3 6 9
26Nested Loops
- Very simple for loop
- public class SimpleLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- System.out.println(i)
-
-
-
- For every number printed by loop above
1 2 3 2 4 6 3 6 9
27Nested Loops
- Very simple for loop
- public class SimpleLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- System.out.println(i)
-
-
-
- For every number printed by loop above
- need another loop to print numbers in row
1 2 3 2 4 6 3 6 9
28Nested Loops
- Very simple for loop
- public class SimpleLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- System.out.println(i)
-
-
-
- For every number printed by loop above
- need another loop to print numbers in row
1 2 3 2 4 6 3 6 9
How do we do that?
29Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
-
30Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i
31Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i
32Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
1
33Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
1
34Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
1
1_
35Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
2
1_
36Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
2
1_
37Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
2
1 2_
38Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
3
1 2_
39Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
3
1 2_
40Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
3
1 2 3_
41Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
4
1 2 3_
42Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
4
1 2 3_
43Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
1
i j
4
1 2 3 _
44Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
4
1 2 3 _
45Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
4
1 2 3 _
46Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
1
1 2 3 _
47Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
1
1 2 3 _
48Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
1
1 2 3 2_
49Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
2
1 2 3 2_
50Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
2
1 2 3 2_
51Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
2
52Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
3
53Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
3
54Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
3
55Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
4
56Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
4
57Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
2
i j
4
58Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
4
59Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
4
60Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
1
61Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
1
62Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
1
63Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
2
64Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
2
65Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
2
66Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
3
67Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
3
68Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
3
69Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
4
70Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
4
71Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
3
i j
4
72Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
4
i j
4
73Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
4
i j
4
74Nested Loops
- Put a loop inside a loop
- trace to see how it works
- public class NestedLoop
-
- public static void main (String args)
-
- for (int i 1 i lt 3 i)
-
- for (int j 1 j lt 3 j)
-
- System.out.print((i j) " ")
-
- System.out.println()
-
-
4
i j
4
Exit!