Mastering C++: From Fundamentals to Advanced Programming

0 0 0 0 0

Chapter 3: Control Flow and Operators in C++

🔹 1. Introduction

Controlling the flow of execution is fundamental in every programming language. In C++, control flow enables you to make decisions and perform repetitive tasks using:

  • Conditional statements (e.g. if, else, switch)
  • Loops (e.g. for, while, do-while)
  • Operators (arithmetic, comparison, logical, assignment)

This chapter will teach you how to build logic, branch behavior, and repeat actions, all of which are essential for writing functional programs.


🔹 2. C++ Operators

Operators are symbols that perform operations on variables and values.

Arithmetic Operators

Operator

Description

Example

Output

+

Addition

3 + 2

5

-

Subtraction

3 - 2

1

*

Multiplication

3 * 2

6

/

Division

6 / 2

3

%

Modulo

5 % 2

1

Assignment Operators

Operator

Description

Example

=

Assign

x = 5

+=

Add and assign

x += 2

-=

Subtract and assign

x -= 3

*=

Multiply and assign

x *= 4

Comparison Operators

Operator

Description

Example

==

Equal to

x == y

!=

Not equal

x != y

> 

Greater than

x > y

< 

Less than

x < y

>=

Greater or equal

x >= y

<=

Less or equal

x <= y

Logical Operators

Operator

Description

Example

&&

Logical AND

x > 0 && y > 0

`


`

!

Logical NOT

!(x > 0)


🔹 3. Conditional Statements

if...else Structure

int age = 20;

if (age >= 18) {

    cout << "Adult";

} else {

    cout << "Minor";

}

if...else if...else

int marks = 85;

if (marks >= 90) {

    cout << "Grade A";

} else if (marks >= 75) {

    cout << "Grade B";

} else {

    cout << "Grade C";

}

switch Statement

int day = 3;

switch(day) {

    case 1: cout << "Monday"; break;

    case 2: cout << "Tuesday"; break;

    case 3: cout << "Wednesday"; break;

    default: cout << "Invalid";

}


🔹 4. Looping in C++

Loops are used to run the same block of code multiple times.

for Loop

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

    cout << i << " ";

}

Output: 1 2 3 4 5

while Loop

int i = 1;

while (i <= 5) {

    cout << i << " ";

    i++;

}

do...while Loop

int i = 1;

do {

    cout << i << " ";

    i++;

} while (i <= 5);

The do...while loop always executes at least once.


🔹 5. Loop Control Statements

Statement

Use Case

break;

Exit the loop immediately

continue;

Skip the current iteration and go to the next


🔹 6. Flowchart: Loop Example

Start

|

|-- i = 1

|-- While i <= 5

|     Print i

|     i++

|

End

Control flow diagrams are useful for visualizing loop logic in technical interviews.


🔹 7. Nested Loops Example

for (int i = 1; i <= 3; i++) {

    for (int j = 1; j <= i; j++) {

        cout << "* ";

    }

    cout << endl;

}

Output:

*

* *

* * *



Back

FAQs


1. What is C++ used for today?

C++ is used in game development, operating systems, compilers, IoT, robotics, and performance-critical applications

2. Is C++ beginner-friendly?

  1. Yes — it’s a great first language, though it requires understanding of memory management and syntax.

3. What’s the difference between C and C++?

C is procedural; C++ supports both procedural and object-oriented programming

4. How is memory managed in C++?

Using pointers, references, and manual memory control with new and delete.

5. What is the STL in C++?

The Standard Template Library is a powerful collection of classes like vector, set, map, and queue.

6. Is C++ still relevant in 2025 and beyond?

Absolutely. It's critical in industries like gaming, defense, automotive, and finance

7. Can I build web apps with C++?

Not commonly — C++ is not typically used for frontend or backend web apps. However, it powers backend engines in browsers.

8. Is C++ used in machine learning?

Yes, many machine learning libraries (like TensorFlow) have C++ backends for performance.

9. How hard is it to switch from C++ to Python?

Very easy — once you’ve mastered C++, most modern languages feel simpler.