What are control statements?

Started by beingchinmay, 01-17-2017, 00:43:53

Previous topic - Next topic

beingchinmayTopic starter

Control statements are useful the way in which the program required to flow. It also increases the readability of the program and also makes the exceution easier.Two types of control statement available in C++ are Branching statement  Loop statement


damponting44

#1
Control statements are programming constructs that control the flow of execution in a program. They determine how different sections of code are executed based on certain conditions or criteria. Common control statements include if-else statements, for loops, while loops, and switch statements. These statements allow programmers to make decisions, repeat code, or choose between different options based on specific conditions. Control statements are essential for creating dynamic and flexible programs.

Here are some specific examples of control statements:

1. If-else statement: This statement allows you to specify a condition and execute different blocks of code based on whether the condition is true or false. For example:

```
if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}
```

2. For loop: This statement allows you to repeat a block of code a specified number of times. For example:

```
for (initialization; condition; increment) {
    // code to repeat
}
```

3. While loop: This statement allows you to repeatedly execute a block of code as long as a certain condition is true. For example:

```
while (condition) {
    // code to repeat
}
```

4. Switch statement: This statement allows you to choose between multiple options based on the value of a variable or expression. For example:

```
switch (variable) {
    case value1:
        // code to execute if variable equals value1
        break;
    case value2:
        // code to execute if variable equals value2
        break;
    default:
        // code to execute if variable does not match any case
}
```

Few more control statements:

1. Nested if-else statement: This is when an if-else statement is nested within another if-else statement. It allows for more complex conditions and multiple branching paths. For example:

```
if (condition1) {
    // code to execute if condition1 is true
    if (condition2) {
        // code to execute if both condition1 and condition2 are true
    } else {
        // code to execute if condition1 is true but condition2 is false
    }
} else {
    // code to execute if condition1 is false
}
```

2. Do-while loop: This statement is similar to the while loop, but it executes the block of code first and then checks the condition. It guarantees that the code inside the loop will execute at least once. For example:

```
do {
    // code to repeat
} while (condition);
```

3. Break statement: This statement is used to terminate the execution of a loop or switch statement. It allows you to exit the current loop or switch block and continue with the rest of the program. For example:

```
while (condition) {
    // code to repeat
    if (condition2) {
        break; // exit the loop if condition2 is true
    }
}
```

4. Continue statement: This statement is used within a loop to skip the remaining code inside the loop for the current iteration and move on to the next iteration. For example:

```
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        continue; // skip the rest of the code and move to the next iteration if i equals 5
    }
    // code to execute for each iteration except when i equals 5
}
```

These additional control statements can provide more flexibility and control over program execution in various scenarios.


harrywood

A control statement is a statement that determines whether other statements will be executed. An if statement decides whether to execute another statement or decides which of two statements to execute.

anilkh7058

Control statements allows instructor to control loop, structure and condition statements of program.
:)
  •