In: Computer Science
Which are the three control structures used in java programming. Which of these three control structures is used in every program or application
There are three types of control structures in java
1. Sequential:
In this control structure, Statements are executed one after another without missing any statement in between. This is the basic control structure of Java programming.
2. Conditional
In this control structure, there are different blocks of statements and a condition decides, whether a block should be executed or not. If a condition is met, then the block of statements are executed, if not met, then the whole block is ignored.
eg. if , if-else, switch etc.
3. Iterative or repetitons
In this control structure , a block of statements are called multiple times again and again till a specific condition is getting satisfied. In this type of control structure, a condition is checked multiple times, and the block is executed again and again till the condition is true.
eg. for, do-while, while etc
Below is the example of code to demonstrate each of the control structure
public class ControlStructure {
public static void main(String[] args) {
// Sequential Control Structure starts
int a = 5;
int b = 10;
int c = a + b;
System.out.println("Sum is " + c);
// Sequential Control Structure ends
// Conditional Control Structure Starts
if (c > 0) {
System.out.println("Value of C is greater than 0");
} else {
System.out.println("Value of C is less than 0");
}
// Conditional Control Structure ends
// Iterative Control Structure starts
for (int i = 0; i < 5; i++) {
System.out.println("Calling multiple times...");
}
// Iterative Control Structure ends
}
}
Output
As you can see in above code,
First four statements are executed one by one sequentially So they fall under first category i.e. sequential control structure.
Next we are checking a condition if c > 0, then we are printing that value of c is greater than 0, else we are printing that value of c is less than zero. So here it depends on value of c that which statement is executed. This is conditional control structure.
Next there is a loop which runs from i = 0 to i < 5 , so it runs for i = 0,1,2,3,4 i.e. 5 times and each time it prints one line which is Calling multiple times.... So a statement is getting repeated mutliple times. And it stops when i < 5 condition is not met. This is iterative control structure or repetitons
Out of these 3, Sequential Control structure is used in every program, becase in every program, there are series of instructure which are executed one by one. So Sequential control structure exists in every program.