freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Control Statements in java

This involves a kind of decision making to see whether a particular condition has occurred or not and direct the computer to execute certain statements accordingly.

Java Images

1. The if Statement

It is clearly observed that if block is executed only if the condition following if is true.

if (Condition)
{
Statements; /* true block (or) if block */
}

Java Images

if Program using true

class Test
{
public static void main(String[] args) 
 {
if (true)
   {
System.out.println("freetimestudy");
  }
System.out.println("hi");
 }
}

intput and output

output:freetimestudy,,hi

if else Statement

It is clearly observed that if block is executed only if the condition following if is true. If the condition is false else block is executed.

if (Condition)
{
Statements; /* true block (or) if block */
}
else
{
Statements; /* false block (or) else block */
}

Java Images

if else program:

class Test
{
public static void main(String[] args)
{
int a=10;
int b=20;
if (a>b){
System.out.println("java");
}
else
{
System.out.println("python");
}
System.out.println("hi");
}
}

intput and output

output:python,,hi