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");
            }
}
}
OUTPUT:-
x and y are equal
OUTPUT:-
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
{
else
{
Statement2 ;
}
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 
Nested if else 
 
3) Nested If Else: -
If condition one is false, then condition two is checked, if
condition 2 is false, then else part is executed
(In Nested If Else statement two,
three or more conditions are apply) 
Syntax:-
if (Test condition1)
{                  
Statement1; 
}
else_if(condition2)
{
else_if(condition2)
{
Statement2; 
} 
else
else
{
Statement 3;
}
Example:-
/*Write a program to find greater of three number using nested if else */
import
java.util.Scanner;
class nested1
{
public static void main(String args[]) 
{                                                                      
Scanner in  = new Scanner(System.in);
            int x,y,z;
            System.out.println("enter
the three number ");
            x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if(x>y && x>z)
            {
                        System.out.println("x
is greater");
            }
else if(y>x && y>z)
            {
                        System.out.println("y
is greater");
            }
else if(z>x && z>y)
            {
                        System.out.println("z
is greater");
            }
else
            {
                        System.out.println("all
are equal");
            }                                                                                                
}
}
 OUTPUT


 
No comments:
Post a Comment