Decision Making Statement
Decision Making Statement in Java
Decision making statement statements is also called selection statement. That is depending on the condition block need to be executed or not which is decided by condition. If the condition is "true" statement block will be executed, if condition is "false" then statement block will not be executed. In java there are three types of decision making statement.
- if
- if-else
- switch
if-then Statement
if-then is most basic statement of Decision making statement. It tells to program to execute a certain part of code only if particular condition is true.
Syntax
if(condition) { Statement(s) }
Example if statement
class Hello { int a=10; public static void main(String[] args) { if(a<15) { System.out.println("Hello good morning!"); } } }
Output
Hello good morning
if-else statement
In general it can be used to execute one block of statement among two blocks, in java language if and else are the keyword in java.
Syntax
if(condition) { Statement(s) } else { Statement(s) } ........
In the above syntax whenever condition is true all the if block statement are executed, remaining statement of the program by neglecting. If the condition is false else block statement executed and neglecting if block statements.
Example if else
import java.util.Scanner; class Oddeven { public static void main(String[] args) { int no; Scanner s=new Scanner(System.in); System.out.println("Enter any number :"); no=s.nextInt(); if(no%2==0) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }
Output
Enter any number : 10 Even number
Switch Statement
The switch statement in java language is used to execute the code from multiple conditions or case. It is same like if else-if ladder statement.
A switch statement work with byte, short, char and int primitive data type, it also works with enumerated types and string.
Syntax
switch(expression/variable) { case value: //statements // any number of case statements break; //optional default: //optional //statements }
Rules for apply switch statement
With switch statement use only byte, short, int, char data type (float data type is not allowed). You can use any number of case statements within a switch. Value for a case must be same as the variable in switch.
Limitations of switch statement
Logical operators cannot be used with switch statement. For instance
Example
case k>=20: // not allowed
Example of switch case
import java.util.*; class switchCase { public static void main(String arg[]) { int ch; System.out.println("Enter any number (1 to 7) :"); Scanner s=new Scanner(System.in); ch=s.nextInt(); switch(ch) { case 1: System.out.println("Today is Monday"); break; case 2: System.out.println("Today is Tuesday"); break; case 3: System.out.println("Today is Wednesday"); break; case 4: System.out.println("Today is Thursday"); break; case 5: System.out.println("Today is Friday"); break; case 6: System.out.println("Today is Saturday"); break; case 7: System.out.println("Today is Sunday"); default: System.out.println("Only enter value 1 to 7"); } } }
Output
Enter any number (1 to 7) : 5 Today is Friday