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 statements —
if,else,switch - Looping statements —
for,while,do-while - Jumping statements —
break,continue,goto,return
Decision-Making Statements
1. if Statement
Executes a block only when the condition is true.
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
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:
- Expression must be
int,char, orenum(NOTfloat) - Case labels must be constants
breakstops the switch from falling through to the next casedefaultis optional
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>: