4.3 Use of Control Structures 23
4.3 Use of Control Structures 23
0 Java Language
• General format:
if(condition)
<statements>;
if statement
• Example 1
Create a program segment that • Pseudocode
will accept student’s mark. If Begin
the mark is greater than 40, input mark
print the message "PASS". if (mark > 40)
I: mark print "PASS"
End
P: determine message based
on mark
O:"Pass"
if statement
ALERT!
• Program segment ◼ Condition in if
statement must be
double mark = sc.nextDouble();
written in a bracket
◼ No semicolon in if
if(mark > 40) line (i.e after the
System.out.print("PASS"); condition)
if(condition)
<statement1>;
else
<statement2>;
end if
if-else statement
• Example 1 • Pseudocode
If a student’s mark is 40 or Start
above, print the message input mark
"PASS". Or else, print the if (mark >= 40)
message "FAIL".
print "PASS"
I: mark
else
P: determine message based
on mark print "FAIL"
O: "PASS" or "FAIL" end if
Stop
if-else statement
• Program segment
if(radius > 0)
{
double area = 3.14 * radius * radius;
System.out.print(area);
}
else
System.out.print("Invalid radius");
Using if-else-if statement
• General format:
if(condition1)
<statement1>;
else if(condition2)
<statement2>;
else
<statement3>;
end if
if-else-if statement
• Example 1 • Pseudocode
Create a program to accept a Begin
number from user, and to determine input number
whether the number is positive, if (number > 0)
negative or zero. print "positive"
I:number else if (number < 0)
P:determine message based on print "negative"
number else
O:"positive" or "negative" or print "zero"
"zero" end if
End
if-else-if statement
• Program segment
if(number > 0)
System.out.println("positive");
else if(number < 0)
System.out.println("negative");
else
System.out.println("zero");
if-else-if statement
• Example 2
The program will be assigned based on
the value of a code. Display the name of • Pseudocode
the program that will be assigned based Begin
on the following table: input code
Code Program if (code = 1)
1 Degree in Multimedia print "Degree in Multimedia"
2 Degree in Networking else if (code = 2)
print "Degree in Networking"
3 Degree in Robotics
else if (code = 3)
print "Degree in Robotics"
I:code end if
P:determine message based on code End
O:"Multi" or "Netw"or "Robo"
if-else-if statement
• Program segment
public static void main(String[] args)
{
int code = sc.nextInt();
if(code==1)
System.out.print("Degree in Multimedia");
else if(code==2)
System.out.print("Degree in Networking");
else if(code==3)
System.out.print("Degree in Robotics");
}
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:
for(int counter=1;counter<=10;counter=counter+1)
{
length=sc.nextDouble();
width=sc.nextDouble();
area = length * width;
System.out.print(area);
}
}
Note:
Counter+1 = Counter++
About for statement i-1 = i--
• for statement combines all the three parts of looping structure in one
statement:
- Practical Test 1
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:
- Practical Test 2
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to: