Download presentation
Presentation is loading. Please wait.
1
Lecture Notes – Week 2 Lecture-2
Chapters 3 & 4
2
Outline Introduction to selection statements
Boolean data type and Boolean expressions One-way if statements Two-way if-else statements Nested if and multi-way if-else statements Switch statements To do list
3
Checking Input from Keyboard
/* This program reads a radius from keyboard and check whether the input is valid, ...*/ import java.util.Scanner; public class ComputerAreaWithConsoleInput { public static void main(String[] args) { Scanner input = new Scanner(system.in); double radius, area; final double PI = ; System.out.print("Enter a number for radius: "); double radius = input.nextDouble(); //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } We want to have a condition on the input. We use a selection statement to check the condition.
4
One-way if Statements /* This a program fragment from the previous slide … //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } A one-way if statement starts with keyword if, followed by a Boolean expression for the condition, then followed by a statement body. 4
5
One-way if Statements } if (Boolean-expression) { statement(s);
if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } if (Boolean-expression) { statement(s); } 5
6
Relational Operators The examples are Boolean expressions. A Boolean expression consists of a variable or value on each side of a relational operator. The expression is evaluated as true if the relation operator is satisfied; otherwise it is evaluated as false. 6 6
7
Logical Operators The examples are logical expressions. A unary logical expression consists of a logical operator, followed by a Boolean expression or value. A binary logical expression consists of a Boolean expression or value on each side of a logical operator. 7 7 7
8
Two-way if-else Statements
/* This a program fragment from the previous slide … //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } else { System.out.println("Negative input"); If there are two alternative actions to be taken, we use a two-way if-else statement: one action is taken when the Boolean expression is evaluated as true while another is taken otherwise. For each alternative action, we would need a block of statements. 8
9
Two-way if-else Statements
if (boolean-expression) { statement(s); \\first block of statement } else { statement(s); \\second block 9 9
10
Multi-way if-else Statements
/* This a program fragment for a multi-way if-else statement … if (score >= 90.0) System.out.print(”A”); \\ if score >= 90.0 else if (score >= 80.0) System.out.print(”B”); \\ if score < 90.0 and >= 80.0 else if (score >= 70.0) System.out.print(”C”); \\ if score < and >=70.0 else if (score >= 60.0) System.out.print(”D”); \\ if score < 70.0 and >= 60.0 else System.out.println(”F");\\ score < 60.0 This statement has 4 nested if-else statements and 5 alternative actions (5 ways). 10 10
11
Multi-way if-else Statements
else if else if else if else 11 11 11
12
switch Statements switch (switch-expression) {
The switch-expression must yield a value of char, byte, short, or int data type. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. value1, ..., and valueN can be constants only. 12 12 12 12
13
switch Statements The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. 13 13 13 13 13
14
To Do List Before Next Lecture
Read Week 2 lecture slides. Read and run Week 2 program examples. Selectively read those sections in Chapters 3 and 4 that cover the topics in this lecture.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.