3. Chapter #3
Selections
Chapter 1 introduces how to declare Boolean variables, and write Boolean
expressions using relational operators. How to implement selection control using
one-way if statements, two-way if-else statements, nested if and multi-way if
statements and switch statements and how to use logical operators.
5/22/2022 SWE-103T OOP SED,SSUET 3
4. Introduction
• Another way to make programs more user-interactive and flexible
is to make the program able to handle more than one case. This is
done by making use of selection statements.
• Selection Statements: Selection statements allow a program to
test several conditions, and execute instructions based on which
condition is true. That is why selection statements are also
referred to as conditional statements.
5/22/2022 SWE-103T OOP SED,SSUET 4
5. Selection Statements in JAVA
5/22/2022 SWE-103T OOP SED,SSUET 5
• Like all high-level programming languages, Java provides selection
statements. Selection statements use conditions that are Boolean
expressions.
• Boolean expression is an expression that evaluates to a Boolean value:
true or false.
• Boolean Data Type (Already discussed in previous lectures)
• The Boolean data type declares a variable with the value either true or false.
• true and false are literals, just like a number such as 10.
• Boolean Variable A variable that holds a Boolean value is known as a
Boolean variable.
• Example: boolean lightsOn = true;
6. Relational/ Comparison Operators
• Java provides six relational operators (also known as comparison
operators), shown in Table 3.1
5/22/2022 SWE-103T OOP SED,SSUET 6
8. Practice task
• 3.18 Assuming that x is 1, show the result of the following
Boolean expressions.
i. !(x > 0) && (x > 0)
ii. (x > 0) || (x < 0)
iii. (x != 0) || (x == 0)
iv. (x >= 0) || (x < 0)
v. (x != 1) == !(x == 1)
5/22/2022 SWE-103T OOP SED,SSUET 8
9. Example Program1
public class BooleanTest {
public static void main(String[] args) {
int x=1;
int y=x-1;
boolean test= x>y;
System.out.println(" x>y is " + test);
}
}
5/22/2022 SWE-103T OOP SED,SSUET 9
10. Example Program2
public class BooleanTest {
public static void main(String[] args) {
int x=1;
int y=x-1;
boolean test= x<y;
System.out.println(" x<y is " + test);
}
}
5/22/2022 SWE-103T OOP SED,SSUET 10
11. • 3.2 Assuming that x is 1, show the result of the following Boolean expressions:
• (x > 0)
• (x < 0)
• (x != 0)
• (x >= 0)
• (x != 1)
• 3.3 Can the following conversions involving casting be allowed? Write a test program to
verify your answer.
boolean b = true;
i = (int)b;
int i = 1;
boolean b = (boolean)i;
Test Questions
5/22/2022 SWE-103T OOP SED,SSUET 11
12. If Statements
• Java has several types of selection statements: one-way if
statements, two-way if-else statements, nested if statements,
multi-way if-else statements, switch statements, and conditional
expressions.
5/22/2022 SWE-103T OOP SED,SSUET 12
One-Way if Statement
• A one-way if statement executes an action if and
only if the condition is true. The syntax for a one-
way if statement is:
if (boolean-expression) {
statement(s);
}
13. Example
• Write a Java program That takes an integer as input and test it, if
the input is positive, print that input is positive. Otherwise quite
the program.
5/22/2022 SWE-103T OOP SED,SSUET 13
14. Example 2
• Calculate the area of circle, if and only if the radius is greater than
or equal to 0.
5/22/2022 SWE-103T OOP SED,SSUET 14
15. Example 3 SimpleIfDemo.java
• Gives a program that prompts the user to enter an integer. If the number is a
multiple of 5, the program displays HiFive. If the number is divisible by 2, it
displays HiEven.
5/22/2022 SWE-103T OOP SED,SSUET 15
16. Practice tasks
• 3.4 Write an if statement that assigns 1 to x if y is greater than 0.
• 3.5 Write an if statement that increases pay by 3% if score is
greater than 90.
5/22/2022 SWE-103T OOP SED,SSUET 16
17. Two-Way if-else Statements
• A one-way if statement performs an action if the specified
condition is true. If the condition is false, nothing is done. But by
using a two-way if-else statement we can work with false
statements as well. Following is the syntax for a two-way if-else
statement:
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
5/22/2022 SWE-103T OOP SED,SSUET 17
18. Example 1
• Write a Java program that takes an integer as input and test it, if
the input is positive, print that “the input is positive”, otherwise
print that “the input is negative”.
5/22/2022 SWE-103T OOP SED,SSUET 18
19. Example 2
• Write a program to check whether the input is even or odd.
5/22/2022 SWE-103T OOP SED,SSUET 19
20. Practice Task
• 1. Write a program to check the attendance of an student. If the
attendance is greater than 75% print a message that you are
allowed other wise print that you are not eligible for exam.
• 2. Write an if statement that increases pay by 3% if KPI score is
greater than 90, otherwise increases pay by 1%.
5/22/2022 SWE-103T OOP SED,SSUET 20
21. Nested if and Multi-Way if-else Statements
• An if statement can be inside another if statement to form a nested
if statement.
• The statement in an if or if-else statement can be any legal Java
statement, including another if or if-else statement.
• The inner if statement is said to be nested inside the outer if
statement. The inner if statement can contain another if
statement;
• In fact, there is no limit to the depth of the nesting.
5/22/2022 SWE-103T OOP SED,SSUET 21
22. Example 1
• IMF passed a resolution to charge income tax if the salary is
greater than 50000. Write a program to calculate the tax amount
as per the given slabs
5/22/2022 SWE-103T OOP SED,SSUET 22
Salary Income Tax Ratio
50000-62000 5%
62001-79000 10%
79001-104000 20%
104001-1000000 30%
24. Example 2
• Write a java program to grant the scholarship to an student.
Student will be granted scholarship in fulfilling the following
conditions. Otherwise he/she wont be eligible for the scholarship.
i. If he/she had 75% or more attendance during semester and he/she
scored 3 gpa or above in exam.
ii. He/She must be the student of either 3rd, 4th, 5th or 6th semester.
5/22/2022 SWE-103T OOP SED,SSUET 24
26. Practice Task
• Write the gpa Assignment code on basis of following table:
5/22/2022 SWE-103T OOP SED,SSUET 26
4 A
3.66 A-
3.33 B+
3 B
2.66 B-
2.33 C+
2 C
1.66 C-
1.3 D+
1 D
0 F
27. Common Errors and Pitfalls
• Common Error 1: Forgetting Necessary Braces
• Common Error 2: Wrong Semicolon at the if Line
• Common Error 3: Redundant Testing of Boolean Values
• Common Error 4: Dangling else Ambiguity
• Common Error 5: Equality Test of Two Floating-Point Values
5/22/2022 SWE-103T OOP SED,SSUET 27
28. Generating Random Numbers
• Math.random( ) can be used to obtain a random double value
between 0.0 and 1.0, excluding 1.0.
• The number can be made integer by writing:
int Variable = (int)(Math.random() * 10);
5/22/2022 SWE-103T OOP SED,SSUET 28
29. switch Statements
• The main reasons for using a switch include improving clarity, by
reducing otherwise repetitive coding, and (if the heuristics
permit) also offering the potential for faster execution through
easier compiler optimization in many cases.
• A switch statement executes statements based on the value of a
variable or an expression.
• Switch works as:
• 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 and to escape from switch.
5/22/2022 SWE-103T OOP SED,SSUET 29
30. Switch Syntax
• Following is the full syntax for the switch statement:
switch (switch-expression) {
case value1: statement(s)1;
break;
case value2: statement(s)2;
break;
...
case valueN: statement(s)N;
break;
default: statement(s)-for-default;
}
5/22/2022 SWE-103T OOP SED,SSUET 30