0% found this document useful (0 votes)
48 views4 pages

Tutorial 4 Chapter 4: Loops: (Hint: What Are The Differences Between A While Loop and A Do-While Loop?)

This document contains a tutorial on loops in Java. It includes sample code for while loops, do-while loops, and for loops. It asks questions about modifying and converting different loop structures. The key points covered are: - Printing all factors of a number using a while loop - Converting a while loop to a do-while loop - Fixing a for loop to correctly calculate a sum from 1 to 10 - When to use for loops vs while loops - The behavior and output of sample code using break and continue statements in nested loops

Uploaded by

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

Tutorial 4 Chapter 4: Loops: (Hint: What Are The Differences Between A While Loop and A Do-While Loop?)

This document contains a tutorial on loops in Java. It includes sample code for while loops, do-while loops, and for loops. It asks questions about modifying and converting different loop structures. The key points covered are: - Printing all factors of a number using a while loop - Converting a while loop to a do-while loop - Fixing a for loop to correctly calculate a sum from 1 to 10 - When to use for loops vs while loops - The behavior and output of sample code using break and continue statements in nested loops

Uploaded by

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

SCE/CE9001/CM101/Tutorial4

Tutorial 4
Chapter 4: Loops
1. Consider the following code segment.
int i = 1;
while (i <= n) {
if (n % i == 0) {
System.out.print(i+" ");
}
i++;
}
(a) hat is the out!ut of the !rogram if n is "# 9# or 1$ res!ecti%el&' Can
&ou tell the !ur!ose of the !rogram'
(() )f the !rogram is modified as (elow# how its (eha%ior will (e affected'
int i = 1;
while (i <= n) {
if (n % i == 0) {
System.out.print(i+" ");
i++;
}
}
*ns+
(a)
hen n is "# the out!ut is+
1 "
hen n is 9# the out!ut is+
1 , 9
hen n is 1$# the out!ut is+
1 $ , 4 - 1$
The !rogram !rints all the factors of num(er n. That means e%er& !ositi%e num(er
that is not (igger than n and di%ides n.
(()
The !rogram will enter an infinite loo! for most %alues of n.
$. Con%ert the following while loo! into a do.while loo!.
(Hint: What are the differences between a while loop and a do-while loop?)
Scnner input = new Scnner(System.in);
int sum = 0;
System.out.println("!nter n inte"er" + "(the input
en#s if it is 0)");
int num$er = input.ne%t&nt();
1
SCE/CE9001/CM101/Tutorial4
while (num$er '= 0) {
sum += num$er;
System.out.println("!nter n inte"er" + "(the
input en#s if it is 0)");
num$er = input.ne%t&nt();
/
*ns+
Scnner input = new Scnner(System.in);
int sum = 0;
int num$er;
#o {
System.out.println("!nter n inte"er" + "(the input
en#s if it is 0)");
num$er = input.ne%t&nt();
sum += num$er;
} while (num$er '= 0);
,. 0im wanted to calculate the sum of integers from 1 to 10. 1e wrote the following
code+
int sum = 0;
for (int i = 1; i < 10; i++) {
sum += i;
}
System.out.println("1+(+)+...+10="+sum);
hat is the out!ut of the code' 2oes it wor3 as e4!ected' )f not# what is the
error'
*ns+
The out!ut is+
15$5,5...51064"
The code does not !ro%ide the sum of integers from 1 to 10 as e4!ected. )nstead# it
onl& gets the sum from 1 to 9. The error is due to one less num(er of iteration. To
correct it# one !ossi(le wa& is to change the test e4!ression to (e i 76 10.
4. Can &ou alwa&s con%ert a while loo! into a for loo!' )s it eas& to con%ert the
following while loo! into a for loo!'
int i = 1;
int sum = 0;
while (sum < 10000){
sum = sum + i;
i++;
}
*ns+
Can. 8ut not recomended.
$
SCE/CE9001/CM101/Tutorial4
int i = 1;
int sum = 0;
for (sum = 0; sum < 10000; sum += i++) {}
System.out.println("sum = "+sum+" i = " + i);
)n general# a for loo! ma& (e used if the num(er of re!etitions is 3nown# as# for
e4am!le# when &ou need to !rint a message 100 times.
* while loo! ma& (e used if the num(er of re!etitions is not 3nown# as in the
case of reading the num(ers until the in!ut is 0.
". *fter the (rea3 statement in (a) is e4ecuted in the following loo!# which statement
is e4ecuted' Show the out!ut. *fter the continue statement in (() is e4ecuted in
the following loo!# which statement is e4ecuted' Show the out!ut.
!u(lic class T4-a 9
/:: Main method :/
!u(lic static %oid main(String;< args) 9
for (int i 6 1= i 7 4= i55)9
for (int > 6 1= > 7 4= >55)9
S&stem.out.!rintln(i : >)=
if ( i : > ? $)
(rea3=
/
S&stem.out.!rintln(i)=
/
/
/
!u(lic class T4-( 9
/:: Main method :/
!u(lic static %oid main(String;< args) 9
for (int i 6 1= i 7 4= i55)9
for (int > 6 1= > 7 4= >55)9
S&stem.out.!rintln(i : >)=
if ( i : > ? $)
continue=
/
S&stem.out.!rintln(i)=
/
/
/
(a) (()
*ns+
(a) (()
i j output
,
SCE/CE9001/CM101/Tutorial4
1 1 1.!rint i:>
1 $ $ !rint i:>
1 , ,.. !rint i:> then (rea3
1 , 1 !rint i
$ 1 $@!rint i
$ $ 4.. !rint i:> then (rea3
$ $ $ !rint i
, 1 , .. !rint i:> then (rea3
, 1 , !rint i
(a)
i j output
1 1 1.!rint i:>
1 $ $ !rint i:>
1 , ,.. !rint i:> then continue
1 4..e4ceed 1 !rint i
$ 1 $@!rint i
$ $ 4.. !rint i:> then continue
$ , - . !rint i:> then continue
$ 4 ..e4ceed $ @!rint i
, 1 ,. !rint i:> then continue
, $ - . !rint i:> then continue
, , 9 . !rint i:> then continue
, 4..e4ceed ,@!rint i
4.e4ceed
(()
4

You might also like