Control structures direct the flow of program execution and are essential for creating dynamic, responsive software. This chapter covers decision-making statements (if, if-else, switch), looping structures (while, do-while, for), and jump statements (break, continue, return). Students will learn how to implement selection and repetition in programs, create nested control structures, and understand how these constructs form the backbone of algorithmic solutions to complex problems.
Chapter 7: Control Statements
Decision-Making Statements
if Statement
The if
statement is used to execute a block of code if a specified condition is true.
if (condition) {
// Code to execute if condition is true
}
Example:
int age = 18;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
}
if-else Statement
The if-else
statement provides an alternative path of execution when the condition is false.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int marks = 75;
if (marks >= 50) {
cout << "You have passed." << endl;
} else {
cout << "You have failed." << endl;
}
if-else-if Ladder
Used for multiple conditions where each condition is checked in sequence.
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else if (condition3) {
// Code for condition3
} else {
// Code when none of the conditions are true
}
Example:
int marks = 85;
if (marks >= 90) {
cout << "Grade: A+" << endl;
} else if (marks >= 80) {
cout << "Grade: A" << endl;
} else if (marks >= 70) {
cout << "Grade: B" << endl;
} else if (marks >= 60) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
Nested if Statements
An if
statement inside another if
statement.
if (condition1) {
// Code for condition1
if (condition2) {
// Code for both condition1 and condition2
}
}
Example:
int age = 25;
bool hasLicense = true;
if (age >= 18) {
if (hasLicense) {
cout << "You can drive." << endl;
} else {
cout << "You need a license to drive." << endl;
}
} else {
cout << "You are too young to drive." << endl;
}
switch Statement
The switch
statement is used to select one of many code blocks to execute based on a variable’s value.
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
// More cases
default:
// Code when no case matches
}
Example:
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid day" << endl;
}
Iterative Statements (Loops)
for Loop
Used when the number of iterations is known beforehand.
for (initialization; condition; update) {
// Loop body
}
Example:
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
// Output: 1 2 3 4 5
while Loop
Used when the number of iterations is not known beforehand, and the loop continues as long as the condition remains true.
while (condition) {
// Loop body
}
Example:
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
// Output: 1 2 3 4 5
do-while Loop
Similar to the while
loop, but guarantees that the loop body executes at least once, as the condition is checked after the execution of the loop body.
do {
// Loop body
} while (condition);
Example:
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
// Output: 1 2 3 4 5
Jump Statements
break Statement
The break
statement terminates the closest enclosing loop or switch statement.
Example in loops:
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break; // Exits loop when i equals 6
}
cout << i << " ";
}
// Output: 1 2 3 4 5
continue Statement
The continue
statement skips the rest of the current iteration and moves to the next iteration.
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skips even numbers
}
cout << i << " ";
}
// Output: 1 3 5 7 9
goto Statement
The goto
statement allows the program to jump to a labeled statement, though its use is generally discouraged in modern programming due to its potential to create spaghetti code.
int i = 1;
start:
if (i <= 5) {
cout << i << " ";
i++;
goto start;
}
// Output: 1 2 3 4 5
Nested Loops
A loop inside another loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << i << "," << j << " ";
}
cout << endl;
}
Output:
1,1 1,2 1,3
2,1 2,2 2,3
3,1 3,2 3,3
Infinite Loops
Loops that run indefinitely unless interrupted by a break statement or an external event.
// Infinite for loop
for (;;) {
// Code
if (exitCondition) break;
}
// Infinite while loop
while (true) {
// Code
if (exitCondition) break;
}
Mastering control statements is crucial for creating programs that can make decisions and repeat actions efficiently, forming the foundation of algorithmic problem-solving in any programming language.
Complete Chapter-wise Hsslive Plus One Computer Application Notes
Our HSSLive Plus One Computer Application Notes cover all chapters with key focus areas to help you organize your study effectively:
- Chapter 1 Fundamentals of Computer
- Chapter 2 Components of the Computer System
- Chapter 3 Principles of Programming and Problem Solving
- Chapter 4 Getting Started with C++
- Chapter 5 Data Types and Operators
- Chapter 6 Introduction to Programming
- Chapter 7 Control Statements
- Chapter 8 Computer Networks
- Chapter 9 Internet
- Chapter 10 IT Applications