What is a control structure? Explain the conditional control structure with examples.

A control structure in programming refers to a block of code that determines the flow of execution depending on certain conditions. These structures allow the program to make decisions, repeat code, or select among various alternatives. Control structures are classified into three main types: sequential, selection, and iteration. Selection structures are used when you need to make decisions based on certain conditions, and conditional control structures specifically deal with decisions that depend on conditions being true or false.

The most common conditional control structures are:

  • if statement: Executes a block of code if a condition is true.
  • if-else statement: Executes one block of code if the condition is true, and another if the condition is false.
  • else-if ladder: Allows checking multiple conditions sequentially.
  • switch statement: Allows testing a variable against multiple constant values.

Example of if statement:

if (age > 18) {
printf("Adult");
}

Example of if-else statement:

if (age > 18)
{
printf("Adult");
} else {
printf("Not an Adult");
}

Example of switch statement:

switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Invalid Day");
}