Control structure in java

The statement that is used to transfer the control at various part of the program is called as a control structure.

There is three control structure in java: –
1) Selection
2) Iteration
3) Switch case

Selection:- Control structure that is used to select an option from the multiple is called a selection statement. It is also called as branching it is used to provide multiple paths in the program.

There is following selection statement: –

a) If
b) If else
c) If else if
d) Nested if
e) Logical operator
f) Conditional operator

a) If:- It is used to check the condition and perform the task when the condition is true otherwise not.

if statement

 

 

 

 

 

 

 

Example:-

Check the numbers is even.

Class selection1
{
Public static void main (string args [])
{
Int n = 6;
If (n ℅ 2 = = 0)
{
System.out.println (“Even number “);
}
}
}

b) If else:- It is used to check the condition and perform the task when the condition is true as well as false.

if else statement

 

 

 

 

 

 

 

 

 

 

Example:-

Check the numbers is even.

Class selection2

{
Public static void main (String [] args)
{
Int n=6;
If (n%2==0)
{
System.out.println (“Even number”);
}
Else
{
System.out.println (“Odd number”);
}
}
}

c) If else if :- It is used to check the multiple condition one by one such that only one condition or statement will be execute at a time. It is also called as ladder if.

if else if

 

 

 

 

 

 

 

 

 

 

 

 

 

Example: –

Class selection
{
Public static void main (String [] args)
{
Int n=6;
If (n>0)
{
System.out.println (“Positive number”);
}
Else if (n<0)
{
System.out.println (“Negative number”);
}
Else
{
System.out.println (“Odd number”);
}
}
}

d) Nested if:- If written in side another if is called as nested if. Any number of if or if else can be written within the if and else part.

Example:-

Find the greatest among three numbers.

Class selection
{
Public static void main (String [] args)
{
Int a=9, b=6, c=2;
If (a>b)
{
If (a>c)
{
System.out.println (“a is greater”);
}
Else
{
System.out.println (“c is greater”);
}
}
Else
{
If (b>c)
{
System.out.println (” b is greater”);
}
Else
{
System.out.println (“c is greater”);
}
}
}
}

e) Use of logical operator:- Operator that is used to combine multiple conditions together is called as logical operator. It can be used with any format of if.

Example:-

Find the greatest among three numbers.

Class selection
{
Public static void main (String [] args)
{
Int a=9, b=6, c=2;
If (a>b && a>c)
{
System.out.println (“a is greater”);
}
Else if (b>a && b>c)
{
System.out.println (“b is greater”);
}
Else
{
System.out.println(“c is greater”);
}
}
}

f) Conditional operator (? : ) :- Operator that is used to check the condition and consider both the true or false part. It is replacement of if else. It is ternary a nature that is used to three statement to perform the task.

Syntax: –

a) Condition? Statement 1: Statement 2
Here the condition is checked and if it is true then statement 1 is executed otherwise statement 2 will be executed.

Example: –

Check the numbers is even or odd.

class selection
{
public static void main(String[] args)
{
int n=6;
n%2==0 ? System.out.println(“Even number”) : System.out.println(“Odd number”);
}
}

b) Variable = Condition? Statement 1 : Statement 2
Here the condition is checked and if it is true then result of statement 1 is store in the variable otherwise result of statement 2 is store in the variable.

Example: –

Convert negative into positive.

class selection
{
public static void main(String[] args)
{
int n=6;
n=n>=0 ? n:n*-1;
System.out.println(“Result =”+n);
}
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.