Chapter 6 Slides - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Chapter 6 Slides

Description:

System.out.println('Eat at Joe's friendly diner for the best lunch value' ... Joe's friendly diner for the best lunch value. Eat at Joe's friendly diner for the ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 63
Provided by: johns379
Category:
Tags: chapter | diner | slides

less

Transcript and Presenter's Notes

Title: Chapter 6 Slides


1
Exposure Java
Chapter 6 Slides
Control Structures I
PowerPoint Presentation created by Mr. John L.
M. Schram
From Materials Created by Mr. Leon Schram
2
Program Flow
Program Flow follows the exact sequence of
listed program statements, unless directed
otherwise by a Java control structure.
3
Types of Control Structures
  • Program execution flow is controlled by three
    general types of control structures.
  • They are simple sequence, selection, and
    repetition. Java provides syntax, and special
    keywords for each one of the three control
    structures.
  • Before we look at actual Java source code
    required to implement control, let us first take
    a look at diagrams that explain each control
    structure.

4
Simple Sequence

Program Statement 1
5
Selection
Frequently, programs cannot follow a single,
simple sequence, path. Decisions need to be
made. Should the applicant be hired or not?
Does the employee get overtime pay? Which tax
bracket is the deduction to be computed from?
Selection is also called conditional branching
or decision making or sometimes even decision .
The program flow encounters a special condition.
The value of the condition determines if the
program flow will branch off from the main
program sequence. Three diagrams will be
shown. The first diagram shows one-way
selection, the second diagram shows two-way
selection, and the third one shows multiple-way
selection.
6
One-Way Selection
7
Two-Way Selection
8
Multi-Way Selection
9
Repetition

Program Statement
Program Statement
Program Statement
Condition
True
False
Program Statement
Program Statement
10
Conditional Statement Definition
A conditional statement is a program expression
that evaluates to true or false. Most
conditional statements require a relational
operator. All conditions must be placed inside
parentheses.
11
Relational Operators
12
Program Input
For the examples in this Chapter to be meaningful
we need the ability for the user to enter
information. Before we look at programs
demonstrating Control Structures, we will see 5
programs that introduce Command Line Input.
Java has MANY, MANY ways to do input, some of
which you will learn later this year. For
right now, Command Line Input is the simplest.
13
// Java0601.java // This program demonstrates how
an argument can be entered from the // command
line using the args array. This program
expects a single argument. public class
Java0601 public static void main(String
args) System.out.println("\nJAVA0615.JAVA\n
") System.out.print("String entered at command
line ") System.out.println(args0) System
.out.println()
Java0601.java Output 1 C\Java\Progs06gtjava
Java0601 Wednesday String entered at command
line Wednesday
Java0601.java Output 2 C\Java\Progs06gtjava
Java06101 Isolde Schram String entered at command
line Isolde
Java0601.java Output 3 C\Java\Progs06gtjava
Java0601 "Isolde Schram" String entered at
command line Isolde Schram
14
// Java0602.java // This program demonstrates
entering three arguments at the command line. //
There is no provision made for entering the wrong
number of arguments. public class
Java0602 public static void main(String
args) System.out.println("\nJAVA0602.JAVA\n
") System.out.println("Argument-1 "
args0) System.out.println("Argument-2 "
args1) System.out.println("Argument-3 "
args2) System.out.println()
Java0602.java Output C\Java\Progs06gtjava
Java0602 One Two Three Argument-1 One Argument-2
Two Argument-3 Three
15
// Java0603.java // This program demonstrates
string concatenation with the entered //
arguments. public class Java0603 public
static void main(String args) System.out.pr
intln("\nJAVA0603.JAVA\n") System.out.println("
Arg0 Arg1 " args0
args1) System.out.println()
Java0603.java Output C\Java\Progs06gtjava
Java0603 100 300 Arg0 Arg1 100300
16
// Java0604.java // This program enters two
numerical strings, converts the strings // to
integers and computes the sum of the
integers. public class Java0604 public static
void main(String args) System.out.println("
\nJAVA0604.JAVA\n") String strInput1,
strInput2 int intNum1, intNum2 int
intSum strInput1 args0 strInput2
args1 intNum1 Integer.parseInt(strInput1)
intNum2 Integer.parseInt(strInput2) intSum
intNum1 intNum2 System.out.println(intNum1
" " intNum2 " " intSum) System.out.
println()
Java0604.java Output C\Java\Progs06gtjava
Java0604 100 200 100 200 300
17
// Java0605.java // This program enters three
real numbers at the command line // and displays
the mean of the entered numbers. public class
Java0605 public static void main(String
args) System.out.println("\nJAVA0605.JAVA\n
") double dblNum1, dblNum2, dblNum3 double
dblMean dblNum1 Double.parseDouble(args0)
dblNum2 Double.parseDouble(args1) dblNum3
Double.parseDouble(args2) dblMean
(dblNum1 dblNum2 dblNum3) /
3 System.out.println("Number1 "
dblNum1) System.out.println("Number2 "
dblNum2) System.out.println("Number3 "
dblNum3) System.out.println("Mean "
dblMean) System.out.println()
Java0605.java Output C\Java\Progs06gtjava
Java0605 1.23 2.31 3.12 Number1 1.23 Number2 2.3
1 Number3 3.12 Mean 2.22
18
Command Line Argument Notes
All command line arguments are entered as
strings, separated by a space between multiple
arguments. If a space is meant to be part of the
entered string, such as "Hello there", the string
must be placed between quotes. Strings arguments
are entered into the args variable with an
index value starting with args0 for the first
argument, args1 for the second argument, and so
on. Integer.parseInt and Double.parseDouble
convert strings to integers and real numbers.
19
Main Method Arguments with JCreator
It IS possible to enter main method arguments
while using JCreator as well! Do the
following Step 1 Look for the toolbar at the
top of the JCreator IDE. Click Configure
followed by clicking on Options. Step 2 Click
JDK Tools on the left side of the Options
window. Step 3 Look for the Select Tool Type
window. If Run Application appears, skip to the
next step, if not pull down the window and
click on Run Application Step 4 Click on
ltdefaultgt. Make sure the ltdefaultgt bar becomes
shaded. Click on the Edit button. The Tool
Configuration Run Application window
appears Step 5 Click on the Parameters tab. The
Tool Configuration Run Application window
will change. Click a check-mark in the box
next to Prompt for main function arguments,
Step 6 Click OK twice. You are now ready to
execute your program.
20
JCreator Main Method Argument Notes
If you have done all of the steps on the previous
slide, the following window will show up whenever
you execute a Java application. NOTE Applets
dont have a main method, and hence no main
method arguments. You enter the exact same data
here that you would type after the file name at
the DOS prompt. An example of Java0605 data is
shown below. If this window shows up, and your
program needs no data, just click ltOKgt. Clicking
ltCancelgt will terminate program execution.
21
// Java0606.java // This program demonstrates
one-way selection with ltifgt. // Run the program
twice. First with Sales equals to 300,000 //
and a second time with Sales equals
500,000. public class Java0606 public static
void main (String args) System.out.println(
"\nJAVA0606.JAVA\n") double sales
Double.parseDouble(args0) double bonus
250.00 if (sales gt 500000.0) bonus
500.0 System.out.println("Yearly bonus "
bonus) System.out.println()
Java0606.java Output 1 with 300000 Yearly
Bonus 250.0
Java0606.java Output 1 with 500000 Yearly
Bonus 750.0
22
Indentation Rule
Java syntax uses freeform program style. Program
statements may be placed on multiple lines with
or without indentation. By convention control
structures and their conditional statements are
placed on one line. The program statement that
is executed, if the condition is true, is placed
on the next line, and indented below the
conditional statement. if(Sales gt500000)
Bonus 500 if(Sales gt500000) Bonus
500
23
// Java0607.java // This program demonstrates
one-way selection with ltifgt. // It also shows
that only one statement is controlled. // Run the
program twice. First with Sales equals to
300,000 // and then a second time with Sales
equals to 500,000. public class
Java0607 public static void main (String
args) System.out.println("\nJAVA0607.JAVA\n
") double sales Double.parseDouble(args0)
double bonus 250.00 if (sales gt
500000.0) bonus 500.0 System.out.println
("Your sales gt 500,000.00") System.out.print
ln("You will receive 500.00 extra bonus.")
System.out.println("Yearly bonus "
bonus) System.out.println()
Java0607.java Output 1 with 300000 Your sales
gt 500,000.00 You will receive 500.00 extra
bonus. Yearly Bonus 250.0
Java0607.java Output 1 with 500000 Your sales
gt 500,000.00 You will receive 500.00 extra
bonus. Yearly Bonus 750.0
24
// Java0608.java // This program demonstrates
one-way selection with ltifgt. // It fixes the
logic problem of the previous program // with
block structure by using braces. public class
Java0608 public static void main (String
args) System.out.println("\nJAVA0608.JAVA\n
") double sales Double.parseDouble(args0)
double bonus 250.00 if (sales gt
500000.0) bonus 500.0 System.out.pri
ntln("Your sales gt 500,000.00") System.out.p
rintln("You will receive 500.00 extra
bonus.") System.out.println("Yearly
bonus " bonus) System.out.println()
Java0608.java Output 1 with 300000 Yearly
Bonus 250.0
Java0608.java Output 2 with 500000 Your sales
gt 500,000.00 You will receive 500.00 extra
bonus. Yearly Bonus 750.0
25
One-Way Selection Syntax
One-Way selection general syntax if (condition
true) execute program statement if
(Counter gt 100) System.out.println("Counter
exceeds 100") Use braces and block
structure to control multiple program statements.
if (Savings gt 10000)
System.out.println("Its skiing time")
System.out.println("Lets pack")
System.out.println("Remember your skis")
26
// Java0609.java // This program demonstrates
two-way selection with ltif..elsegt. public class
Java0609 public static void main (String
args) System.out.println("\nJAVA0609.JAVA\n
") int sat Integer.parseInt(args0) if
(sat gt 1100) System.out.println("You are
admitted") else System.out.println("You are
not admitted") System.out.println()
Java0609.java Output 1 with 1000 You are not
admitted
Java0609.java Output 2 with 1200 You are
admitted
27
// Java0610.java // This program demonstrates
two-way selection with ltif..elsegt. // Multiple
statements require the use of block
structure. public class Java0610 public static
void main (String args) System.out.println(
"\nJAVA0610.JAVA\n") int sat
Integer.parseInt(args0) if (sat gt
1100) System.out.println("You are
admitted") System.out.println("Orientation
will start in June") else System.out
.println("You are not admitted") System.out.pr
intln("Please try again when your SAT
improves.") System.out.println()
Java0610.java Output 1 with 1000 You are not
admitted Please try again when your SAT improves.
Java0610.java Output 2 with 1200 You are
admitted Orientation will start in June
28
Two-Way Selection Syntax
Two-Way selection general syntax if
(condition true) execute first program
statement else // when condition is
false execute second program statement if
(GPA gt 90.0) System.out.println ( "Youre
an honor graduate") else
System.out.println ("Youre not an honor
graduate")
29
// Java0611.java // This program demonstrates
multi-way selection with ltswitchgt and ltcasegt. //
This program compiles, but displays illogical
output. public class Java0611 public static
void main (String args) System.out.println(
"\nJAVA0611.JAVA\n") char grade
args0.charAt(0) // converts String character
to char type switch (grade) case 'A'
System.out.println("90 .. 100 Average") case
'B' System.out.println("80 .. 89
Average") case 'C' System.out.println("70
.. 79 Average") case 'D'
System.out.println("60 .. 69 Average") case
'F' System.out.println("Below 60
Average") System.out.println()
Java0611.java Output with C 70 .. 79 Average 60
.. 69 Average Below 60 Average
30
// Java0612.java // This program demonstrates
multi-way selection with ltswitchgt and ltcasegt.
This // program adds ltbreakgt and ltdefaultgt.
ltbreakgt is required for logical output. public
class Java0612 public static void main (String
args) System.out.println("\nJAVA0612.JAVA\n
") char grade args0.charAt(0) //
converts String character to char type switch
(grade) case 'A' System.out.println("90
.. 100 Average") break case 'B'
System.out.println("80 .. 89 Average") break
case 'C' System.out.println("70 .. 79
Average") break case 'D' System.out.println
("60 .. 69 Average") break case 'F'
System.out.println("Below 60 Average") break
default System.out.println("No Match
Found") System.out.println()
Java0612.java Output 1 with A 90 .. 100 Average
Java0612.java Output 2 with C 70 .. 79 Average
Java0612.java Output 3 with Q No Match Found
31
Multi-Way Selection Syntax
switch(selectionVariable) case
selectionConstant program statement break
case selectionConstant program
statement break default
program statement
switch(courseGrade) case A
points 4 break case B points
3 break case C points 2
break case D points 1 break
case F points 0 break
default points 0 The default
statement is used to handle the situation when a
proper match is not found. Frequently an error
message is used to indicate that no match was
found.
32
// Java0613.java // This program displays 40
identical lines very inefficiently // with 40
separate println statements. public class
Java0613 public static void main(String
args) System.out.println("\nJAVA0613.JAVA\n
") System.out.println("Eat at Joe's friendly
diner for the best lunch value") System.out.pri
ntln("Eat at Joe's friendly diner for the best
lunch value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch
value") System.out.println("Eat at Joe's
friendly diner for the best lunch value") . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . .
33
Java0613.java Output Eat at Joe's friendly diner
for the best lunch value Eat at Joe's friendly
diner for the best lunch value Eat at Joe's
friendly diner for the best lunch value Eat at
Joe's friendly diner for the best lunch value Eat
at Joe's friendly diner for the best lunch
value Eat at Joe's friendly diner for the best
lunch value Eat at Joe's friendly diner for the
best lunch value Eat at Joe's friendly diner for
the best lunch value Eat at Joe's friendly diner
for the best lunch value Eat at Joe's friendly
diner for the best lunch value Eat at Joe's
friendly diner for the best lunch value Eat at
Joe's friendly diner for the best lunch value Eat
at Joe's friendly diner for the best lunch
value Eat at Joe's friendly diner for the best
lunch value Eat at Joe's friendly diner for the
best lunch value Eat at Joe's friendly diner for
the best lunch value Eat at Joe's friendly diner
for the best lunch value Eat at Joe's friendly
diner for the best lunch value Eat at Joe's
friendly diner for the best lunch value Eat at
Joe's friendly diner for the best lunch value Eat
at Joe's friendly diner for the best lunch
value . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
34
// Java0614.java // This program displays 40
identical lines efficiently // with one println
statement and a ltforgt loop structure. public
class Java0614 public static void main(String
args) System.out.println("\nJAVA0614.JAVA\n
") int k for (k 1 k lt 40 k)
System.out.println("EAT AT JOE'S FRIENDLY
DINER FOR THE BEST LUNCH VALUE") System.out.pri
ntln()
35
Java0614.java Output EAT AT JOE'S FRIENDLY DINER
FOR THE BEST LUNCH VALUE EAT AT JOE'S FRIENDLY
DINER FOR THE BEST LUNCH VALUE EAT AT JOE'S
FRIENDLY DINER FOR THE BEST LUNCH VALUE EAT AT
JOE'S FRIENDLY DINER FOR THE BEST LUNCH VALUE EAT
AT JOE'S FRIENDLY DINER FOR THE BEST LUNCH
VALUE EAT AT JOE'S FRIENDLY DINER FOR THE BEST
LUNCH VALUE EAT AT JOE'S FRIENDLY DINER FOR THE
BEST LUNCH VALUE EAT AT JOE'S FRIENDLY DINER FOR
THE BEST LUNCH VALUE EAT AT JOE'S FRIENDLY DINER
FOR THE BEST LUNCH VALUE EAT AT JOE'S FRIENDLY
DINER FOR THE BEST LUNCH VALUE EAT AT JOE'S
FRIENDLY DINER FOR THE BEST LUNCH VALUE EAT AT
JOE'S FRIENDLY DINER FOR THE BEST LUNCH VALUE EAT
AT JOE'S FRIENDLY DINER FOR THE BEST LUNCH
VALUE EAT AT JOE'S FRIENDLY DINER FOR THE BEST
LUNCH VALUE EAT AT JOE'S FRIENDLY DINER FOR THE
BEST LUNCH VALUE EAT AT JOE'S FRIENDLY DINER FOR
THE BEST LUNCH VALUE EAT AT JOE'S FRIENDLY DINER
FOR THE BEST LUNCH VALUE EAT AT JOE'S FRIENDLY
DINER FOR THE BEST LUNCH VALUE EAT AT JOE'S
FRIENDLY DINER FOR THE BEST LUNCH VALUE EAT AT
JOE'S FRIENDLY DINER FOR THE BEST LUNCH VALUE EAT
AT JOE'S FRIENDLY DINER FOR THE BEST LUNCH
VALUE . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
36
// Java0615.java // This program displays
consecutive numbers 1 through 15. // It also
shows how the loop control variable may be //
defined inside the ltforgt program
statement. public class Java0615 public
static void main(String args) System.out.pr
intln("\nJAVA0615.JAVA\n") for (int k 1 k
lt 15 k) System.out.print(k "
") System.out.println()
Java0615.java Output 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15
37
// Java0616.java // This program demonstrates how
to use block structure with a ltforgt loop control
structure. public class Java0616 public
static void main(String args) System.out.pr
intln("\nJAVA0616.JAVA\n") for (int k 1 k
lt 5 k) System.out.println("
") System.out.print
ln("Line Number " k) System.out.println()

Java0616.java Output
Line Number 1
Line Number 2
Line Number 3
Line Number 4
Line Number 5
38
// Java0617.java // This program displays various
counting schemes. // It also demonstrates the
versatility of the for loop. public class
Java0617 public static void main(String
args) System.out.println("\nJAVA0617.JAVA\n
") for (int p 1 p lt 15 p)
System.out.print(p " ") System.out.print
ln() for (int q 1 q lt 15 q3)
System.out.print(q " ") System.out.print
ln() for (int r 15 r gt 1 r--)
System.out.print(r " ") System.out.print
ln() for (double s 0 s lt 3 s0.5)
System.out.print(s " ") System.out.print
ln() for (char t 'A' t lt 'Z'
t) System.out.print(t "
") System.out.println("\n\n")
Java0617.java Output 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 1 4 7 10 13 15 14 13
12 11 10 9 8 7 6 5 4 3 2 1 0.0 0.5
1.0 1.5 2.0 2.5 3.0 A B C D E F G H I
J K L M N O P Q R S T U V W X Y Z
39
Fixed Repetition with ltforgt
for (Part1 Part2 Part3) loop body The
for loop has three distinct parts Part1
initializes the Loop Control Variable
(LCV). Part2 sets the exit condition for the
loop. Part3 determines how the LCV changes. for
(k 1 k lt 10 k) System.out.println("J
ava is 10 times more fun")
40
Note to Teachers and Studentswith Advanced
Knowledge
It is possible to treat the for loop structure
like a conditional loop that is not fixed. In
fact, a for loop can be designed to behave
exactly like a while loop. It is my intention to
use and treat a for loop like a fixed
iteration loop and use the while loop and
do...while loop for other repetition
situations. This approach is less likely to
cause confusion. At some later date, when you
are comfortable with all the control
structures, you can use them in any appropriate
manner. If this does not make sense to you,
good, ignore this little summary box, and move on.
41
// Java0618.java // This program
demonstrates the precondition ltwhilegt
loop. public class Java0618 public static void
main(String args) System.out.println("\nJAV
A0618.JAVA\n") int p 1 int q 1 int r
15 double s 0 char t 'A' while (p lt
15) System.out.print(p "
") p System.out.println() while (q
lt 15) System.out.print(q "
") q3 System.out.println() while (r
gt 0) System.out.print(r "
") r-- System.out.println() while (s lt
3) System.out.print(s " ") s
0.5 System.out.println() while (t lt
'Z') System.out.print(t "
") t System.out.println("\n\n")
Java0618.java Output 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 1 4 7 10 13 15 14 13
12 11 10 9 8 7 6 5 4 3 2 1 0.0 0.5
1.0 1.5 2.0 2.5 3.0 A B C D E F G H I
J K L M N O P Q R S T U V W X Y Z
42
while Loop Syntax
initialize condition variable while(condition is
true) loop body alter condition
variable in loop body x 0 //
initialize condition variable while(x lt 10)
x // alter condition variable
System.out.println("x " x)
43
(No Transcript)
44
(No Transcript)
45
// Java0619.java // This program demonstrates the
postcondition ltdo..whilegt loop. public class
Java0619 public static void main(String
args) System.out.println("\nJAVA0619.JAVA\n
") int p 1 int q 1 int r 15
double s 0 char t 'A' do
System.out.print(p " ") p while
(p lt 15) System.out.println() do
System.out.print(q " ") q3
while (q lt 15) System.out.println() do
System.out.print(r " ") r-- while
(r gt 0) System.out.println() do
System.out.print(s " ") s 0.5
while (s lt 3) System.out.println() do
System.out.print(t " ") t while
(t lt 'Z') System.out.println("\n\n")
Java0619.java Output 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 1 4 7 10 13 15 14 13
12 11 10 9 8 7 6 5 4 3 2 1 0.0 0.5
1.0 1.5 2.0 2.5 3.0 A B C D E F G H I
J K L M N O P Q R S T U V W X Y Z
46
do...while Loop Syntax
initialize condition variable while(condition is
true) loop body alter condition
variable in loop body x 0 //
initialize condition variable do x
// alter condition variable System.out.println(
"x " x) while (x lt 10)
47
Worked-Out Exercises
The next section in this chapter 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.
48
public class Ex0601 public static void main
(String args) for (int x 1 x lt 8
x2) System.out.println("x " x)

Ex0601 Answer x Output 1 x 1 3 x 3 5 x
5 7 x 7
49
public class Ex0602 public static void main
(String args) for (int x 1 x lt 8
x2) System.out.println("x " x)

Ex0602 Answer x Output 1 x 1 3 x 3 5 x
5 7 x 7
50
public class Ex0603 public static void main
(String args) for (int x 0 x lt
8 x2) System.out.println("x "
x)
Ex0603 Answer x Output 0 x 0 2 x 2 4 x
4 6 x 6 8 x 8
51
public class Ex0604 public static void main
(String args) int x 0 int y
0 for (x 1 x lt 5 x)
y System.out.println("y " y)

Ex0604 Answer x y Output 0 0 1 1 2 2 3 3
4 4 5 5 y 5
52
public class Ex0605 public static void main
(String args) int x 0 int y
0 for (x 1 x gt 1 x--) y
System.out.println("y " y)

Ex0605 Answer x y Output 0 0 y 0
53
public class Ex0606 public static void main
(String args) int x 0 int y
0 while (x lt 5) y
x y System.out.println("y
" y)
Ex0606 Answer y x Output 0 0 1 1 2 2 3 3
4 4 5 5 y 5
54
public class Ex0607 public static void main
(String args) int x 0 int y
0 while (x lt 10) y x
2 x y 3
System.out.println("y " y)
Ex0607 Answer y x Output 0 0 2 5 7 10 y 7
55
public class Ex0608 public static void main
(String args) int x 0 int y
0 while (x lt 10) y x
2 x
System.out.println("x " x)
System.out.println("y " y)
Ex0608 Answer 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
56
public class Ex0609 public static void main
(String args) int x 2 while
(x lt 10) if (x 2 0)
x2 else x
System.out.println("x " x)
Ex0609 Answer x x 2 0 Output 2 true 4 tru
e 6 true 8 true 10 x 10
57
public class Ex0610 public static void main
(String args) int x 2 do
if (x 2 0) x2
else x while (x lt 10)
System.out.println("x " x)
Ex0610 Answer x x 2 0 Output 2 true 4 true
6 true 8 true 10 true x 10
58
public class Ex0611 public static void main
(String args) int x 10 int
y 20 do x y 2
y x - 2 while (x lt y)
System.out.println("x " x)
Ex0611 Answer x y Output 10 20 22 20 x 22
59
public class Ex0612 public static void main
(String args) int x 10 int
y 1 do if (x 2
0) x 5 else y 2
while (y lt x)
System.out.println("x " x)
Ex0612 Answer x 2 0 x y Output 10 1 true
15 false 3 false 5 false 7 false 9
false 11 false 13 false 15 x 15
60
public class Ex0613 public static void main
(String args) int x 1 int y
3 int z 5 while (z gt 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)
Ex0613 Answer x y z Output 1 3 5 8 13 -5 x
8 y 13 z -5
61
public class Ex0614 public static void main
(String args) int x 1 int y
2 int z 3 for (int k 1 k lt
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)
Ex0614 Answer k x y z Output 1 2 3 1 5 8 -3
2 5 2 3 3 5 8 -3 4 5 2 3 5 5 8 -3 6 5 2 3
7 5 8 -3 8 5 2 3 9 5 8 -3 10 5 2 3 x
5 y 2 z 3
62
public class Ex0615 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 y y z
while (z ! 0)
Ex0615 Answer x y z Output 168 90 0 78 90 7
8 12 78 12 6 12 6 0 y 6
Write a Comment
User Comments (0)
About PowerShow.com