if…else Statement

if…else Statement

This Statement is used to perform a task depending on whether a given condition is true or false.

Syntax
if (condition)
{
//statement-if condition is true;
}
else
{
//statement-if condition is false;
}
Programs
To find leap year or not
public class test
{
public static void main(String[] args)
{
int yr = 2014;
if((yr % 400 == 0) || ((yr % 4 == 0) && (yr % 100 != 0)))
System.out.println(“Year ” + yr + ” is a leap year”);
else
{
System.out.println( yr + ” is not a leap year”);
}
}
}

Output:
2014 is not a leap year

To find even or odd number
public class test
{
public static void main(String[] args)
{
int a=50;
if((a%2)==0)
{
System.out.println(a+”is even number”);
}
else
{
System.out.println(a+”is odd number”);
}
}
}

Output:
50 is even number

Leave a Reply

Your email address will not be published. Required fields are marked *