Branching statements
• Sometimesa single (or block) statement needs to be executed if a certain
condition exists.
• A branching statement is used for this purpose, and it is controlled by a
Boolean expression (true/false).
• There are mainly the following types of branching statements:
o Simple if.
o If-else.
o Multiway if-else (If-elseif-else)
o Nested if-else.
o switch.
4.
Simple if Statement…
• General structure:
• If the boolean_expression is true, then the block_statement is executed. Otherwise, it
will NOT be executed.
• The if-elsestatement is used if an alternative action need to be done in case the
condition is false.
• General structure:
if ( boolean_expression ){
then_block
}
else {
else_block
}
Executed when boolean_expression true
Executed when boolean_expression false
if-else Statement …
7.
if-else Statement
• Example:
System.out.print(“Entertwo numbers: “);
int n1 = k.nextInt();
int n2 = k.nextInt();
if ( n1 > n2 ){
System.out.println(n1 + “ is greater than ” + n2);
}
else {
System.out.println(n1 + “ is not greater than ” + n2);
}
What is printed if n1 = n2?
8.
if-elseif-else Statement …
•This statement is used for multiple alternative actions based on different
conditions. It is used when a single block needs to be executed from several blocks.
• General structure: if (condition-1)
BLOCK1
else if (condition-2)
BLOCK2
else if (condition-3)
BLOCK3
...
else if (condition-n)
BLOCKn
else
BLOCKn+1
Optional
Block i is executed if
its condition-i is true
It is executed if all
conditions are false
conditions are
evaluated from
top to bottom
9.
if-elseif-else Statement …
•The following code prints appropriate message based on the following
table:
System.out.print("Enter your score: ");
int score = k.nextInt();
if (score >= 85)
System.out.println("Grade is A");
else if (score >= 75)
System.out.println("Grade is B");
else if (score >= 65)
System.out.println("Grade is C");
else if (score >= 50)
System.out.println("Grade is D");
else
System.out.println("Grade is N");
Test Score Grade
85 score A
75 score 85 B
65 score 75 C
50 score 65 D
score 50 N
10.
The switch Statement
•The switch statement is the only other kind of Java statement that
implements multiway branching.
• When a switch statement is evaluated, one of a number of different
branches is executed.
• The choice of which branch to execute is determined by a controlling
expression enclosed in parentheses after the keyword switch
• The controlling expression must evaluate to a char, int, short, or byte and
each label is of the same type.
11.
The switch Statement
•Each branch statement in a switch statement starts with the
reserved word case, followed by a constant called a case
label, followed by a colon, and then a sequence of statements
• Each case label must be of the same type as the controlling
expression
• Case labels need not be listed in order or span a complete interval,
but each one may appear only once
• Each sequence of statements may be followed by a break
statement ( break;)
12.
• There canalso be a section labeled default:
• The default section is optional, and is usually last
• Even if the case labels cover all possible outcomes in a given switch
statement, it is still a good practice to include a default section
• It can be used to output an error message, for example
• When the controlling expression is evaluated, the code for the
case label whose value matches the controlling expression is
executed
• If no case label matches, then the only statements executed are those
following the default label (if there is one)
The switch Statement
13.
The switch Statement
•The switch statement ends when it executes a break statement,
or when the end of the switch statement is reached
• When the computer executes the statements after a case label, it
continues until a break statement is reached
• If the break statement is omitted, then after executing the code for one
case, the computer will go on to execute the code for the next case
• If the break statement is omitted inadvertently, the compiler will not
issue an error message
double y =30, z = 20;
Scanner kb = new Scanner(System.in);
System.out.println("1. add ");
System.out.println("2. Subtract ");
System.out.println("3. Multiply ");
System.out.print("Enter a value:");
int choice = kb.nextInt();
switch (choice) {
case 1: System.out.println(z + y);
break;
case 2: System.out.println(z - y);
case 3: System.out.println(z * y);
break;
default: System.out.println("Wrong Choice.");
}
break;
Enter a value: 2
-10
Enter a value: 65
Wrong Choice.
If omitted
Enter a value: 2
-10
600
The Switch Statement
16.
Block Statement (Compoundstatement)
• A block statement consists of one or more Java statements enclosed in braces.
• Example of a block statement:
{
statement 1;
statement 2;
…
statement n;
}
• It is used to group a number of statements to be done under certain condition.
• A block statement is treated like a single statement.
17.
Block Statement (Compoundstatement)
• Use braces if the <then> or <else> block has more than 1 statement. If only one
statement is there braces are optional.
System.out.print("Enter two numbers: ");
int x = k.nextInt(), y = k.nextInt(), d;
if ( x > y )
d = x – y;
System.out.println("The difference = " + d);
else
d = y – x;
System.out.println("The difference = " + d);
if ( x > y ){
d = x – y;
}else{
d = y – x;
System.out.println(“The difference = “ + d);}
Error
Correct
18.
Nested if Statement
•An if statement can be inside another if statement.
• The inner if statement is executed when the enclosing block statement is
executed.
• Always use indentation with nested if statements for better readability.
if (salary >= 5000) {
if (months >= 3) {
System.out.println("You get 50000 loan");
} else {
System.out.println("You get 20000 loan");
}
} else {
System.out.println("You cannot get a loan");
}
19.
if (x <y) {
if (x < z)
System.out.println("Hello");
} else {
System.out.println("Good bye");
}
Equivalent to
if (x < y) {
if (x < z) {
System.out.println("Hello");
}
} else {
System.out.println("Good bye");
}
This if is executed only
when x < y is true.
Nested simple if
20.
Nested if-else
Equivalent to
•In nested if statements when there is no braces, else is associated with the closest if.
if (x < y)
if (x < z)
System.out.println("Hello");
else
System.out.println("Good bye");
if (x < y) {
if (x < z) {
System.out.println("Hello");
}
else {
System.out.println("Good bye");
}
}
This if-else is executed only when x < y
is true.
Exercises
1.Write a Javaprogram that reads an integer using Scanner then
tests if it is odd or even.
The format of the input and output should be as follows:
Please enter a number: 10
10 is even.
2.Write a program that reads three integers and prints them in
increasing order. Example:
Please enter three integers: 9 21 5
The integers in increasing order are: 5 9 21
23.
3.Write a programthat reads an integer between 0 and 10
and
then prints the word corresponding to the number. Example:
Please enter a number between 0 and 10: 7
You entered: seven
Exercises
#7 If n1==n2
Flase and it will print out n1 is not greater than n2
#15 First example With all breaks
Second adding wrong choice
third example (no break after case 2)
#19 If you want the else to match with the first if, then you have to write
if (x < y) {
if (x < z)
messageBox.show("Hello");
}
else
messageBox.show("Good bye");
#20 If you want the else to match with the first if, then you have to write
if (x < y) {
if (x < z)
messageBox.show("Hello");
}
else
messageBox.show("Good bye");