What is Control Structure in C++?

Video Tutorial
FREE
Introduction to If-Else thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Programs that we write are not only limited to a linear set of instructions, but there are also times when we have to repeat a certain part of code, make decisions, and change the flow of the program based on a specific condition being fulfilled or not, and more, there come our control structures.

They form the basic building block of structured programming languages.

For example: Suppose we are in the traffic department and have to write a program that only allows people to drive if they are above 18 and have their driving license; now, what will we do?

We will write a specific condition check in our program and will ask the user to enter their age and to tell if they have their driving license, and if both the conditions are satisfied, then only we will allow the user to drive.

This is exactly what control structures are; we are already familiar with them in real life. Our program was going linearly, executing each line one by one, and when it encountered our conditional control structure which will only execute if the condition provided will be fulfilled.

Many times in our code, we will need that a particular piece of code should only execute if a specific condition gets fulfilled. In C++, along with Conditional Control Structures, we also have the Iteration or Loop Control Structure.

Types of Control Structure in C++

There are three types of Control Structures in C++, And all the program processes we write can only be implemented with these three control structures.

Sequence Structure

This is the most simple and basic form of a control structure. It is simply the plain logic we write; it only has simple linear instructions, no decision making, and no loop. The programs we all wrote at the start of our programming journey were this control structure only. It executes linearly line by line in a straight line manner.

See the below code

Output:

Let's understand the program flow and how it happened.

  • Firstly, num1 and num2 were declared and initialized.
  • Then, the sum variable was declared and was assigned the numerical sum of num1 and num2.
  • We are then printing the sum.

Sequence Structure

We can see that our program came one way, in a straight-line manner, with no bending, no reverse, simply a straight flow. This is simply what Sequence Control Structures are.

Selection Structure

Sometimes in our program, we will need to write certain condition checks that this part of code should only execute if a particular condition meets or another part of code should run.

For example: The example we took at the start is that we have to write a program that will validate if a user is eligible to drive or not based on two conditions:

  • if the age is equal to or above 18
  • if the user has their driving license

Let's understand it with the help of a program

Output:

Here,

  • We are first asking the user to enter their age and then ask if they have a driving license.
  • Then we are checking the condition using an if-else control statement, and if it satisfies, then executing the first block of code, else executing the other block of the code.

A block means a group of statements enclosed in curly brackets {....}, and those specific statements are part of that specific block.

In C++, we have two types of Selection Control statements:

  • If else statements
  • Switch case statements We will be understanding in-depth about each of these in the coming section.

conditions

Loop Structure

Suppose we are asked to write a program that prints "Hello World" once. We will simply write it as:

Now, we are asked to write a program that will print "Hello World" 10 times; we can write it as

Output:

But this seems like, a lot of repetition, but we can manage it somehow.

Now, what if we have to print it 100 or, let's say, 1000 times? Are we going to copy-paste the same line 1000 times? No, that will not be feasible. There comes the use of loop control structures.

Whenever in our program we see that a certain piece of code is being repeated repeatedly, then we can enclose that in a loop structure and provide the condition that this code should execute these specific number of times. Taking the above example, suppose we are asked to write a program that will print "Hello World" 1000 times.

Input:

Output:

Loop Structure

We have three types of Loops in C++:

  • While Loop
  • Do While Loop
  • For Loop

Read more about C++ Loops Here

Control statements in C/C++ to Implement Control Structures

Conditional Statements

In general, we have two types of conditional statements in C++:

  • if statements
  • if else-if ladder

if conditional statements Sometimes we only want to check one condition, and if it meets, we want to process some statements.

Syntax:

Example:

Output:

From this, we can also see that the else part inside an if-else statement is optional.

if else-if ladder Unlike above, here we check for multiple cases.

Syntax:

Example:

Output:

Explanation: Here, first, we are reading the user's age from the console. Then we check if the age is 18. In that case, we print "Your age is 18". If the age is greater than 18, then we print a similar message. If both the above conditions are false, the age is smaller than 18, and we notify the user with a similar message.

Iteration Statements

As discussed above, sometimes, in our program, when we want to repeat a certain set of instructions, we can wrap them inside a loop. In C++, we have three types of iteration or loop statements:

  • While loop
  • Do while loop
  • For loop

while loop While loop is used when we want to run a loop till a condition is evaluated to be true

Syntax:

Example:

Input:

Output:

do-while loop This is the same as the while loop. The only difference is that the loop runs at least once, whether the condition is true or false.

Syntax:

Example:

Input

Output:

for loop for loop is used when we are exactly sure in numbers for how many times our loop has to run.

Syntax:

Example:

Output:

Here we have just taken a small glimpse of what Iteration Statements in C++ are.

To understand deeply about loops, visit here

Jump Statements

As the name suggests, jump statements are used to jump out of a block of code, be it a conditional statement, an iteration statement, or something else. In C++, we generally have 4 types of jump statements:

  • break
  • continue
  • goto
  • exit()

break statements Moves the program flow out of the current block when encountered.

Example:

Output:

