Transcript Exercises05

Worked Out Exercises
These slides will present a variety of small
programs. Each program has a control structure
that was introduced in this chapter.
Our concern will be with the output of each
program, and more importantly, develop some
methods to determine program output correctly,
which involves control structures.
You can expect that on quizzes, tests and the
APCS Examination only a program segment or a
method is shown.
public class Ex0501
{
public static void main (String args[])
{
for (int x = 1; x < 8; x++)
System.out.println("x = " + x);
}
}
x
Output
1
x=1
2
x=2
3
x=3
4
x=4
5
x=5
6
x=6
7
x=7
public class Ex0502
{
public static void main (String args[])
{
for (int x = 1; x <= 8; x++)
System.out.println("x = " + x);
}
}
x
Output
1
x=1
2
x=2
3
x=3
4
x=4
5
x=5
6
x=6
7
x=7
8
x=8
public class Ex0503
{
public static void main (String args[])
{
for (int x = 0; x <= 8; x+=2)
System.out.println("x = " + x);
}
}
x
Output
0
x=0
2
x=2
4
x=4
6
x=6
8
x=8
public class Ex0504
{
public static void main (String args[])
{
x
int x = 0;
0
int y = 0;
0
for (y = 1; y <= 25; y++)
0
{
0
y+=5;
0
System.out.println(y);
0
}
0
0
}
0
}
0
0
y
0
1
6
7
12
13
18
19
24
25
30
Output
6
12
18
24
30
public class Ex0505
{
public static void main (String args[])
{
x
y
int x = 0;
0
0
int y = 0;
for (x = 1; x > 1; x--)
y++;
System.out.println("y = " + y);
}
}
Output
y=0
public class Ex0506
{
public static void main (String args[])
{
y
x
int x = 0;
0
0
int y = 0;
1
1
while (x < 5)
2
2
{
3
3
y++;
4
4
x = y;
5
5
}
System.out.println("y = " + y);
}
}
Output
y=5
public class Ex0507
{
public static void main (String args[])
{
int x = 0;
y
x
int y = 0;
0
0
while (x < 10)
2
5
{
7
10
y = x + 2;
x = y + 3;
}
System.out.println("y = " + y);
}
}
Output
y=7
public class Ex0508
{
public static void main (String args[])
{
int x = 0;
int y = 0;
while (x < 10)
{
y = x * 2;
x++;
}
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
y
x Output
0 0
0 1
2 2
4 3
6 4
8 5
10 6
12 7
14 8
16 9
18 10
x = 10
y = 18
public class Ex0509
{
public static void main (String args[])
{
int x = 2;
x x % 2 == 0
while (x < 10)
2
true
{
4
true
if (x % 2 == 0)
6
true
x+=2;
else
8
true
x++;
10
}
System.out.println("x = " + x);
}
}
Output
x = 10
public class Ex0510
{
public static void main (String args[])
{
x x % 2 == 0
int x = 2;
do
2
true
{
4
true
if (x % 2 == 0)
6
true
x+=2;
8
true
else
x++;
10
true
}
while (x < 10);
System.out.println("x = " + x);
}
}
Output
x = 10
public class Ex0511
{
public static void main (String args[])
{
int x = 10;
x
y
int y = 20;
10
20
do
22
20
{
x = y + 2;
y = x - 2;
}
while (x < y);
System.out.println("x = " + x);
}
}
Output
x = 22
public class Ex0512
{
public static void main (String args[])
{
x % 2 == 0
x
int x = 10;
10
int y = 1;
true
15
do
false
{
false
if (x % 2 == 0)
false
x += 5;
false
else
false
y += 2;
false
}
false
while (y < x);
System.out.println("x = " + x);
}
}
y
Output
1
3
5
7
9
11
13
15
x = 15
public class Ex0513
{
public static void main (String args[])
{
x
y
int x = 1;
int y = 3;
1
3
int z = 5;
8
13
while (z > x + y)
{
x = y + z;
y = x + z;
z = x - y;
}
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}
}
z
5
-5
Output
x=8
y = 13
z = -5
public class Ex0514
{
public static void main (String args[])
{
int x = 1;
int y = 2;
int z = 3;
for (int k = 1; k <= 10; k++)
{
x = y + z;
y = x + z;
z = x - y;
}
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}
}
k
x
y
z
1
2
3
4
5
6
7
8
9
10
1
5
5
5
5
5
5
5
5
5
5
2
8
2
8
2
8
2
8
2
8
2
3
-3
3
-3
3
-3
3
-3
3
-3
3
Output
x=5
y=2
z=3
public class Ex0515
{
public static void main (String args[])
{
int x = 168; int y = 90; int z = 0;
do
{
z = x % y;
if (z == 0)
System.out.println("y = " + y);
else
x
{
x = y;
168
y = z;
}
90
}
78
while (z != 0);
}
12
}
y
z
90
0
78
78
12
6
12
6
0
Output
y=6