Basic leadership Statement in Java
Basic leadership proclamation explanations is likewise called choice articulation. That is relying upon the condition square should be executed or not which is chosen by condition. On the off chance that the condition is "genuine" explanation square will be executed, if condition is "false" then proclamation piece won't be executed. In java there are three sorts of basic leadership articulation.
- if
- if-else
- switch
if-then Statement
on the off chance that then is most fundamental proclamation of Decision making explanation. It advises to program to execute a specific piece of code just if specific condition is valid.
Syntax
if(condition) { Statement(s) }
Example if statement
class Hello { int a=10; public static void main(String[] args) { if(a<15 ello="" good="" morning="" pre="" system.out.println="">Output
Hello good morningif-else statement
When all is said in done it can be utilized to execute one square of articulation among two pieces, in java dialect if and else are the catchphrase in java.Syntax
if(condition) { Statement(s) } else { Statement(s) } ........In the above grammar at whatever point condition is genuine all the if piece proclamation are executed, outstanding explanation of the system by dismissing. On the off chance that the condition is false else piece explanation executed and disregarding if square proclamations.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 numberSwitch Statement
The switch proclamation in java dialect is utilized to execute the code from numerous conditions or case. It is same like if else-if stepping stool explanation.A switch proclamation work with byte, short, burn and int primitive information sort, it additionally works with specified sorts and string.Syntax
switch(expression/variable) { case value: //statements // any number of case statements break; //optional default: //optional //statements }Rules for apply switch proclamationWith switch explanation utilize just byte, short, int, burn information sort (glide information sort is not permitted). You can utilize any number of case articulations inside a switch. Esteem for a case must be same as the variable in switch.Confinements of switch articulationSensible administrators can't be utilized with switch articulation. Case in pointExample
case k>=20: // not allowedExample 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"); } } }OutputEnter any number (1 to 7) : 5 Today is Friday15>