Decision making in java
If…else
statement format
If(condition)
{
If condition is true execute
this part
}
else
{
If condition is false then
execute this part
}
Example
class student
{
public static void
main(String [] args)
{
int marks=55;
if(marks>35)
{
System.out.println("student
is pass");
}
else
{
System.out.println("student
is fail");
}
}
}
Example we take above is very
simple example of if else
We declare class A
Declare main method
public static void
main(String [] args)
initialize variable marks=55;
if(marks>35)
we declare marks=55;
marks>35
hence condition is true and
body of if is executed
and we get output
student is pass
now initialize marks=34(less
than 35)
save the program
compile again
now run the program
here condition in if is false
hence body of else is
executed and
we get output
student is fail
No comments:
Post a Comment