HSSLIVE Plus One Computer Application Chapter 7: Control Statements Notes

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.

cpp
if (condition) {
    // Code to execute if condition is true
}

Example:

cpp
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.

cpp
if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

Example:

cpp
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.

cpp
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:

cpp
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.

cpp
if (condition1) {
    // Code for condition1
    if (condition2) {
        // Code for both condition1 and condition2
    }
}

Example:

cpp
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.

cpp
switch (expression) {
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    // More cases
    default:
        // Code when no case matches
}

Example:

cpp
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.

cpp
for (initialization; condition; update) {
    // Loop body
}

Example:

cpp
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.

cpp
while (condition) {
    // Loop body
}

Example:

cpp
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.

cpp
do {
    // Loop body
} while (condition);

Example:

cpp
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:

cpp
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.

cpp
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.

cpp
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.

cpp
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.

cpp
// 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:

  1. Chapter 1 Fundamentals of Computer
  2. Chapter 2 Components of the Computer System
  3. Chapter 3 Principles of Programming and Problem Solving
  4. Chapter 4 Getting Started with C++
  5. Chapter 5 Data Types and Operators
  6. Chapter 6 Introduction to Programming
  7. Chapter 7 Control Statements
  8. Chapter 8 Computer Networks
  9. Chapter 9 Internet
  10. Chapter 10 IT Applications

Leave a Comment