BCA / B.Tech 12 min read

What is a Control Statement (structure)

In programming languages, statements or structures used to control the flow of a program's execution are called Control Statements (or structures).

Types of Control Statements:

  • Conditional statements
  • Loop statements

1. Conditional Statements

Conditional statements are used to check specific conditions and make decisions based on them. They are of the following types:

(a) if statement

The if statement is used to test a specific condition. It executes once when the condition is true.

if (condition) {
    statements;
}

(b) if-else statements

The if-else statement is used to test a specific condition. If the condition is true, the statements in the if block are executed; otherwise, the statements in the else block are executed.

if (condition) {
    statement;
} else {
    statement;
}

(c) Switch statement

The switch statement is a multi-way branch statement that defines different paths for program execution. It is an alternative to the if-else statement.

switch (variable) {
    case constant 1:
        statements;
        break;
    case constant 2:
        statements;
        break;
    case constant 3:
        statements;
        break;
    default:
        statements;
}

2. Loop statements

Loop statements are used to execute a single statement or multiple statements more than once until a condition is met.

Types of loop statements:

  • while loop
  • do-while loop
  • for loop

1. while loop

The while loop is an entry-controlled loop. In this, statements are continuously executed as long as the condition is true. It first checks the condition and then executes the statements.

while (condition) {
    statement1;
    statement2;
}

2. do-while loop

The do-while loop is an exit-controlled loop. It is similar to the while loop, but in this, the statements are executed first, and then the condition is checked. This ensures that the loop is executed at least once.

do {
    statement1;
    statement2;
} while (condition);

3. For Loop

The for loop is an entry-controlled loop. In this, statements are executed as long as the condition is true. It consists of three components: initialization, condition, and increment/decrement.

for (initialization; condition; increment/decrement) {
    statement1;
    statement2;
}

Other Statements:

Besides these, there are some other statements as follows:

1. Break statement

The break statement is used to terminate the sequence in a switch statement and to exit a loop immediately.

break;

2. Continue statement

The continue statement is used when we want to continue with the next iteration of the loop and skip the other statements of the current iteration.

continue;