×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Control Statements in C++ – Complete Tutorial



Last Updated on: 5th Dec 2025 17:24:17 PM

Control Statements in C++ allow a program to make decisions, repeat tasks, and control the flow of execution.
Without control statements, a program would run top to bottom without any intelligence.

 

With control statements, programs can:

✔ Take decisions
✔ Repeat actions
✔ Jump to specific sections
✔ Execute or skip code blocks
✔ Handle real-life logic

 

Control statements make a program dynamic, smart, and efficient, just like how humans make decisions in daily life.

 

Example:

  • If OTP is correct → allow login

  • If marks ≥ 33 → student passes

  • Repeat “Send data” until the server receives it

  • Display items until user chooses to exit

 

2.Control Statements : -

Control Statements are instructions that determine the flow, direction, and decision-making ability of a program.
They allow C++ to:

  • Choose between multiple paths → (decision control)

  • Repeat blocks of code → (looping control)

  • Jump to another part of code → (branching control)

 

They convert linear code into logical, interactive, and real-world functioning programs.

 

3. Types of Control Statements in C++

C++ provides three major types:

 

1) Decision-Making Statements

  • if

  • if…else

  • else if

  • nested if

  • switch

 

2) Looping Statements (Iterative Statements)

  • for

  • while

  • do…while

 

3) Jump Statements (Branching Statements)

  • break

  • continue

  • goto

  • return

Let's explore each one in detail with practical applications.

 

4. Decision-Making Statements (Selection Statements)

Decision-making allows the program to choose which block to execute based on conditions.

 

4.1 if Statement

Executes a block only if a condition is true.

 

Syntax:

if (condition) {
    // code
}

 

Example:

int age = 20;
if (age >= 18) {
    cout << "Eligible to vote";
}

 

Real-Life Example:

If user entered correct password → login success.

 

4.2 if…else Statement

Executes one block when condition is true, another when false.

 

Example:

int marks = 40;
if (marks >= 33)
    cout << "Pass";
else
    cout << "Fail";

 

Real-Life Example:

If payment successful → show receipt
Else → show payment failed message

 

4.3 else if Ladder

Used to check multiple conditions.

 

Example:

int marks = 85;

if (marks >= 90)
    cout << "Grade A";
else if (marks >= 75)
    cout << "Grade B";
else if (marks >= 60)
    cout << "Grade C";
else
    cout << "Fail";

 

Real-Life Example:

Shopping website decides discount:

  • If > 5000 → 20%

  • If > 3000 → 10%

  • Else → 5%

 

4.4 Nested if Statement

if inside another if.

 

Example:

int age = 25;
bool hasID = true;

if (age >= 18) {
    if (hasID) {
        cout << "Entry Allowed";
    }
}

 

Real-Life Example:

Flight check-in:

  • If passport valid

  • AND if ticket confirmed

  • THEN allow boarding

 

4.5 switch Statement

Used when multiple conditions are based on a single variable.

 

Syntax:

switch(expression) {
    case value1:
        break;
    case value2:
        break;
    default:
}

 

Example:

int day = 3;
switch(day) {
    case 1: cout << "Monday"; break;
    case 2: cout << "Tuesday"; break;
    case 3: cout << "Wednesday"; break;
    default: cout << "Invalid Day";
}

 

Real-Life Example:

ATM menu selection
1 = Withdraw
2 = Check balance
3 = Change PIN

 

5. Looping (Iterative) Statements

Loops repeat a block of code until a condition becomes false.

 

5.1 for Loop

Used when number of iterations is known.

 

Syntax:

for (initialization; condition; increment) {
    // code
}

 

Example:

for (int i = 1; i <= 5; i++) {
    cout << i << " ";
}

 

Real-Life Example:

Print 100 invoices automatically
Upload 50 files
Send 10 emails

 

5.2 while Loop

Used when number of iterations is unknown and depends on condition.

Example:

int i = 1;
while (i <= 5) {
    cout << i << " ";
    i++;
}

 

Real-Life Example:

Keep checking:

  • If user logs in

  • If server responds

  • If battery > 0

 

5.3 do…while Loop

Executes code at least one time even if condition is false.

 

Example:

int i = 1;
do {
    cout << i << " ";
    i++;
} while(i <= 5);

 

Real-Life Example:

ATM always shows menu once
E-commerce always shows the cart page at least once

 

6. Jump Statements

 

6.1 break Statement

Stops loop or switch.

Example:

for (int i = 1; i <= 10; i++) {
    if (i == 5) break;
    cout << i << " ";
}

 

Output:

1 2 3 4


Real-Life Example:
Stop billing when user cancels purchase.

 

6.2 continue Statement

Skips only the current iteration.

Example:

for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    cout << i << " ";
}

 

Output:

1 2 4 5

 

Real-Life Example:

Skip listing products with zero stock.

 

6.3 goto Statement

Used to jump to a labeled section (not recommended).

Example:

int n = 1;
start:
cout << n++ << " ";
if (n <= 5) goto start;

 

Real-Life Example:

In old systems: error handling, logs jumps.

 

6.4 return Statement

Ends function and returns a value.

Example:

int sum(int a, int b) {
    return a + b;
}

 

7. Real-Life Scenario – ATM Program

int option;

cout << "1. Withdraw\n2. Check Balance\n3. Exit\n";
cin >> option;

switch(option) {
    case 1: cout << "Withdrawal Processed"; break;
    case 2: cout << "Your Balance is 50000"; break;
    case 3: cout << "Thank you!"; break;
    default: cout << "Invalid Option";
}

 

8. Real-Life Scenario – School Result System

int marks;
cin >> marks;

if (marks >= 90)
    cout << "A Grade";
else if (marks >= 75)
    cout << "B Grade";
else if (marks >= 33)
    cout << "Pass";
else
    cout << "Fail";

 

9. Real-Life Scenario – E-Commerce Order Loop

int items = 3;
while (items > 0) {
    cout << "Packing item...\n";
    items--;
}

 

10. Conclusion

Control statements are the foundation of logic building in C++.
They help programs:

✔ Make decisions
✔ Repeat tasks
✔ Skip or stop execution
✔ Navigate between code blocks
✔ Build real-life applications

Understanding control statements is essential for mastering C++ programming, problem-solving, competitive coding, and software development.

 

Keep practicing — you're doing amazing!

Happy Coding!    yes

 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo