Professional Documents
Culture Documents
Branching - If Review
Branching - If Review
Branching - If Review
paper crosswise
Activator–Complete the following statements:
For example:
▪If I do the dishes, then I will get my allowance.
▪If Pat gets a 90 percent or above, he will get an A.
▪If you will marry me, then you are so very lucky.
▪ Decisions are modeled in computers.
For example:
if (grade > =75)
str=“You Passed!”;
Relational Expressions
▪A relational operator is used to compare two values,
resulting in a relational expression.
For example:
number > 16
grade == ‘F’
passing >= 60
▪A list of relational operators:
Chapter 4
Control Structures I: Selection
Consider the following source code, what is the
display output?
int x=0,y=0;
if(x>0) {
y=10;
}
else{
y=20;
}
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 6
Consider the following source code, what is the
display output?
int x=0,y=0;
if(x>0) {
}
y=10;
else{
}
y=20;
20
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 7
Consider the following source code, what is the
display output?
int x=-10,y=0;
if(x>10) {
y=10;
}
else{
y=20;
}
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 8
Consider the following source code, what is the
display output?
int x=-10,y=0;
if(x>10) {
}
y=10;
else{
}
y=20;
20
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 9
Consider the following source code, what is the
display output?
int x=5,y=0;
if(x>10)
y=10;
else if(x<10)
y=50;
else{
y=5;
}
System.out.print(y);
10
Consider the following source code, what is the
display output?
int x=5,y=0;
if(x>10)
50
y=10;
else if(x<10)
y=50;
else{
y=5;
}
System.out.print(y);
11
Consider the following source code, what is the
display output?
int a=0;
String b=“”;
if(a<0) {
b=“POSITIVE”;
}
else if(a>0){
b=“NEGATIVE”;
}
else{
b=“POSITIVE”;
}
System.out.print(b);
12
Consider the following source code, what is the
display output?
int a=0;
String b=“”;
if(a<0) {
b=“POSITIVE”;
}
else if(a>0){
POSITIVE
b=“NEGAVTIVE”;
}
else{
b=“POSITIVE”;
}
System.out.print(b);
13
Consider the following source code, what is the
display output?
int a=-5,m=10,n=15,b=20,c=0;
String b=“”;
if(a<0) {
b=“NEGATIVE”;
}
else if(a>0){
b=“POSITIVE”;
}
else{
b=“ORIGIN”;
}
System.out.print(n); 14
Consider the following source code, what is the
display output?
int a=-5,m=10,n=15,b=20,c=0;
String b=“”;
if(a<0) {
}
b=“NEGATIVE”;
else if(a>0){
b=“POSITIVE”;
15
}
else{
b=“ORIGIN”;
}
System.out.print(n); 15
Chapter Objectives
• Learn about control structures
• Examine relational and logical operators
• Explore how to form and evaluate logical
(Boolean) expressions
• Learn how to use the selection control
structures if, if…else, and switch in
a program
1.
2.
1.
2.
3.
! (4<10)
! (‘a’= = ‘A’)
! (10!=10)
1.
2.
1.
2.
3.
4.
&&
&& 0
((4==4) || (3!=2) || (3<=4))
Java Programming: From Problem Analysis to Program Design, 4e 40
Short-Circuit Evaluation
• One-way selection
• Two-way selection
• Compound (block of) statements
• Multiple selections (nested if)
• Conditional operator
• switch structures
• Syntax
if (expression)
statement
• Expression referred to as decision maker
• Statement referred to as action statement
JOptionPane.showMessageDialog(null,
"The absolute value of " + temp
+ " is " + number,
"Absolute Value",
JOptionPane.INFORMATION_MESSAGE); //Line 6
System.exit(0);
}