Control Structure – Common Concepts in Programming Languages

Control Structure

We are in the third article of “Learn One Programming Language and Work on All”, so far. I will try my best to make the concept of “Control Structure” very clear and easy-to-understand.

Let’s revive the common concepts list:

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

What is Control Structure?

In easy words, it defines the flow of a program. The way that gets you to a destination.

For instance, in a real-life example of going to another city which is a 100 mile away from your home, you go through different routes and road-types. While traveling you may face one-way routes, turning points or crossroads. These routes and route-mode help you reach the place you look for, at the same time these routes control you from getting into a trap or a false destination.

Control Structures are likely the same as routes and route-modes in the above example. According to wiki:

A control structure is like a block of programming that analyses variables and chooses a direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control \”flows\”). Hence it is the basic decision-making process in computing; It is a prediction.

Hah! We had different route-modes, like one-ways, crossroads, turning point and etc. – do we have same types in Control Structures?

Types of Control Structures

There are three basic types in every programming language, thus, if you understand these types in general, you will master the concept for all programming languages. Implementations and syntax may differ according to languages but concept stands above all.

When a program is running, the code is being read by the computer line by line (from top to bottom, and for the most part left to right), just like you would read a book.  This is known as the “Code Flow”.

  1. Sequential Flow / Sequential Logic
  2. Conditional Flow / Selection Logic
  3. Repetitive Flow / Iteration Logic

Let’s go in detail with some examples.

1- Sequential Flow

\"\"

This means that your path to destination (another city, according to example above) is straight or it means that you have a straight road Infront of you.

In case of programming, you program should flow a serial mode. Or it should follow a sequential flow by reading the code line-by-line (Top to Bottom approach).

2- Conditional Flow

\"conditions


This means that you have reached a crossroad on your way to the other city (according to example above), now you need to make a decision on which route to follow to reach the destination.

Flow based on the condition you meet. If your application is asked to check condition and continue based on a certain condition, then you might put some conditional statements in your codes to change the flow of program, based on what-to-do at certain conditions.

Selection Logic simply involves a number of conditions or parameters which decides one out of several written modules.

Selection Logic or Conditional Flow is having three varieties.

  1. Single Condition Check
  2. Dual Condition Check
  3. Multi-Condition Check

3- Repetitive Flow / Iteration Logic

\"iteration

If you need to repeat some portions/lines of your code, “Repetitive Flow” exactly is what you are up to. This Control Structure gives you the power to iterate in certain conditions according to the condition you set and avoids code redundancy.

When you need to write a thousand line of Hello World, how would you do that?

Or when you have to print information about 150 students from a table, will you write them on your own or will you use an iterator/loop to repeat the steps of printing?

Three basic iterators or loops in almost all programming languages are:

  1. For Loop (For Structure)
  2. While Loop (While Structure)
  3. Do-While Loop (Do-While Structure)

We’ve learned that code flows from top to bottom and for the most part left to right, just like a book.  We’ve learned that we can skip over certain code or execute certain parts of code over and over again, and this is all achieved by using different control structures.  These control structures are immensely important to programming, as they make the programs function properly.  For example, you wouldn’t want people to be able to login to your Facebook account if they enter the wrong password, right?  Well, that’s why we use the if...else control structure, if the passwords match, then login, else show a “password is incorrect” screen.  So, without control structures, your program’s code would only flow in one way, and it would essentially only do one thing over and over again, which wouldn’t be very helpful.  Changing what the code does based on a variable is what makes programs useful to us all!

Finally, hope the concept of “What a Control Structure does?” gets clear to you. See you in the next topic Data Structure.

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these