Control statements provide the logic and decision-making capabilities that allow programs to respond differently based on conditions and to repeat operations. This chapter covers the three fundamental control structures: sequence, selection (if-else constructs and switch statements), and iteration (for, while, and do-while loops). It demonstrates how these structures can be combined and nested to implement complex algorithms, with emphasis on loop design patterns, loop invariants, and techniques for ensuring proper loop termination.
Chapter 7: Control Statements
Control statements direct the flow of program execution based on conditions or for repetitive tasks. They are fundamental building blocks that enable programs to make decisions and repeat actions. C++ provides three main types of control statements: selection (decision-making), iteration (loops), and jump statements.
Selection Statements
Selection statements allow a program to execute different code blocks based on certain conditions.
if Statement
The if
statement executes a block of code if a specified condition is true.
Syntax:
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 block of code to execute when the condition is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int number = 15;
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
if-else if-else Statement
This structure allows checking multiple conditions in sequence.
Syntax:
if (condition1) {
// Code for condition1 true
} else if (condition2) {
// Code for condition2 true
} else if (condition3) {
// Code for condition3 true
} else {
// Code for all conditions false
}
Example:
int marks = 75;
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: B" << endl;
} else {
cout << "Grade: C" << endl;
}
Nested if Statements
An if
statement can be placed inside another if
or else
block.
Example:
int age = 25;
bool hasID = true;
if (age >= 18) {
if (hasID) {
cout << "You can enter the venue." << endl;
} else {
cout << "You need to show ID to enter." << endl;
}
} else {
cout << "You must be 18 or older to enter." << endl;
}
switch Statement
The switch
statement selects one of many code blocks to execute based on the value of an expression.
Syntax:
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
// More cases...
default:
// Code for when no cases match
}
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 number" << endl;
}
Iteration Statements (Loops)
Loops allow a program to execute a block of code repeatedly.
for Loop
The for
loop is typically used when the number of iterations is known in advance.
Syntax:
for (initialization; condition; update) {
// Code to repeat
}
Example:
// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
// Output: 1 2 3 4 5
while Loop
The while
loop continues executing as long as a specified condition is true.
Syntax:
while (condition) {
// Code to repeat
}
Example:
// Print numbers from 1 to 5
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
// Output: 1 2 3 4 5
do-while Loop
The do-while
loop is similar to the while
loop, but it guarantees that the code block is executed at least once before checking the condition.
Syntax:
do {
// Code to repeat
} while (condition);
Example:
// Print numbers from 1 to 5
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
// Output: 1 2 3 4 5
Nested Loops
Loops can be placed inside other loops to handle multi-dimensional data or complex repetitive tasks.
Example:
// Print a 3x3 multiplication table
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << i * j << "\t";
}
cout << endl;
}
Output:
1 2 3
2 4 6
3 6 9
Jump Statements
Jump statements alter the normal flow of program execution.
break Statement
The break
statement terminates the innermost loop or switch statement.
Example in a loop:
// Print numbers from 1 to 10, but stop at 5
for (int i = 1; i <= 10; i++) {
if (i > 5) {
break;
}
cout << i << " ";
}
// Output: 1 2 3 4 5
continue Statement
The continue
statement skips the rest of the current iteration and proceeds to the next iteration of the loop.
Example:
// Print odd numbers from 1 to 10
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
cout << i << " ";
}
// Output: 1 3 5 7 9
goto Statement
The goto
statement transfers control to a labeled statement within the same function. (Note: Generally discouraged in modern programming due to potential for creating “spaghetti code”)
Example:
int i = 1;
start:
if (i <= 5) {
cout << i << " ";
i++;
goto start;
}
// Output: 1 2 3 4 5
return Statement
The return
statement exits the current function and optionally returns a value.
Example:
int getMax(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
Practical Applications of Control Statements
Example 1: Finding the Sum of First n Natural Numbers
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum of first " << n << " natural numbers is: " << sum << endl;
return 0;
}
Example 2: Menu-Driven Program
#include <iostream>
using namespace std;
int main() {
int choice;
bool running = true;
while (running) {
cout << "\nMENU\n";
cout << "1. Add two numbers\n";
cout << "2. Check if a number is prime\n";
cout << "3. Check if a number is even or odd\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum: " << a + b << endl;
break;
}
case 2: {
int num, i;
bool isPrime = true;
cout << "Enter a positive integer: ";
cin >> num;
if (num <= 1) {
isPrime = false;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}
break;
}
case 3: {
int num;
cout << "Enter an integer: ";
cin >> num;
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
break;
}
case 4:
running = false;
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
Example 3: Pattern Printing
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
// Print stars
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl;
}
return 0;
}
Output for rows = 5:
*
***
*****
*******
*********
Understanding control statements is crucial for developing efficient and effective programs. They enable programs to make decisions, repeat tasks, and control the flow of execution, allowing for the implementation of complex algorithms and solutions to various problems.
Complete Chapter-wise Hsslive Plus One Computer Science Notes
Our HSSLive Plus One Computer Science Notes cover all chapters with key focus areas to help you organize your study effectively:
- Chapter 1 The Discipline of Computing
- Chapter 2 Data Representation and Boolean Algebra
- Chapter 3 Components of the Computer System
- Chapter 4 Principles of Programming and Problem Solving
- Chapter 5 Introduction to C++ Programming
- Chapter 6 Data Types and Operators
- Chapter 7 Control Statements
- Chapter 8 Arrays
- Chapter 9 String Handling and I/O Functions
- Chapter 10 Functions
- Chapter 11 Computer Networks
- Chapter 12 Internet and Mobile Computing