1. Conditional Statement if
Fun point
Assume that, if i want to eat a Biriyani @Biryani Zone.
Condition : Do you have money?
Ans : if Yes go and Eat.
Ans : if No be here and enjoy Hostel Food
- Based on the Yes or No, we are taking the decision for the next step
- Same as in java also, expression will produce either true or false answer.
- based on the true or false ,ll take a step
when should we use if statement?
- Suppose you want to do either one thing or nothing at all then you should go for if statement
syntax:
if (expression)
{
statements;
}
- if statement holds
an expression.
- expression gives
the result either true or false.
- if the result
is true then if block
statements will executes.
- if
the result is false then if
block statements will not executes.
--------------------------------------------------------------------------------
Program : Basic program on if statement
Program name : IfDemo1.java
Output :
value of i==j is :true
inside if block
class IfDemo1
{
public static void main(String args[])
{
int i = 10;
int j = 10;
System.out.println("value of i==j is :"+(i==j));
if(i==j)
{
System.out.println("inside if block");
}
}
}
Compile : javac IfDemo1.java
Run : java IfDemo1
Output :
value of i==j is :true
inside if block
--------------------------------------------------------------------------------
Program : Basic program on if statement
Program name : IfDemo2.java
Output :
value of i==j is :false
class IfDemo2
{
public static void main(String args[])
{
int i = 10;
int j = 20;
System.out.println("value of i==j is :"+(i==j));
if(i==j)
{
System.out.println("inside if block");
}
}
}
Compile : javac IfDemo2.java
Run : java IfDemo2
Output :
value of i==j is :false
--------------------------------------------------------------------------------
Thanks for your time.
- Nireekshan