×
>
<

C++ Programming

C++ Control Structures | CrackEase

Control Structures

Control Structures Introduction

Control Structures 

Control structures in C++ determine the order in which statements are executed. They allow programs to make decisions, repeat actions, and branch based on conditions.

Without control structures, programs would execute sequentially from top to bottom. Control structures enable dynamic behavior by allowing conditional execution and repetition of code blocks.

Note: C++ provides three types of control structures: selection (if, switch), iteration (for, while, do-while), and jump statements (break, continue, return).

Types of Control Structures
Control Structure Types

Categories of Control Structures:-

  • Selection Statements: Execute code based on conditions (if, if-else, switch).
  • Iteration Statements: Repeat code blocks (for, while, do-while loops).
  • Jump Statements: Transfer control unconditionally (break, continue, return, goto).
  • Decision Making: Choose between multiple paths based on boolean expressions.
  • Flow Control: Alter the natural sequential execution of statements.
Selection Statements
If Statement

1. if Statement:

Executes a block of code only when the specified condition evaluates to true. If the condition is false, the block is skipped entirely.

2. if-else Statement:

Provides an alternative block to execute when the condition is false. Ensures one of two code paths is always taken.

3. else-if Ladder:

Allows testing multiple conditions sequentially. The first true condition's block executes, and remaining conditions are skipped.

1. if Statement

The if statement executes a block of code only when the condition is true.

Syntax:

  
if (condition)
{
    // statements executed if condition is true
}
  

Example:

  
#include <iostream>
using namespace std;

int main()
{
    int a = 8;
    int b = 5;
    
    if (a > b)
    {
        cout << "a is greater than b" << endl;
    }
    
    return 0;
}
  

Output:

  
a is greater than b
  
2. if-else Statement

The if-else statement provides an alternative block when the condition is false.

Syntax:

  
if (condition)
{
    // executed if condition is true
}
else
{
    // executed if condition is false
}
  

Example - Check Odd or Even:

  
#include <iostream>
using namespace std;

int main()
{
    int number;
    
    cout << "Enter an integer: ";
    cin >> number;
    
    if (number % 2 == 0)
    {
        cout << number << " is an even integer." << endl;
    }
    else
    {
        cout << number << " is an odd integer." << endl;
    }
    
    return 0;
}
  

Output:

  
Enter an integer: 7
7 is an odd integer.
  
3. else-if Ladder

Use else if when you need to check multiple conditions sequentially. The first true condition's block runs and the rest are skipped.

Syntax:

  
if (condition1)
{
    // executed if condition1 is true
}
else if (condition2)
{
    // executed if condition1 is false and condition2 is true
}
else
{
    // executed if none of the above conditions is true
}
  

Example - Compare Two Numbers:

  
#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int b = 5;
    
    if (a > b)
    {
        cout << "a is greater than b" << endl;
    }
    else if (a < b)
    {
        cout << "a is less than b" << endl;
    }
    else
    {
        cout << "a is equal to b" << endl;
    }
    
    return 0;
}
  

Output:

  
a is equal to b
  

Common Comparison Operators:

  
if (a == b)          // a is equal to b
if (a != b)          // a is not equal to b
if (a < b)           // a is less than b
if (a > b)           // a is greater than b
if (a <= b)          // a is less than or equal to b
if (a >= b)          // a is greater than or equal to b
if (a == b && a > 5) // a equals b AND a is greater than 5
if (a == b || a > 5) // a equals b OR a is greater than 5
  
Switch Statement
Switch Statement

Switch Case Statement:-

  • Multi-way Branch: Select one of many code blocks based on a value.
  • Integer/Enum Types: Works with integral types (int, char) and enums.
  • Case Labels: Each case must have a constant value.
  • Break Statement: Prevents fall-through to subsequent cases.
  • Default Case: Executes when no case matches (optional but recommended).
Switch Statement Example

Syntax:

  
switch (expression)
{
    case constant1:
        // statements
        break;
    case constant2:
        // statements
        break;
    default:
        // statements (when no case matches)
}
  

Example:

  
#include <iostream>
using namespace std;

int main()
{
    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;
        default:
            cout << "Weekend" << endl;
    }
    
    return 0;
}
  

Output:

  
Wednesday
  

Important: Always include break after each case to prevent fall-through. Without break, execution continues to the next case.

Iteration Statements (Loops)
Loops in C++

1. for Loop:

Best when the number of iterations is known beforehand. Combines initialization, condition, and increment in one line.

2. while Loop:

Entry-controlled loop that checks the condition before each iteration. Used when iterations depend on a condition.

3. do-while Loop:

Exit-controlled loop that executes at least once before checking the condition. Useful for menu-driven programs.

1. for Loop

The for loop is ideal when you know exactly how many times to iterate. It combines initialization, condition checking, and increment/decrement in a single line.

Syntax:

  
for (initialization; condition; increment/decrement)
{
    // loop body
}
  

Example - Print Numbers 1 to 10:

  
#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 10; i++)
    {
        cout << i << endl;
    }
    
    return 0;
}
  

Output:

  
1
2
3
4
5
6
7
8
9
10
  
2. while Loop

The while loop is an entry-controlled loop that checks the condition before executing the loop body. It's used when the number of iterations is not known in advance.

Syntax:

  
while (condition)
{
    // loop body
    // update statement
}
  

Example - Print Numbers 1 to 10:

  
#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    
    while (i <= 10)
    {
        cout << i << endl;
        i++;
    }
    
    return 0;
}
  

Output:

  
1
2
3
4
5
6
7
8
9
10
  
3. do-while Loop

The do-while loop is an exit-controlled loop that executes the body first, then checks the condition. This guarantees at least one execution.

Syntax:

  
do
{
    // loop body
    // update statement
} while (condition);
  

Example - Print Numbers 1 to 10:

  
#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    
    do
    {
        cout << i << endl;
        i++;
    } while (i <= 10);
    
    return 0;
}
  

Output:

  
1
2
3
4
5
6
7
8
9
10
  

Note: The semicolon after while(condition); is required in do-while loops.

Jump Statements
Jump Statements

1. break Statement:

Terminates the innermost loop or switch statement immediately and transfers control to the statement following it.

2. continue Statement:

Skips the remaining statements in the current loop iteration and proceeds directly to the next iteration.

3. return Statement:

Exits the current function and optionally returns a value to the calling function.

break Statement

The break statement terminates the innermost enclosing loop or switch statement. Execution continues with the statement immediately following the terminated statement.

Example - Exit Loop When i Equals 5:

  
#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 10; i++)
    {
        if (i == 5)
        {
            break;  // exit loop when i is 5
        }
        cout << i << endl;
    }
    
    cout << "Loop ended" << endl;
    
    return 0;
}
  

Output:

  
1
2
3
4
Loop ended
  
continue Statement

The continue statement skips the remaining code in the current iteration and jumps to the next iteration of the loop.

Example - Skip Number 5:

  
#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 7; i++)
    {
        if (i == 5)
        {
            continue;  // skip when i is 5
        }
        cout << i << endl;
    }
    
    return 0;
}
  

Output:

  
1
2
3
4
6
7
  

Observation: Number 5 is skipped because continue jumps to the next iteration before the cout statement executes.

Loop Comparison Table
Feature for Loop while Loop do-while Loop
Control Type Entry-controlled Entry-controlled Exit-controlled
Minimum Execution Zero times Zero times At least once
Best Use Case Known iterations Condition-based Execute first, check later
Syntax Complexity All in one line Condition only Condition at end
Footer Content | CrackEase