+ All Categories
Home > Documents > October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.

October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.

Date post: 03-Jan-2016
Category:
Upload: annabelle-letitia-freeman
View: 224 times
Download: 0 times
Share this document with a friend
74
March 27, 2022 ICS102: For Loop 1 The for-loop and Nested loops
Transcript

April 20, 2023 ICS102: For Loop 1

The for-loop and Nested loops

April 20, 2023 ICS102: For Loop 2

Outline

The for Statement Syntax Semantics of the for Statement Nested Loops continue, break, and exit Statements

April 20, 2023 ICS102: For Loop 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

April 20, 2023 ICS102: For Loop 4

- …The for Statement

Scanner in = new Scanner(System.in);

int i, sum = 0, number;

for (i = 0; i < 20; i++) {

number = in.nextInt();

sum += number;

}These statements are executed for 20 times ( i = 0, 1, 2, … , 19).

These statements are executed for 20 times ( i = 0, 1, 2, … , 19).

April 20, 2023 ICS102: For Loop Chapter 7 - 5

for ( i = 0 ; i < 20 ; i++ ) {

number = in.nextInt();

sum += number;

}

- The for Statement Syntax

for ( <initialization>; <boolean expression>; <update> )

<statement> // OR Block of Statments

InitializationInitialization Boolean Expression

Boolean Expression UpdateUpdate

Statement(loop body)

Statement(loop body)

April 20, 2023 ICS102: For Loop Chapter 7 - 6

- The for Control Flow

i = 0;i = 0;

false

number = in.nextInt( );sum += number;

number = in.nextInt( );sum += number;

i ++;i ++;

i < 20 ? i < 20 ?

true

April 20, 2023 ICS102: For Loop 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

April 20, 2023 ICS102: For Loop 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 <=3; rowNum++){ for (columnNum = 1; columnNum <=2; columnNum++)

{ System.out.print(" row " + rowNum + " column " +

columnNum);}

System.out.println();}

April 20, 2023 ICS102: For Loop 9

- continue, break, and exit Statements …

Class test { public static void main( String [] args) { for (int i = 0; i < 10; i++) { statement 1;

statement 2;if( cond) contine;statement 3;statement 4;

} statement 5; statement 6; }}

April 20, 2023 ICS102: For Loop 10

… - continue, break, and exit Statements …

Class test { public static void main( String [] args) { for (int i = 0; i < 10; i++) { statement 1;

statement 2;if( cond) break;statement 3;statement 4;

} statement 5; statement 6; }}

April 20, 2023 ICS102: For Loop 11

… - continue, break, and exit Statements

Class test { public static void main( String [] args) { for (int i = 0; i < 10; i++) { statement 1;

statement 2;if( cond) System.exit(0);statement 3;statement 4;

} statement 5; statement 6; }}

April 20, 2023 ICS102: For Loop 12

For-loop examples

April 20, 2023 ICS102: For Loop 13April 20, 2023 ICS102: while & do-while 13

Questions

1. Write a Java program which computes the sum of all the odd numbers between 0 and 100.

2. Write a Java program which reads 20 numbers using a scanner and computes their average.

3. 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.

April 20, 2023 ICS102: For Loop 14April 20, 2023 ICS102: while & do-while 14

Q1 Solution

int sum = 0;for( int n = 1; n <= 100; n = n + 2)

{ sum += n;}System.out.println(“The sum is “ +

sum);

Write a Java program which computes the sum of all the odd numbers between 0 and 100.

April 20, 2023 ICS102: For Loop 15April 20, 2023 ICS102: while & do-while 15

Q2 Solution

Scanner kb = new Scanner(System.in);double x;double sum = 0;for (int cnt = 0; cnt < 20; cnt++) { System.out.println(“Enter a number”);

x = kb.nextDouble(); sum += x;}System.out.println(“The Average is “ +

sum/cnt);

Write a Java program which reads 20 numbers using a scanner and computes their average.

April 20, 2023 ICS102: For Loop 16April 20, 2023 ICS102: while & do-while 16

Q3 Solution

Scanner kb = new Scanner(System.in);int even_cnt = 0;int odd_cnt = 0;int n;For(;;) { n = kb.nextInt(); if (n < 0) break; else if ( n%2 == 0) even_cnt++; else odd_cnt++; }System.out.println(“Even = “ + even_count + “ odd = “

odd_cnt);

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.

April 20, 2023 ICS102: For Loop 17

Nested-loop examples

April 20, 2023 ICS102: For Loop 18

Questions

1. Write a java program which gives the following output122333444455555

2. Write a java program which prints all the prime numbers less than 1000.

April 20, 2023 ICS102: For Loop 19

Q1 Solution

Write a java program which gives the following output1223334444

for(int k = 1; k <= 5; k++) {For ( int j = 1; j <=k; j++)

System.out.print(k);System.out.println();

}

April 20, 2023 ICS102: For Loop 20

Q2 solution

Write a java program which prints all the prime numbers less than 1000.

int n, j;

for(int k = 2; k < 100; k++) {

n = 0;

j = 2;

while(n == 0 && j < k/2) {

if (k%j == 0) n++;

j++;

}

if( n ==0) System.out.println(k);

}

April 20, 2023 ICS102: For Loop 21

THE END

April 20, 2023 ICS102: For Loop 22

Nested Loops Tracing

Nested Loops

Very simple for loop

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

What does it do?

Nested Loops

Very simple for loop

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

What does it do? Prints

123

Nested Loops

Very simple for loop

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 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 63 6 9

Nested Loops

Very simple for loop

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

For every number printed by loop above

1 2 32 4 63 6 9

Nested Loops

Very simple for loop

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

For every number printed by loop above need another loop to print numbers in row

1 2 32 4 63 6 9

Nested Loops

Very simple for loop

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

For every number printed by loop above need another loop to print numbers in row

1 2 32 4 63 6 9

How do we do that?

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i 1

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i 1

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

1

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

1

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

1

1_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

2

1_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

2

1_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

2

1 2_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

3

1 2_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

3

1 2_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

3

1 2 3_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

4

1 2 3_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

4

1 2 3_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

1

4

1 2 3

_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

4

1 2 3

_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

4

1 2 3

_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

1

1 2 3

_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

1

1 2 3

_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

1

1 2 3

2_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

2

1 2 3

2_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

2

1 2 3

2_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

2

1 2 32 4_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

3

1 2 32 4_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

3

1 2 32 4_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

3

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

4

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

4

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

2

4

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

4

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

4

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

1

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

1

1 2 32 4 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

1

1 2 32 4 63_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

2

1 2 32 4 63_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

2

1 2 32 4 63_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

2

1 2 32 4 63 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

3

1 2 32 4 63 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

3

1 2 32 4 63 6_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

3

1 2 32 4 63 6 9_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

4

1 2 32 4 63 6 9_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

4

1 2 32 4 63 6 9_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

3

4

1 2 32 4 63 6 9_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

4

4

1 2 32 4 63 6 9_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

4

4

1 2 32 4 63 6 9_

Nested 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 <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

i

j

4

4

1 2 32 4 63 6 9_

Exit!


Recommended