Control Statements in C++

Topic 15 of 15

Up until now, our programs have been like a straight road—they execute line-by-line from top to bottom. But what if your program needs to make a choice? In this lecture, we explore Control Structures, which allow your code to be selective about what it runs.

Definitions of Core Topics

Before we dive into the details, let’s define the building blocks of program logic:

Topic

Simple Definition

Control Structure A way to decide which parts of a program run and which are skipped.
Condition A specific requirement that must be “True” for a certain block of code to execute.
Expression In this context, it is a comparison that results in either a True or False value.
Relational Operator Special symbols (like > or <) used to compare two values.
Block of Code A group of instructions enclosed in braces { } that run together.
Flowchart A visual diagram that maps out how a program moves from one step to the next.

From Sequential to Selective Execution

In our previous lessons, code was sequential: line 1, then line 2, then line 3. However, real life involves decisions. For example, you might decide: “If the shop is open, I will buy milk”.

In programming, we use Selective Execution to choose specific parts of a program to run based on current data. This is essential for complex systems, such as a bank notifying a customer only if their balance falls below a certain limit.

Types of Control Statements in C++:

In C++ we can divide the control statements into the following three types:

  • Selection Statements:

Selection statements are used to decide which part of the program will execute and which one will not based on a condition.

  • It can be divided into the following types:
    if satement
  • else if statement
    else statement
  • switch statement

Note: We will discuss these statements in detail in the later sessions.

  • Iteration Statements:

In C++ iteration statements are used to execute a block of statements again and again.
These satements are also called loops in C++.

  • There are mainly three types of loops in C++
    for loop
  • while loop
  • do-while loop

Note: Each types will be discussed later in detail.

  • Jump Statements:

These statements are used to skip a portion of code and jump to another part of code when a certain condition is met.

It can be categorized into:

  • break statement
  • continue statement
  • exit statement
  • return statement
    Note: Each will be explained later.

The Mechanics of a Condition: Relational Operators

A condition is essentially an expression that compares two values. To make these comparisons, C++ uses Relational Operators.

There are six primary relational operators you need to know:

  1. < (Smaller than)
  2. > (Greater than)
  3. == (Equal to) — Note: This uses two equals signs!
  4. <= (Smaller than or equal to)
  5. >= (Greater than or equal to)
  6. != (Not equal to)
Critical Tip: Never confuse = with ==. A single = assigns a value to a variable (like a container), while a double == compares two values to see if they are the same.

 

Bonus: Visualizing Logic with Flowcharts

Before writing complex code, programmers often use flowcharts.

  • Oval: Start and End of the program.
  • Diamond: Represents the Condition (the decision point).
  • Rectangle: Represents an action or statement.
  • Arrows: Show the direction of the program’s “flow”.

Using a flowchart helps you see exactly where the program will skip code if a condition is not met.

Engagement Boost: What’s your biggest C++ fear—logical conditions, flowcharts, or just remembering where the semicolons go? Let us know in the comments!