Control Structures (If-else, Loops)

Expert Answer & Key Takeaways

Detailed explanation of decision-making statements (if, else-if, switch) and looping constructs (for, while, do-while) in C programming.

Control Structures in C

In C programming, the default execution flow is sequential (line by line). However, 'Control Structures' allow programmers to alter this flow, enabling decision-making and repetitive execution based on specific conditions.

1. Decision Making Statements (Conditional Statements)

These statements execute a block of code only if a specific condition is evaluated as true (non-zero).

1.1 The if Statement

The simplest control structure. It evaluates a condition; if the condition is true, the code inside the {} is executed. If false, it is skipped.
if (age >= 18) { printf("You can vote."); }

1.2 The if-else Statement

Provides an alternative path. If the condition is true, the if block executes. If it is false, the else block executes.
if (number % 2 == 0) { printf("Even number"); } else { printf("Odd number"); }

1.3 The else if Ladder

Used to test multiple conditions sequentially. As soon as one condition evaluates to true, its block is executed, and the rest of the ladder is skipped.
if (marks >= 90) { printf("Grade A"); } else if (marks >= 80) { printf("Grade B"); } else { printf("Fail"); }

1.4 The switch Statement

A cleaner alternative to a long else-if ladder when testing a single variable against multiple constant values.
  • Must use case to define values.
  • The break keyword stops the switch block. Without it, execution 'falls through' to the next cases.
  • The default case executes if no match is found.
switch (dayNumber) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; default: printf("Invalid Day"); }

2. Looping (Iteration) Statements

Loops allow a block of code to be executed repeatedly as long as a specified condition remains true.

2.1 The while Loop (Entry-Controlled Loop)

The condition is checked before entering the loop body. If the condition is false initially, the loop body may not execute even once.
int i = 1; // Initialization while (i <= 5) { // Condition printf("%d ", i); i++; // Update } // Output: 1 2 3 4 5

2.2 The do-while Loop (Exit-Controlled Loop)

The condition is checked after the loop body executes. Therefore, a do-while loop is guaranteed to execute at least once, regardless of the condition.
int i = 10; do { printf("Hello"); // This prints once even though 10 is not < 5 i++; } while (i < 5);

2.3 The for Loop

The most commonly used loop when the exact number of iterations is known in advance. It condenses initialization, condition, and update into a single line.
// Syntax: for (initialization; condition; update) for (int i = 1; i <= 5; i++) { printf("%d ", i); }

3. Jump Statements

Jump statements transfer the control of the program to another part unconditionally.

3.1 break

Used to immediately exit the innermost loop or switch statement. Control passes to the statement directly following the loop.

3.2 continue

Used inside loops to skip the current iteration and immediately jump to the next iteration (it goes to the update/condition check step).
for (int i = 1; i <= 3; i++) { if (i == 2) { continue; // Skips printing '2' } printf("%d ", i); } // Output: 1 3

3.3 goto

Transfers control to a labeled statement in the program. Generally discouraged in modern programming as it creates confusing 'spaghetti code'.

Course4All Editorial Board

Verified Expert

Subject Matter Experts

Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.

Pattern: 2026 Ready
Updated: Weekly