COS202 Lecture 1
COS202 Lecture 1
LECTURE 1
We can use these conditions to perform different actions for different decisions. Java has the
following conditional statements:
● Use if to specify a block of code to be executed, if a specified condition is true
● Use else to specify a block of code to be executed if the same condition is false
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed
The if Statement
Use the if statement to specify a block of Java code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error because Java
is case-sensitive.
Example 1.1
1 public class Main {
2 public static void main(String[] args) {
3 int cos201PassMark = 45;
4 int totalScoreInCOS201 = 56;
5
6 if (totalScoreInCOS201 > cos201PassMark) {
7 System.out.println(“You are welcome to COS202 Class”);
8 }
9 }
10 }
In the example above, we use two variables to test whether totalScoreInCOS201 is greater than
cos201PassMark (using the > operator). As totalScoreInCOS201 is 56, and cos201PassMark
is 45, and we know that 56 is greater than 45, we print to the screen that “You are welcome to
COS202 Class”.
Note that using the else statement only (without the if statement) would generate a syntax error.
The if statement must precede the else statement and not vice versa.
Example 1.2
1 public class Main {
2 public static void main(String[] args) {
3 int cos201PassMark = 45;
4 int totalScoreInCOS201 = 38;
5
6 if (totalScoreInCOS201 > cos201PassMark) {
7 System.out.println(“You are welcome to COS202 Class”);
8 }
9 else {
10 System.out.println(“Sorry, go back to COS201 Class”);
11 }
12 }
13 }
There is also a short-hand if… else, which is known as the ternary operator because it consists
of three operands. It can be used to replace multiple lines of code with a single line, and is most
often used to replace simple if… else statements
Example 1.3
1 public class Main {
2 public static void main(String[] args) {
3 int votingAge = 18;
4 int candidateAge = 21;
5
6 String result = (candidateAge > votingAge) ? “Eligible to vote” : “Sorry, ineligible to vote”;
7 System.out.println(result);
8
9 }
10 }
Example 1.4
1 public class Main {
2 public static void main(String[] args) {
3 int tuna = 5;
4
5 if (tuna > 0) {
6 System.out.println(tuna + “is a positive integer”);
7 }
8 else if (tuna < 0) {
9 System.out.println(tuna + “is a negative integer”);
10 } else {
11 System.out.println(tuna + “is zero”);
12 }
13 }
14 }
Switch Statement
You can use the switch statement instead of writing many if..else statements. The switch
statement selects one of many code blocks to be executed
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example 1.5
1 public class Main {
2 public static void main(String[] args) {
3 int day = 4;
4 switch (day) {
5 case 1:
6 System.out.println("Sunday");
7 break;
8 case 2:
9 System.out.println("Monday");
10 break;
11 case 3:
12 System.out.println("Tuesday");
13 break;
14 case 4:
15 System.out.println("Wednesday");
16 break;
17 case 5:
18 System.out.println("Thursday");
19 break;
20 case 6:
21 System.out.println("Friday");
22 break;
23 case 7:
24 System.out.println("Saturday");
25 break;
26 default:
27 System.out.println("This is not a day of the week");
26 }
27 }
28 }
CLASS WORK
Mini Project: Simple Grading System
Task: Develop a Java program that takes a student's score as input and determines their grade
using if-else or switch-case statements.