CONTROL STATEMENT
In java program statement are executed sequentially one after another in order they appears in a program.
- But some time user want to executing the program under some conditions and also some time we requires to execute some statement several times.
- The control statement allow the user to do above said things.
- In java control statements are (i) Conditions
(iii) Jump
In this blog we will understand only about conditional statement.
CONDITIONAL STATEMENT
The statement which execute under some condition is known as 'conditional statement'.
- It allows selective execution of statement by enabling decisions and subsequent selection of some of several actions.
- Java support the following types of conditional statement i.e.
- Simple if statement.
- if.......else statement.
- if.......else....if statement.
- nested if else statement.
- switch statement.
SIMPLE IF STATEMENT
- In case os simple if statements the statement is executed only when the condition is satisfied or it is true
Syntax
if(condition)
{
statement (s);
}
// s is for denoting more than 1 statement.
FLOW CHART
For EXAMPLE
public class Sample{
public static void main(String args[]){
int a=20, b=30;
if(b>a) System.out.println("b is greater"); } }
if(b>a) System.out.println("b is greater"); } }
If else statement
If else statement check the condition if the condition is satisfied then the first statement is executed otherwise it execute the second statement.
SYNTAX
if(condition)
{
true block statement;
}
else
{
false block statement;
}
FLOW CHART
import java.util.*;
class follwme
{
public static void main(String args[])
{
int Age;
Scanner sc=new Scanner(System.in);
System.out.print("please enter the code : ");
Age=sc.nextInt();
if(Age is <=45)
{
System.out.println("you are able to go to this ride");
}
else
{
System.out.println("you are not able");
}
if.....else....if
In if else if statement,the condition is tested first.If the condition is satisfied then the first statement is executed otherwise it test the second condition is full filled then the second condition is executed otherwise it execut the third statement.
SYNTAX
if(condition)
{
Statement 1;
}
else if (condition)
{
Statement 2;
}
else
{
Statement 3 ;
}
FLOW CHART
FOR EXAMPLE
import java.util.*;
class ifelse
{
public static void main(String args[])
{
int blue=1;
int pink=2;
int green=3;
int yellow=4;
int code;
Scanner sc=new Scanner(System.in);
System.out.print("please enter the code : ");
code=sc.nextInt();
if(code==1)
{
System.out.println("the colour is : blue ");
}
else if(code==2)
{
System.out.println("the colour is : pink");
}
else if(code==3)
{
System.out.println("the colour is : green");
}
else if(code==4)
{
System.out.println("the colour is : yellow");
}
else if(code>4)
System.out.println("colour is not avalaible");
}
}
Nested if else
when a condition is under a condition it is known as nested if, that means a condition contain more than one one condition is called nested if else.
SYNTAX
if(condition)
{
if(condition2)
{
Statement 1;
else
Statement 2;
}
else
{
if (condition3)
{
Statement 3;
else
Statement 4;
}
FOR EXAMPLE
import java.util.*;
class nestedif
{
public static void main()
{
int a=5,b=7,c=4,z;
Scanner ch=new Scanner(System.in);
System.out.print("Enter three number : ");
a=ch.nextInt();
b=ch.nextInt();
c=ch.nextInt();
if (a>b&&a>c)
{
if (b>a&&b>c)
System.out.print( +a+"is greater");
else
System.out.print(+b+" is greater");
}
else
{
if (c>a&&c>b)
System.out.print(+c+" is greater");
}
}
}
0 Comments