Skip to main content

Control Structure in C

Control structures determine how a program runs its instructions. They allow the program to make decisions, repeat steps, or jump to different parts of the code based on conditions. A control structure is a combination of:
  • Decision-making statementsif, else, switch
  • Looping statementsfor, while, do-while
  • Jumping statementsbreak, continue, goto, return
Control Structure diagram showing three branches: Decision-Making (if, if-else, switch-case), Looping (for, while, do-while), and Jumping (break, continue, goto, return)

Decision-Making Statements

1. if Statement

Executes a block only when the condition is true.
Example:
Output: Eligible to Vote and Welcome
Without curly braces, only the immediately following line is part of the if. The rest always executes.

2. if-else Statement

Example:

3. if-else if-else Ladder

Check multiple conditions in sequence:

4. Nested if

An if inside another if:

5. switch-case

Use when choosing one option from many based on a single value:
Rules:
  • Expression must be int, char, or enum (NOT float)
  • Case labels must be constants
  • break stops the switch from falling through to the next case
  • default is optional
Example:
Fall-through (no break):

Looping Statements

while Loop

Checks condition before executing the body:

do-while Loop

Executes the body at least once, then checks condition:

for Loop

Best when the number of iterations is known:

Nested Loops

Infinite Loop


Jumping Statements

break

Exits a loop or switch immediately:

continue

Skips the rest of the current iteration:

goto

Transfers control to a labeled statement (use sparingly):

return

Exits a function and optionally returns a value:

exit()

Terminates the entire program immediately. Requires #include <stdlib.h>: