0% found this document useful (0 votes)
6 views5 pages

COS202 Lecture 1

The document provides an overview of Java conditional statements, including 'if', 'else', 'else if', and 'switch' statements, along with their syntax and examples. It explains how to use these statements to control the flow of a program based on logical conditions. Additionally, it includes a mini project task for developing a simple grading system using these concepts.

Uploaded by

wwwtope947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

COS202 Lecture 1

The document provides an overview of Java conditional statements, including 'if', 'else', 'else if', and 'switch' statements, along with their syntax and examples. It explains how to use these statements to control the flow of a program based on logical conditions. Additionally, it includes a mini project task for developing a simple grading system using these concepts.

Uploaded by

wwwtope947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ACHIEVERS UNIVERSITY, OWO

COLLEGE OF NATURAL AND APPLIED SCIENCES


DEPARTMENT OF COMPUTER SCIENCE
COS 202 – COMPUTER PROGRAMMING II – 3 UNITS

LECTURER IN CHARGE - MR. ADEPOJU, S. E.

LECTURE 1

JAVA CONDITIONAL STATEMENT


Java supports the usual logical conditions from mathematics:
●​ Less than: a < b
●​ Less than or equal to: a <= b
●​ Greater than: a > b
●​ Greater than or equal to: a >= b
●​ Equal to: a == b
●​ Not Equal to: a != b

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”.

The if… else Statement


Use the else statement to specify a block of code to be executed if the condition is false.
Syntax
​ if (condition) {
​ ​ // block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

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​ }

The else if Statement


Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

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
}

●​ The switch expression is evaluated once.


●​ The value of the expression is compared with the values of each case.
●​ If there is a match, the associated block of code is executed.
●​ The break and default keywords are optional.

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​ }

The break Keyword


When Java reaches a break keyword, it breaks out of the switch block. This will stop the
execution of more code and case testing inside the block. When a match is found, and the job is
done, it's time for a break. There is no need for more testing.
A break can save a lot of execution time because it "ignores" the execution of all the rest of the
code in the switch block.

The default Keyword


The default keyword specifies some code to run if there is no case match. Note that if the
default statement is used as the last statement in a switch block, it does not need a break.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy