Flow control refers to the process of directing the sequence of execution of statements in a program. In Java, there are several flow control statements that allow you to control the flow of execution in your program. The main flow control statements in Java are:
-
Conditional statements:
if
,else if
, andelse
statements are used to execute a block of code based on a certain condition. - Switch statement:
switch
statement is used to execute one of several possible blocks of code, based on the value of a single expression. -
Looping statements:
for
,while
, anddo-while
statements are used to repeat a block of code a certain number of times or until a certain condition is met. -
Jump statements:
break
,continue
, andreturn
statements are used to alter the normal flow of execution in a program.
Here’s an example program that demonstrates the use of these flow control statements:
public class FlowControlExample
{
public static void main(String[] args)
{
int x = 5;
if (x > 10)
{
System.out.println("x is greater than 10");
}
else if (x > 5)
{
System.out.println("x is greater than 5 but less than or equal to 10");
}
else
{
System.out.println("x is less than or equal to 5");
}
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue;
}
if (i == 8)
{
break;
}
System.out.println(i);
}
int i = 1;
while (i <= 5)
{
System.out.println(i);
i++;
}
int j = 1;
do
{
System.out.println(j);
j++;
}
while (j <= 5);
int score = 80;
switch (score)
{
case 90:
System.out.println("Grade A");
break;
case 80:
System.out.println("Grade B");
break;
case 70:
System.out.println("Grade C");
break;
default:
System.out.println("Grade D");
}
}
}
This program demonstrates how the different flow control statements work in Java. Depending on the value of x
, the program uses conditional statements to execute different blocks of code. The program also uses looping statements to repeat blocks of code a certain number of times or until a certain condition is met. Finally, the program uses a switch
statement to execute one of several possible blocks of code, based on the value of a single expression.
Entry control loop vs Exit control loop
Here’s a table summarizing the differences between entry-controlled loops and exit-controlled loops:
Entry-controlled loop | Exit-controlled loop | |
---|---|---|
Definition | A loop where the test condition is checked before entering the loop body. | A loop where the test condition is checked after executing the loop body. |
Control | The loop will not execute if the test condition is false before entering the loop body. | The loop will execute at least once before checking the test condition. |
Syntax | while (testCondition) { // loop body } |
do { // loop body } while (testCondition); |
Examples | while (i < 10) { // loop body } |
do { // loop body } while (i < 10); |
Advantages | Test condition is evaluated first, which can save time if the condition is false. | Loop body is guaranteed to execute at least once, which can be useful in some scenarios. |
Disadvantages | Loop body may not execute at all if the test condition is false initially. | Test condition is evaluated after executing the loop body, which can lead to unnecessary iterations. |