The point to remember is that it only exits the first block or block in scope, i.e., if we are in nested blocks, which means, block inside a block, then it will only exit the most inner block.

continue statement When encountered, it just skips that particular iteration, which means we are telling the compiler that as soon as you see the continue statement, forget everything written below it for that iteration, and start with the next iteration.

Example:

Output:

We can see that after the condition of i == 5 matches, and the program encounters the continue statement, it skipped that particular iteration by skipping 5 in the output.

goto statement the goto statement is used to move the program flow to a user-defined label. The use of goto is discouraged, as it sometimes confuses fellow programmers and those who will read our code.

Example:

Output:

We can see that goto can be used as a loop, along with a jump statement, but it is advised to avoid the use of goto statements.

exit() function The purpose of using exit() is to terminate or exit the current program with a specific exit code.

Example:

Output:

It will exit the program, and the value we have supplied to the exit() function, here 0, will be returned to the operating system as the program's exit code.

Switch Statements

Switch statements are like if-else statements only, but the difference here is that they check for specific individual cases, unlike if-else statements, which also check for logical expressions.

Syntax:

Example:

Input:

Output:

Both the if-else and switch statements are selection statements and are used to transfer the flow of the program to a specific block of statements upon certain conditions being met. But they indeed have some minor differences. Let's now understand them.

Differences between if-else and switch statements

  • Generally, the expression inside if-else statements are used to decide whether to run their respective block of code if a condition is being met, for example, if (age >= 18) or if (age < 60). On the other hand, in switch statements, we check for individual cases, i.e., case: 18, case: 19, or case: 60.
  • In if statements, we can place multiple checks inside the same expression using && operator, whereas in switch statements, we can only check one case at a time.
  • If else expressions check for equality and logical expression, whereas switch checks only for equality.
  • If else statements can evaluate integers, floating-point values, characters, booleans, etc., whereas switch statements can only evaluate integer or character datatype.
  • With if-else statements, the program flow is always like, either if block will execute or else-if will execute or else will execute, which means only 1 block will execute from the if-else ladder.
  • On the other hand, in switch statements, we can execute all the cases below the matched case if the break statements are not applied.
  • It's time taking and challenging to modify if-else expressions if we want to change the respective cases because tracing down which case is responsible for that specific task can take some time, whereas, with switch statements, it is easy, as here, we are checking for individual cases only. Hence, it's easy to track down the required expression.

True and False

Until now, we have understood the selection and iteration control statements, how each function, and how the code executes.

But the most important part here is the conditions; both works on certain conditions being evaluated as true or false, and both involve decision-making steps. C++ has a built-in datatype bool, specifically designed to hold true and false values.

All the statements that we have studied till now, internally, are evaluated as booleans only. In C++, the value 0 is considered false and the value 1 is considered true.

Logical Operators

We will not always have simple condition checks in our program, like age >= 18 or num < 10, sometimes, we will be in need to check even more complex conditions, we might have to chain two or more conditions together; In that case, we will be using logical operators. We have three types of logical operators in C++:

  • && (logical and operator)
  • || (logical or operator)
  • ! (logical not operator)

Logical AND Operator (&&)

It is a binary operator, i.e., it needs two operands and is evaluated as true only if both the operands are true.

Syntax:

Example:

Input:

Output:

We can see that only if both the conditions are true, i.e., num is greater than or equal to 10 and num is less than 20, then only the condition will be true.

Truth Table:

condition1condition2condition1 && condition2
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Logical OR Operator (||)

It evaluates to true if at least one of the operands is true.

Syntax:

Example:

Input:

Output:

We can see that if either of the two conditions is true, i.e., num is greater than or equal to 10 or num is less than 20, then only the condition will be true.

Truth Table:

condition1condition2condition1 && condition2
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Logical NOT Operator (!)

It is a unary operator and reverses the truth value of its operand.

Syntax:

Example:

Output:

Conditional Operator

Also called a Ternary Operator, when sometimes we just want to do a small and short check and do not want to write the long syntax of if-else or switch statements, then we can simply use the ternary conditional operator.

Syntax:

Example:

Input:

Output:

Unlock the full potential of C++ with our Free C++ course. Join today and earn a recognized certification in C++ programming.

Conclusion

  • Control Structures are used to alter the flow of the program based on certain conditions being met or not.
  • In C++, we mainly talk about selection and loop control structures.
  • There are two selection control structures, if-else ladder and switch statements. Here the if-else checks for equality and logical expressions, whereas switch only checks for equality.
  • In C++, we have three types of loops: while, do-while, and for loop. while and do-while is preferred when we want to iterate through the loop till a certain condition is being evaluated to be true, whereas for loop is used when we know exactly how many times our loop is going to run.
  • Jump statements are used to manipulate the flow of a program when encountered. In C++, we mainly have 4 types of jump statements: break, continue, goto, and exit().
  • In C++, value 0 is evaluated as false and value 1 is evaluated as true.
  • Logical operators are used to combine and check even more complex conditions in our program. In C++ we have three logical operators: &&, ||, !.
  • Ternary operator can be used for a quick selection check in our program. Syntax : condition ? statements if true: statements if false.