Meta Title (58 characters)
C Control Statements Tutorial: if, Loops & Switch Guide
Meta Description (156 characters)
Learn C control statements with examples. Master if, if-else, nested if, switch, while, do-while, for, break, continue, goto, and practice programs.
Module 4: Control Statements in C
Learning Objectives
By the end of this module, you will be able to:
- Understand what control statements are and why they are important.
- Make decisions using
if,if-else, and nestedif. - Select multiple options using the
switchstatement. - Repeat tasks using
while,do-while, andforloops. - Control loop execution using
breakandcontinue. - Learn the basics of the
gotostatement and when to avoid it. - Build beginner projects using control statements.
Introduction
Control statements determine the flow of execution in a C program. By default, a program executes statements one after another from top to bottom. However, many real-world applications require making decisions, repeating tasks, or jumping to different parts of the code.
For example:
- Check whether a student passed or failed.
- Print numbers from 1 to 100.
- Display a menu and perform the selected operation.
- Exit a loop when a condition is met.
Control statements make programs dynamic, interactive, and intelligent.
Types of Control Statements
C provides three main categories of control statements:
-
Decision-Making Statements
ifif-elsenested ifswitch
-
Looping Statements
whiledo-whilefor
-
Jump Statements
breakcontinuegoto
The if Statement
The if statement executes a block of code only when a condition is true.
Syntax
1if (condition) 2{ 3 // Code to execute 4}
Flow
1Condition 2 | 3True ----> Execute Block 4 | 5False ---> Skip Block
Example
1#include <stdio.h> 2 3int main() 4{ 5 int age = 20; 6 7 if (age >= 18) 8 { 9 printf("You are eligible to vote."); 10 } 11 12 return 0; 13}
Output
1You are eligible to vote.
The if-else Statement
The if-else statement chooses between two blocks of code.
Syntax
1if (condition) 2{ 3 // True block 4} 5else 6{ 7 // False block 8}
Example
1#include <stdio.h> 2 3int main() 4{ 5 int marks; 6 7 printf("Enter marks: "); 8 scanf("%d", &marks); 9 10 if (marks >= 40) 11 printf("Pass"); 12 else 13 printf("Fail"); 14 15 return 0; 16}
Output
1Enter marks: 35 2Fail
The else if Ladder
Use an else if ladder when checking multiple conditions.
Example
1#include <stdio.h> 2 3int main() 4{ 5 int marks; 6 7 printf("Enter marks: "); 8 scanf("%d", &marks); 9 10 if (marks >= 90) 11 printf("Grade A"); 12 else if (marks >= 75) 13 printf("Grade B"); 14 else if (marks >= 60) 15 printf("Grade C"); 16 else 17 printf("Grade D"); 18 19 return 0; 20}
Nested if
A nested if is an if statement placed inside another if.
Syntax
1if (condition1) 2{ 3 if (condition2) 4 { 5 // Code 6 } 7}
Example
1#include <stdio.h> 2 3int main() 4{ 5 int age = 22; 6 int citizen = 1; 7 8 if (age >= 18) 9 { 10 if (citizen == 1) 11 { 12 printf("Eligible to vote."); 13 } 14 } 15 16 return 0; 17}
The switch Statement
The switch statement selects one block of code from multiple choices.
It is often cleaner than using many else if statements.
Syntax
1switch(expression) 2{ 3 case value1: 4 // Code 5 break; 6 7 case value2: 8 // Code 9 break; 10 11 default: 12 // Code 13}
Example
1#include <stdio.h> 2 3int main() 4{ 5 int day; 6 7 printf("Enter day number (1-7): "); 8 scanf("%d", &day); 9 10 switch(day) 11 { 12 case 1: 13 printf("Monday"); 14 break; 15 16 case 2: 17 printf("Tuesday"); 18 break; 19 20 case 3: 21 printf("Wednesday"); 22 break; 23 24 case 4: 25 printf("Thursday"); 26 break; 27 28 case 5: 29 printf("Friday"); 30 break; 31 32 case 6: 33 printf("Saturday"); 34 break; 35 36 case 7: 37 printf("Sunday"); 38 break; 39 40 default: 41 printf("Invalid day."); 42 } 43 44 return 0; 45}
Why break is Important in switch
Without break, execution continues into the next case. This behavior is called fall-through.
Example:
1switch(choice) 2{ 3 case 1: 4 printf("One"); 5 case 2: 6 printf("Two"); 7}
If choice is 1, both "One" and "Two" are printed.
The while Loop
A while loop repeats a block of code while a condition remains true.
Syntax
1while(condition) 2{ 3 // Code 4}
Flow
1Condition 2 | 3True 4 | 5Execute Block 6 | 7Repeat
Example
1#include <stdio.h> 2 3int main() 4{ 5 int i = 1; 6 7 while(i <= 5) 8 { 9 printf("%d\n", i); 10 i++; 11 } 12 13 return 0; 14}
Output
11 22 33 44 55
The do-while Loop
A do-while loop executes the code at least once, even if the condition is false.
Syntax
1do 2{ 3 // Code 4} 5while(condition);
Example
1#include <stdio.h> 2 3int main() 4{ 5 int i = 1; 6 7 do 8 { 9 printf("%d\n", i); 10 i++; 11 } 12 while(i <= 5); 13 14 return 0; 15}
Difference Between while and do-while
| while | do-while |
|---|---|
| Condition checked first | Condition checked last |
| May execute zero times | Executes at least once |
The for Loop
The for loop is ideal when the number of iterations is known.
Syntax
1for(initialization; condition; update) 2{ 3 // Code 4}
Example
1#include <stdio.h> 2 3int main() 4{ 5 for(int i = 1; i <= 10; i++) 6 { 7 printf("%d\n", i); 8 } 9 10 return 0; 11}
Infinite Loop
1for(;;) 2{ 3 printf("Hello"); 4}
Be careful—this loop never ends unless interrupted.
The break Statement
The break statement immediately exits a loop or switch statement.
Example
1#include <stdio.h> 2 3int main() 4{ 5 for(int i = 1; i <= 10; i++) 6 { 7 if(i == 6) 8 break; 9 10 printf("%d ", i); 11 } 12 13 return 0; 14}
Output
11 2 3 4 5
The continue Statement
The continue statement skips the current iteration and moves to the next iteration.
Example
1#include <stdio.h> 2 3int main() 4{ 5 for(int i = 1; i <= 5; i++) 6 { 7 if(i == 3) 8 continue; 9 10 printf("%d ", i); 11 } 12 13 return 0; 14}
Output
11 2 4 5
The goto Statement (Basics)
The goto statement transfers control to a labeled statement.
Although supported in C, it should be used sparingly because it can make code difficult to read and maintain.
Syntax
1goto label; 2 3/* Other statements */ 4 5label: 6 // Code
Example
1#include <stdio.h> 2 3int main() 4{ 5 printf("Start\n"); 6 7 goto end; 8 9 printf("This line will not execute.\n"); 10 11end: 12 printf("End\n"); 13 14 return 0; 15}
Output
1Start 2End
When to Use goto
- Exiting deeply nested loops (rare cases)
- Error handling in low-level system programming
When to Avoid goto
- General application development
- Replacing loops or conditional statements
- Creating unstructured "spaghetti code"
Practice Project 1: Number Guessing Game
1#include <stdio.h> 2 3int main() 4{ 5 int secret = 25; 6 int guess; 7 8 printf("Guess a number between 1 and 50: "); 9 scanf("%d", &guess); 10 11 if(guess == secret) 12 printf("Congratulations! You guessed correctly."); 13 else if(guess < secret) 14 printf("Too low!"); 15 else 16 printf("Too high!"); 17 18 return 0; 19}
Practice Project 2: Multiplication Table
1#include <stdio.h> 2 3int main() 4{ 5 int number; 6 7 printf("Enter a number: "); 8 scanf("%d", &number); 9 10 for(int i = 1; i <= 10; i++) 11 { 12 printf("%d x %d = %d\n", number, i, number * i); 13 } 14 15 return 0; 16}
Practice Project 3: Pattern Printing
Right-Angled Triangle
1#include <stdio.h> 2 3int main() 4{ 5 int rows; 6 7 printf("Enter number of rows: "); 8 scanf("%d", &rows); 9 10 for(int i = 1; i <= rows; i++) 11 { 12 for(int j = 1; j <= i; j++) 13 { 14 printf("* "); 15 } 16 17 printf("\n"); 18 } 19 20 return 0; 21}
Output
1* 2* * 3* * * 4* * * * 5* * * * *
Common Mistakes to Avoid
- Forgetting braces
{}around multiple statements inifor loops. - Omitting the
breakstatement in aswitch, causing unintended fall-through. - Creating infinite loops by not updating the loop variable.
- Using
=instead of==in conditions. - Overusing
gotoinstead of structured control statements. - Forgetting the semicolon after
while(condition);in ado-whileloop.
Best Practices
- Use
if-elsefor simple decisions andswitchfor multiple fixed options. - Prefer
forloops when the number of iterations is known. - Use
whileloops when the number of iterations depends on a condition. - Keep loop bodies simple and readable.
- Use
breakandcontinueonly when they improve code clarity. - Avoid
gotoin most applications and favor structured programming.
Module Summary
In this module, you learned:
- How to make decisions using
if,if-else,else if, and nestedif. - How to select from multiple options using the
switchstatement. - How to repeat tasks using
while,do-while, andforloops. - How to control loop execution with
breakandcontinue. - The basics of the
gotostatement and why it should be used cautiously. - How to apply these concepts by building a number guessing game, a multiplication table generator, and a pattern printing program.
After completing this module, you'll be ready to learn functions in C, including function declaration, definition, parameters, return values, recursion, scope, and storage classes, allowing you to write modular and reusable programs.