Control Structure in JAVA:-
A control structure is a block of a program that analyzes the direction of the program in which to go based on a block.
Programming language provide 5 types of control structure present in java:-
- Sequential control Structure
- Conditional control Structure
- Loop control Structure
- Selection control Structure
- Jump control Structure
-
Sequential control Structure
- If no control statement used in java program, then the control flow in sequential.
- In case of sequential control structure each and every statement execute for a single time only.
-
Conditional control Structure
- Replacement of conditional and ternary operator.
- The conditional statement is various type:-
If
If………else
Nested if
Multiple if
If……else Statement Example
class DisplayArgs { public static void main(String[] args) { int length=args.length; if(args.length>0) { for(int i=0;i>length;i++) { System.out.println(args[i]); } } else { System.out.println("no arguments enter from commandline :"); } } } output no arguments enter from commandline :
Nested If Example
class Greatest { public static void main(String[] args) { float no1=Float.parseFloat (args[0]); float no2=Float.parseFloat (args[1]); float no3=Float.parseFloat (args[2]); if(no1>no2 && no1>no3) { System.out.println("no1 is greatset"); } else if(no2>no3) { System.out.println("no2 is greatset"); } else { System.out.println("no3 is greatest"); } } } Output No1 is greatest
Multiple If Example
class Test { public static void main(String[] args) { char ch=65; char ch1='A'; if(ch==ch1) System.out.println("true"); else System.out.println("flase"); String text="JAVA is simple ,1.8 is the latset version"; int digit=0,let=0,upper=0 ,lower=0,space=0; { char letter=text.charAt(1); if(Character.isDigit(letter)); digit++; if(Character.isLetter(letter)); letter++; if(Character.isUpperCase(letter)); upper++; if(Character.isLowerCase(letter)); lower++; if(Character.isWhitespace(letter)); space++; } System.out.println("text"); System.out.println("No of Letter :"+let); System.out.println("No of UpperCse :"+upper); System.out.println("No of LowerCase :"+lower); System.out.println("No of space :"+space); } }
Output
true
text
No of Letter :0
No of UpperCase: 1
No of LowerCase: 1
No of space: 1
-
Loop control Structure
A loop statement allows executing a group of statements in multiple times .jav support 3 types of loop control structure.
For loop
While loop
Do….while loop
For …..each
For loop
For loop is an entry control loop. In another word, it called as know-time loop or program control loop.
For loop is suitable for:-
- Start position
- Step position
- Stop position
Initialization step executes for a single time but or other parts execute much time.
Example
class Pyramid { public static void main(String[] args) { int row=Integer.parseInt(args[0]); for (int i=0;i<row;i++) { for(int j=0;j<row -i;j++) { System.out.print(" "); } for(int k=0;k<=i;k++) { System.out.print(" " +k); } System.out.println(); } } } Output 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4
While loop
It is an entry control loop and another word it is called as program control loop.
A number of time loop will execute decide by the program.
Example
class Test { public static void main(String[] args) { int i=1; while(i<10) { System.out.println(i); i++; } } } Output 1 2 3 4 5 6 7 8 9
Do…..while loop
It is an exit control loop. Do…..while loop at least executes at one time. It checks the loop at the time.
Finally, block used in java replacement of do….while loop. Finally block is much more powerful than do….while loop.
Example
class Test { public static void main(String[] args) { int i=1; do { System.out.println(i); i++; } while(i<10); } } Output 1 2 3 4 5 6 7 8 9
For each loop
- For each loop can be used in the Array, collection framework, and va-list.
- It only supports forward direction traverse.
- Initialization, condition checking, and updating is the job of java compiler. It only required to source and destination.
Source can be
- Array
- Collection cases
- Va-list
- Collection interfaces
Destination can be
- Primitive data types of class.
Example
class Test
{
public static void main(String[] args)
{
int x[]={1,2,3,4,5};
System.out.println("traverse by for loop");
for (int i=0;i<x.length;i++)
{
System.out.println(x[i]);
}
System.out.println("traverse by for each loop");
int a=0;
for(int b:x)
{
a+=b;
System.out.println(b);
}
}
}
Output:-
traverse by for loop
1
2
3
4
5
traverse by for each loop
1
2
3
4
5
Selection control structure
It is based on a switch case. It is suitable for the menu driven program.
It is the replacement of multiple if.
Switch without a case is no error but case without the switch is not allowed.
Case label only can be constant.
Float and double is not allowed for switch expression in case of a label. The duplicate case is not allowed.
Example
public class Test
{
public static void main(String args[])
{
int i=2;
switch(i)
{
case 1:
System.out.println("Case1 ");
case 2:
System.out.println("Case2 ");
case 3:
System.out.println("Case3 ");
case 4:
System.out.println("Case4 ");
default:
System.out.println("Default ");
}
}
}
Output
Case2
Case3
Case4
Default
Jump control structure
In Java “jump” control structure support 3 statement:-
- Break
- Continue
- Return
- Break
Break statement only can be placed in a loop switch.
Break jumps the control outside of the current loop or current switch.
Example
class Test { public static void main(String args[]) { int i = 1; while (i<=10) { System.out.println("\n" + i); i++; if (i==5) { break; } } } }
Output
1
2
3
4
- Continue
Continue only can be used in the loop.
Continue to jump the control of updating part of the loop. The execution starts from the top part of the loop.
Example
class Test { public static void main(String args[]) { for (int i=1; i<10; i++) { if (i%2 == 0) continue; System.out.println("\n" + i); } } } Output 1 3 5 7 9
-
Return
The return statement is compulsory if the return type of function is not void type.
More than one return statement is not allowed in java within a single function.
The return statement immediately terminates the program in which it is executed.
Example
class Test { public static void main(String args[]) { boolean t = true; System.out.println("Before the statement return"); if(t) return; // return the caller System.out.println("The statement is not execute"); } }
Output
Before the statement return