Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Control Structures (Part 1)

 Java Control Structures


 Decision Control Structures

 If

 If-Else

 If-Else-If

 Switch
Is it important?

Yes No

Do you need to do it now? Do it later.

Yes No

Do it now. Do it later.

*Property of STI J0047


• allow programmers to change the way statements are
executed

• dictate flow of control within the program

There are two types of Java Control Structures:


1. Decision Control Structures
2. Repetition Control Structures

*Property of STI J0047


• Java statements that allow a programmer to select and
execute specific blocks of code while skipping other sections

There are 4 types of Decision Control Structures in Java:


1. If
2. If-Else
3. If-Else-If
4. Switch

*Property of STI J0047


• uses the if statement in Java

• statement/block of code executed if and only if a related boolean


statement returns a value of true

• can execute several statements (compound statements) using


paired braces ( {} )

General Format:
if ([expression]){
[statement or block of code];
}
or
if ([expression]) [statement];

*Property of STI J0047


Example:
The program prints a message if the final grade of a student exceeds 75%:

public class PassFail1{

public static void main (String[] args)


{

int grade = 85;


if (grade >= 75){
System.out.println("Congratulations!");
System.out.println("You Passed!");
}
}
}

*Property of STI J0047


• uses the if-else statement in Java

• used to execute a statement if a condition is true, and another statement


if the condition is false

General Format:
if ([expression]){
[statement1/codeblock1];
}
else {
[statement2/codeblock2];
}
or
if ([expression]) [statement1];
else [statement2];

*Property of STI J0047


Example:
The program prints a message if the final grade of a student exceeds 75%, else
it would print another message if the student fails to exceed 75%:

public class PassFail2{

public static void main (String[] args)


{

int grade = 65;


if (grade >= 75){
System.out.println("Congratulations!");
System.out.println("You Passed!");
}
else{
System.out.println("Sorry!");
System.out.println("You failed!");
}
}
}

*Property of STI J0047


• uses the if-else-if statement

• used for nested structures involving more than one condition that needs to be
satisfied

General Format:
if ([expression1]) {
[statement1/codeblock1];
}
else if ([expression 2]) {
[statement2/codeblock2];
}
else {
[statement3/codeblock3];
}

*Property of STI J0047


Example:
The program prints two separate messages if the final grade of a student exceeds 85% and
75%, else it would print another message if the student fails to exceed 75%:

public class PassFail3{

public static void main (String[] args)


{

int grade = 95;


if (grade >= 85){
System.out.println("Congratulations!");
System.out.println("You Excelled!");
}
else if (grade >= 75 ){
System.out.println("Good!");
System.out.println("You Passed!");
}
else{
System.out.println("Sorry!");
System.out.println("You failed!");
}
}
}
*Property of STI J0047
There are common errors associated with using if, if-else,
and if-else-if statements:
1. the condition inside the if statement does not evaluate to a
boolean value.
2. writing elseif instead of else if.
3. using = instead of == for comparison.

Try to avoid these errors as much as possible

*Property of STI J0047


• uses the switch statement

• enables programs to switch between different statements based on a given


expression or condition

General Format:
switch ([switch expression])
{
case 1:
[statement1];
case 2:
[statement2];
case n:
[statement];
default:
..
}
*Property of STI J0047
There are a few differences of the switch statement when
compared with the if statements:
1. multiple statements/blocks of code can be executed without using
braces
2. when a case in the switch statement has been matched, statements
for the matched case and succeeding cases are executed
3. break statements must be used to prevent execution of subsequent
cases
4. cannot be used for ranged values

*Property of STI J0047


Example:
The program prints a message for each corresponding grade rating based on given
grade ranges:
public class PassFail4{
public static void main (String[] args){
char rating;
switch (rating) {
case 'S':
System.out.println("Excellent!");
break;
case 'A':
System.out.println("Good!");
break;
case 'B':
System.out.println("Study Harder!");
break;
default:
System.out.println("You failed!");
break;
}
}
}

*Property of STI J0047

You might also like