Control flow statements

Control flow refers to the order in which statements are executed in a program. It allows you to make decisions and repeat blocks of code based on certain conditions. 

Characteristics of control flow in C:

Here are some of the control flow structures in C:

Conditional Statements:

Conditional control flow structures allow you to make decisions and execute different blocks of code based on certain conditions.

The syntax of an if-else statement in C is as follows:

if (condition) {

    // Code to execute if the condition is true

} else {

    // Code to execute if the condition is false


Here's how an if-else statement works:

It's important to note the following points about if-else statements:

Looping Statements:

Looping Statements allow you to repeat a block of code multiple times. They are useful when you want to iterate over a collection, perform a task a specific number of times, or repeat an action until a condition is met. 

The while loop repeatedly executes a block of code as long as a condition is true. The condition is checked before each iteration.

The do-while loop executes a block of code once, and then repeatedly executes it as long as a condition is true. The condition is checked after each iteration.

The for loop provides a compact way to execute a block of code repeatedly for a specific number of times. It consists of an initialization, a condition, and an increment/decrement expression.

The break statement is used to terminate the execution of a loop prematurely. It allows you to exit a loop based on a certain condition.

The continue statement is used to skip the remaining statements in a loop and start the next iteration. It allows you to bypass certain iterations based on a condition.

Switch statement:

The switch statement in C is used to test the value of a variable or an expression against a series of cases. It provides a concise way to handle multiple choices based on the value of a single expression. It provides an alternative to multiple if-else statements.

Here's the syntax of the switch statement:

Here's an example to illustrate the usage of the switch statement:

In this example, the user is prompted to enter a number between 1 and 3. The value is then tested using a switch statement. If the value matches one of the cases (1, 2, or 3), the corresponding code block is executed. If the value doesn't match any case, the code inside the default block is executed.

Note that the break statement is used after each case to terminate the switch statement and prevent falling through to the next case. Without the break statement, the execution would continue to the next case, leading to unintended behavior.

The switch statement provides a structured way to handle multiple choices based on a single expression, making the code more readable and efficient compared to using multiple if-else statements.

Conditional (Ternary) Operator:

The conditional operator ?: provides a shorthand way of writing an if-else statement in a single expression. It allows you to make a decision based on a condition and assign a value accordingly.

These control flow structures provide the ability to make decisions, repeat code, and control the flow of execution in a program. By utilizing these structures effectively, you can create more dynamic and efficient code.