Fundamental concepts of programming Part #2 Control

4 Major Fundamental concepts of programming:

  1. Variables
  2. Control Structures
  3. Data Structures
  4. Syntax

What are Control Structures

Control structures are simply conditions in the code which keep a program running. When the code is running, each line is executed until a certain decision needs to be made. A real life example is when you are playing an RPG computer game and you are given a choice of which weapon to use. If you want to choose a knife, the damage would be set to 5, but if you choose a sword, the damage would be set to 10.

The decision that will be made by the user playing the RPG computer game, will affect the flow of code in the video game, which are the control structures.

Here is a code example of choosing a weapon in an RPG computer game

Control structures photo 1

As you can see above, this control structure is an if..else condition. If either knife or sword is selected, one of the decisions is executed and the other decision is ignored. When a decision is executed, the two curly braces in the body {} will set a value to the variable “power”.

Another example of control structures is loops. All the basic loops are covered in the C++ Better Explained book which you can purchase here. In the last article, covering variables, we talked about Booleans, where a real life example is a playable character was dead in a video game, a Boolean variable that was currently on false, will trigger to true and end the game. This concept being applied is shown below.

Control structures photo 2

The while loop code will keep executing the code between the curly braces {}, repeatedly  until the variable character_alive  becomes false.

Summary

Control structures are what creates decisions in programming and keeps running code until a certain condition has been broken. If..else statements and while loops are two of the basic parts of control structures but C++ features a few more control structure types.

Share this post