google Ads

Monday, July 13, 2009

if-else statements

The if-else statements

It is used to control the flow of execution and also used carry out the logical test and ten pickup one of the two possible actions depending on the logical test.
It is used to execute some statements when the condition is true and execute some other statements. When the conditions is false.

Syntax:
if (condition)
{
statements 1;
}
else
{
statements 2;
}
statements 3;

If the conditions is true, then the statements 1 and statements 3 will be executed. The true statements may be a single statements or group of statements. If the condition is false then the statements 1 are not executed but statements 2 and statements 3 will be executed.

Example:
if (a>=10)
{
System.out.println(“A is greater than or equal to 10”);
}
else
{
System.out.println(“A value is less than 10”);

}
System.out.println(“A value is :” +a);

The above example : If a =15 the condition is true output will be proved “A is greater than or equal to 10” .and A Value is : 15

If a = 6the condition is false output will be proved ”A value is less than 10”. A value is : 6

Example:

import java.io.*;
public class biggest
{
public static void main(String args[])
{
int a,b;
a=15; b=24;


if(a>b)
{
System.out.println(“A is biggest number”);
}
else
{
System.out.println(“B is biggest Number”);
}
System.out.println(“A = “ +a +”B = “+ b);
}
}

Output will be

B is biggest Number
A=15 B= 24

No comments:

Post a Comment