Lesson 10
Control Structures
Control structures determine how a program executes its statements. There are three major control structures in C: Sequential, Selection (Decision-Making), and Jump (Control Transfer) statements.
C Track
Save progress as you learn.
44 lessons
Lesson content
Control Structures in C Programming
Every C program follows a specific order of execution known as the control flow. By default, statements are executed one after another in the order they appear. However, real-world programs often need to make decisions, skip certain statements, or transfer control to another part of the program. This is achieved using control structures.
Control structures determine how a program executes its statements. They allow a program to choose between different execution paths or alter the normal sequence of execution based on conditions.
For example, a program may:
- Display "Pass" or "Fail" based on marks.
- Execute different code depending on the user's choice.
- Exit a function after completing its task.
- Skip unnecessary statements.
Understanding control structures is essential because they help create logical, efficient, and interactive C programs.
Prerequisites
- Introduction to C Programming
- Variables and Constants
- Data Types in C Programming
- Operators and Expressions
- Basic Input and Output (printf() and scanf())
Core Concepts: Control Structures in C Programming
A control structure controls the order in which statements are executed in a C program. By default, C executes statements sequentially. Control structures modify this flow whenever required.
There are three major control structures in C:
- Sequential Control Structure
- Selection (Decision-Making) Control Structure
- Jump (Control Transfer) Statements
Note: Looping (iteration) structures such as for, while, and do...while are discussed separately.
1. Sequential Control Structure
The Sequential Control Structure is the default execution flow of every C program. Statements execute one after another in the order they are written.
Example
#include <stdio.h>
int main()
{
printf("Welcome
");
printf("Learning C
");
printf("Programming
");
return 0;
}Output: Welcome / Learning C / Programming
2. Selection (Decision-Making) Control Structure
Selection statements allow a program to choose one execution path based on a condition.
C provides the following decision-making statements:
- if
- if...else
- else if ladder
- Nested if
- switch
These statements are used whenever a program needs to make decisions.
if Statement
The if statement executes a block of code only if the specified condition is true.
Syntax
if(condition)
{
// statements
}Example
if(age >= 18)
{
printf("Eligible to Vote");
}if...else Statement
The if...else statement executes one block of code if the condition is true and another block if the condition is false.
Syntax
if(condition)
{
// True block
}
else
{
// False block
}Example
if(mark >= 50)
{
printf("Pass");
}
else
{
printf("Fail");
}else-if Ladder
The else-if ladder is used when multiple conditions need to be evaluated.
Syntax
if(condition1)
{
// statements
}
else if(condition2)
{
// statements
}
else
{
// statements
}Example
if(mark >= 90)
{
printf("Grade A");
}
else if(mark >= 75)
{
printf("Grade B");
}
else if(mark >= 50)
{
printf("Grade C");
}
else
{
printf("Fail");
}Nested if Statement
A Nested if is an if statement placed inside another if statement. It is useful when one condition depends on another.
Example
if(age >= 18)
{
if(citizen == 1)
{
printf("Eligible to Vote");
}
}switch Statement
The switch statement selects one block of code from several alternatives based on the value of an expression. It is commonly used as an alternative to multiple if...else statements when comparing a single variable against constant values.
Syntax
switch(expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}Example
switch(choice)
{
case 1:
printf("Addition");
break;
case 2:
printf("Subtraction");
break;
default:
printf("Invalid Choice");
}3. Jump (Control Transfer) Statements
Jump statements transfer the control of execution from one part of a program to another. C provides four jump statements.
| Statement | Purpose |
|---|---|
| break | Terminates a switch statement or loop immediately. |
| continue | Skips the remaining statements in the current loop iteration and moves to the next iteration. (Discussed in the Loops chapter.) |
| goto | Transfers control to a labeled statement within the same function. |
| return | Exits a function and optionally returns a value to the calling function. |
Example: break
switch(choice)
{
case 1:
printf("Option 1");
break;
default:
printf("Invalid Option");
}Example: goto
#include <stdio.h>
int main()
{
goto end;
printf("This will not execute.
");
end:
printf("Program Ended.");
return 0;
}Example: return
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}Advantages of Control Structures
- Controls the execution flow of a program.
- Enables decision-making based on conditions.
- Improves code readability and organization.
- Reduces unnecessary execution of statements.
- Makes programs more interactive and efficient.
Best Practices
- Use braces {} even for single statements.
- Prefer switch when checking multiple constant values of the same variable.
- Keep decision-making conditions simple and readable.
- Avoid excessive nesting of if statements.
- Avoid using goto unless absolutely necessary.
When NOT to Use Certain Control Structures
- Do not use switch for range-based conditions; use if...else instead.
- Avoid deeply nested if statements as they reduce readability.
- Avoid using goto in large programs because it can make code difficult to understand and maintain.
Summary / Key Takeaways
- Control structures determine the order in which program statements are executed.
- The three major control structures covered in this chapter are Sequential, Selection (Decision-Making), and Jump (Control Transfer) statements.
- Sequential execution is the default flow of every C program.
- Decision-making statements allow programs to execute different blocks of code based on conditions.
- Jump statements alter the normal flow of execution.
- Looping structures (for, while, and do...while) are separate iteration control structures and are covered independently.
FAQ About Control Structures in C
1. What are control structures in C programming?
Control structures determine the order in which statements are executed. They allow a program to make decisions and alter the normal flow of execution.
2. What are the main types of control structures in C?
The main control structures are Sequential Control Structure, Selection (Decision-Making) Control Structure, and Jump (Control Transfer) Statements. Looping (iteration) structures are usually discussed separately.
3. What is the purpose of the switch statement?
The switch statement selects one block of code from multiple alternatives based on the value of an expression. It is often used instead of multiple if...else statements.
4. What is the difference between if and switch?
The if statement can evaluate complex conditions using relational and logical operators, while the switch statement compares a single expression against multiple constant values.
5. What are jump statements in C?
Jump statements transfer program control from one location to another. C provides four jump statements: break, continue, goto, and return.
6. Why are control structures important?
Control structures make programs dynamic and interactive by allowing them to make decisions, control execution flow, and respond to different conditions efficiently.