0% found this document useful (0 votes)
57 views

Tutorial 4 Problem Solving: Repetition (Nested Loop) & Conditional Structures

A nested loop is when one loop is placed inside another loop. Nested loops are useful when you need to perform repetitive tasks multiple times. The outer loop must complete all its iterations before the inner loop can start its next iteration. Both for and while loops can be nested as outer and inner loops. A nested loop consists of at least one loop inside another loop. You can nest loops multiple times.

Uploaded by

Farah Cakey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Tutorial 4 Problem Solving: Repetition (Nested Loop) & Conditional Structures

A nested loop is when one loop is placed inside another loop. Nested loops are useful when you need to perform repetitive tasks multiple times. The outer loop must complete all its iterations before the inner loop can start its next iteration. Both for and while loops can be nested as outer and inner loops. A nested loop consists of at least one loop inside another loop. You can nest loops multiple times.

Uploaded by

Farah Cakey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Tutorial 4

Problem Solving: Repetition (nested loop) & conditional


structures

Section A: Self-Test
1. What is a ‘nested’ loop?

2. In what situation you need a nested loop?

3. In nested loops, the outer loop must be terminated before the inner loop.
a. True b. False

4. Only For loops can be nested.


a. True b. False

5. We can use for loops as outer loops and while loops as inner loops.
a. True b. False

6. What makes up a nested loop?


a. At least one loop inside of another loop
b. One loop placed after another loop
c. At least one If statement inside of a loop
d. At least one Wait statement inside of a loop

7. You can only use nested loops once.


a. True b. False
Section B: Hand Tracing
1. How many times is the println statement executed?
for(int i=1;i<2;i++) {
for(int j=0;j<5;j++) {
System.out.println(j + " ");
}
}

2. What is the output of the following code segments?


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("(" + i + "," + j + ") ");
}
}

3. What is the output of the following code segments?


int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(j + " ");
}
System.out.println();
}

4. What is the output of the following code segments?


for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((i+j >= 5))
System.out.print('1');
else
System.out.print('0');
}
System.out.println();
}
5. Consider the following code segments.
1 Scanner sc = new Scanner(System.in);
2 int n, k, p, sum;
3 double avg;
4 n = sc.nextInt();
5 for(int i = 0; i < n; i++) {
6 k = sc.nextInt();
7 sum = 0;
8 for(int j = 0; j < k; j++) {
9 p = sc.nextInt();
10 sum = sum + p;
11 }
12 avg = (double) sum / k;
13 System.out.println(avg);
14 }

What is the output of the code segments if the input is as follows.


3
5 20 35 44 57 68
3 21 66 9
4 11 12 13 14

Section C: Write Code Segments


1. Write code segments that print a square based on the specified input which indicates the size
of the square. For example, if the input is 5, then the output is as follows.
11111
22222
33333
44444
55555
2. Write code segments that print a triangle based on the specified input which indicates the height
of the triangle. For example, if the input is 5, then the output is as follows.
1
22
333
4444
55555

3. Write code segments that print a square based on the specified input which indicates the size
of the square. For example, if the input is 5, then the output is as follows.
+-+-+
+-+-+
+-+-+
+-+-+
+-+-+

4. Write code segments that print a square based on the specified input which indicates the size
of the square. For example, if the input is 4 5, then the output is as follows.
*****
* *
* *
*****

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy