if, if else, Nested if else



Decision Control Statements:-

There are three types of decision Control Statements.
1) If                 2) If Else         3) Nested if Else

1) If: -

In these types of statements if condition is true then block of code is executed.

Syntax: -                                                                                        

if (Test Condition)
Statement1; 
}

Example: -

Write a program to find numbers is equal

class IfExample
{
public static void main(String args[])
{                                                                                                                     
            int x = 10, y = 10;
if(x==y)
            {
                        System.out.println("x and y are equal");
            }
}
}

2) If Else: -

 In these types of statements if condition is true then block of code is executed. If condition is false then else part of statement is executed.

Syntax: -

if (Test condition)
Statement1 ;
}
else
{         
Statement2 ;
}

Example 1:-

Write a program to find even or odd

import java.util.Scanner;
class IfElseExample
{
public static void main(String args[])
{
Scanner in  = new Scanner(System.in);
int x;
System.out.println("enter the number ");
            x = in.nextInt();
if(x%2==0)
            {
                        System.out.println("even number");
            }
else
            {
                        System.out.println("odd number");                                                                                                                
            }
}
}
OUTPUT 


No comments:

Post a Comment