C++ Lesson 5 – Control Statements

Today we’ll be looking at control statements in C++. We’ll start by looking at conditionals.

Obviously, we have access to the same conditionals that are in C. This includes:

  • If
  • Else
  • Else-if
  • Switch

And these can be implemented in the same way. This is shown below:

These can be used to control the flow of your application, allowing the behaviour to change based on the states within the application.

Next we’ll be looking at loops in C++. You have access to the same loops in C, which includes:

  • For
  • While
  • Do While

Which can be implemented as so:

Whilst the structure and main syntax of the loop is the same, in C++ you have access to iteration over collections. This is done in the same way we do it in Java:

Further to these control statements, GOTO exists. GOTO is the perfect example of the situation where just because something can be done, doesn’t mean it should be.

This is because it can make the code jump out of scope and create other bugs in the code, so it’s best to avoid it at all costs.

Next time

Next time we’ll be looking at classes in C++, we’ll be creating and using classes in our own applications.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started