Chapter (3)
Controls Statements
Selection Statements
Java supports two selection statements: if and switch. These statements allow to control the flow of
program’s execution based upon conditions known only during run time.
Relational Operators
Logical Operators
Operator Result
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
The if Statement
Use the if statement to specify a block of Java code to be executed if a condition is true
if (condition) {
// block of code to be executed if the condition is true
}
The else Statement
Use the else statement to specify a block of code to be executed if the condition is false
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
}
Example-1
class IfStatement {
public static void main(String[] args) {
int x = 20; int y = 18;
if (x > y) {
System.out.println(x+" is greater than "+y);
}
else {
System.out.println(y+" is greater than "+x);
}
}
}
The if-else-if Statement
A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder.
Basic Software Engineering Ch3-1
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
Example-2
class IfElse {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
The Switch Statement
The switch statement is a multi-way branch statement. When match is found, the Statements
associated with the case value is executed until the brake statement. The default statement is
executed, if no matches are found.
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
default:
// default statement sequence
}
Example-3
class SampleSwitch {
public static void main(String args[]) {
int month = 8; String monthString;
switch(month) {
case 1: monthString = "January"; break;
case 2: monthString = "February"; break;
Basic Software Engineering Ch 3-2
case 3: monthString = "March"; break;
case 4: monthString = "April"; break;
case 5: monthString = "May"; break;
case 6: monthString = "June"; break;
case 7: monthString = "July"; break;
case 8: monthString = "August"; break;
case 9: monthString = "September"; break;
case 10: monthString = "October"; break;
case 11: monthString = "November"; break;
case 12: monthString = "December"; break;
default: monthString = "Invalid month"; break;
}
System.out.println(monthString);
}
}
Example-4
class SampleSwitch {
public static void main(String args[]) {
int day = 4;
switch (day)
{ case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
case 4: System.out.println("Thursday"); break;
case 5: System.out.println("Friday"); break;
case 6: System.out.println("Saturday"); break;
case 7: System.out.println("Sunday"); break;
}
}
}
Exercise1. Write a program to read the marks of three subjects. Then define the result of the
student as follow:
marks>=80 ..marks<=100 Distinction
65 >=marks ..mark <80 Pass with Credit
40>=marks ..mark <65 Pass
marks<=40 ..mark >=0 Fail
Exercise2. Write a Java program to calculate total price for total books and unit price. If total cost in
more than 5000, say the message. “You will get discount.” Discount amount in fixed to 700 and
display the next cost. (totalbooks= 10, unitprice=1000)
Exercise3. The electricity accounts for each resident in a town are calculated as follows. If meter
units used <=500, the cost is 2 kyats 1 unit. If meter units >500 and <=1000, the 4 kyats for first
500 units and 2 kyats for next extra units. In addition, the service fees 5 kyats for each resident no
matter on the meter units used. Calculate total kyats for resident.( units=1100)
Exercise4. Write a calculator program following given two numbers and operator using switch case
statement. ( firstno=10, secondno=5, operator=’*’) // operator is + or – or / or * or other
Basic Software Engineering Ch 3-3