Operators
Operators
~ 0101
• if
• if-else
• if-else-if ladder
• nested if
• switch
• Java’s program control statements can be put into
the following categories:
– Selection
• Selection statements allow your program to choose
different paths of execution based upon the outcome of an
expression or the state of a variable.
– Iteration
• Iteration statements enable program execution to repeat
one or more statements (that is, iteration statements form
loops).
– jump
• Jump statements allow your program to execute in a
nonlinear fashion.
Control Statements
• The if Statement : The Java if
statement tests the condition.
• It executes the if block if
condition is true.
if(condition) {
// statement;
}
Syntax:
if(condition){
//code if condition is true
}
else{
//code if condition is false
}
//It is a program of odd and even number.
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
//It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.
if(marks<35){
System.out.println("fail");
}
else if(marks>=35 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}
//Find out given no is +ve or –ve or zero
if(number>0){
System.out.println("POSITIVE");
}else if(number<0){
System.out.println("NEGATIVE");
}else{
System.out.println("ZERO");
}
}
}
Nested if
• The nested if statement represents
the if block within another if block.
• Here, the inner if block condition
executes only when outer if block
condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
//Java Program to demonstrate the use of Nested If Statement.
default:
code to be executed if all cases are
not matched;
}
public class SwitchExample {
public static void main(String[] args) {
int number=2;
//Switch expression
switch(number){
//Case statements
case 1: System.out.println(“ONE");
break;
case 2: System.out.println(“TWO");
break;
case 3: System.out.println(“THREE");
break;
//Default case statement
default:System.out.println("Not in 1, 2 or 3");
}
}
}
case 5: monthString="5 - May";
//Name of the month break;
case 6: monthString="6 - June";
public class SwitchMonthExample { break;
public static void main(String[] args) { case 7: monthString="7 - July";
//Specifying month number break;
int month=7; case 8: monthString="8 - August";
String monthString=""; break;
case 9: monthString="9 - September";
//Switch statement break;
switch(month){ case 10: monthString="10 - October";
//case statements within the switch block break;
case 11: monthString="11 - November";
case 1: monthString="1 - January"; break;
break; case 12: monthString="12 - December";
case 2: monthString="2 - February"; break;
break;
case 3: monthString="3 - March"; default:System.out.println("Invalid Month!");
break;
case 4: monthString="4 - April"; }
break; //Printing month of the given number
System.out.println(monthString);
}
}
switch w/o break
• It executes all statements after the first match, if a
break statement is not present.
//without break statement
public class SwitchExample2 {
public static void main(String[] args) {
int num=2;
//switch expression with int value
switch(num){
//switch cases without break statements
Output:
case 1: System.out.println(“1");
2
case 2: System.out.println(“2"); 3
case 3: System.out.println(“3"); Not in 1, 2 or 3
default:System.out.println("Not in 1, 2 or 3");
}
}
}
// In a switch, break statements are optional.
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1: OUTPUT:
case 2: i is less than 5
case 3: i is less than 5
case 4: i is less than 5
System.out.println("i is less than 5"); i is less than 5
break; i is less than 5
case 5: i is less than 10
case 6: i is less than 10
case 7: i is less than 10
case 8: i is less than 10
case 9: i is less than 10
System.out.println("i is less than 10"); i is 10 or more
break; i is 10 or more
default:
System.out.println("i is 10 or more");
}
}
}
Nested Switch Statement
• We can use switch statement inside other switch
statement in Java.
• It is known as nested switch statement.
switch(expression1){
case value1:
//code to be executed;
break;
case value2:
switch(expression2){
case value21:
//code to be executed;
break; //optional
case value22:
break; //optional
default:
default code to be executed
}
break;
default:
code to be executed if all cases are not matched;
}
Example: -
You are searching for a department in a university and you’re asked to select a
school from a choice of three schools namely:
1. School of Arts
2. School of Business
3. School of Engineering
Having selected a school you are again provided with a list of departments that fall
under the department namely:
1. School of Arts
A. Department of finearts
B. Department of sketcharts
2. School of Business
A. Department of Commerce
B. Department of purchasing
3. School of Engineering
A. CSE
B. ECE
The initial choices for Computer Science, Business and Engineering schools would
be inside as a set of switch statements. Then various departments would then be
listed within inner switch statements beneath their respective schools.
import java.io.*;
import java.util.Scanner;
class NestedExample
{
public static void main(String args[])
{
int a,b;
System.out.println("1.School of Arts\n");
System.out.println("2.School of Business\n");
System.out.println("3.School of Engineering\n");
System.out.println("make your selection\n");
Scanner sobj = new Scanner(System.in);
a=sobj.nextInt();
switch (a)
{
case 1:
System.out.println("You chose Arts\n" ); break;
case 2:
System.out.println("You chose Business\n" ); break;
case 3:
// Engineering
System.out.println("You chose Engineering\n" );
System.out.println("1.CSE\n" );
System.out.println("2.ECE\n" );
System.out.println("make your selection\n");
b=sobj.nextInt();
switch(b)
{
case 1:
System.out.println("You chose CSE\n" );
break;
case 2:
System.out.println("You chose ECE" );
break;
}
break;
}
}
}
enum in Switch Statement
• Java allows us to use enum in switch statement.
• An enum is a special "class" that represents a group
of constants (unchangeable variables, like final variables).
• To create an enum, use the enum keyword (instead of
class or interface), and separate the constants with a
comma.
• Note that they should be in uppercase letters:
enum Level {
LOW, MEDIUM, HIGH
}