Flow of Control (if, else, while, for)
Tags: cpp, beginners, computerscience, oop
Published: 2020-11-21 05:50:25

---
title: Flow of Control (if, else, while, for)
published: true
description: Branching and Loops in C++
tags: cpp, beginner, computerscience, oop 
cover_image: https://images.unsplash.com/photo-1527266237111-a4989d028b4b?ixlib=rb-1.2.1&auto=format&fit=crop&w=2550&q=80
series: Programming 101 with C++
---

**Branching and loops** are at the heart of most calculations and decisions in your program.

Normally code is executed **top to bottom**. A number of keywords change this:
* (For branching)
 * if
 * else
* (For looping)
 * while
 * for

These **keywords** work with **conditions that are logical expressions** that can be **true or false**
> (x > 0)
> (y - 2 < b)

Here, parenthesis or round brackets are necessary around conditions in C++.

# Operators

There are operators to compare two operands -
> \> \>= < <= == !=

**Note:**
**==:** two equal signs are used for **comparison**
**=:** single equal sign in used for **assignment**
**!:** means **'not'**

# if

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w4h2qt7prwjjakvaiovj.png)

* condition must always have ()
* statements will run only if condition is true

### Code to compare two numbers
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iub28tq3ndeuhfc7iz8n.png)

### Garbage In, Garbage Out

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fk0hy126j9kwg4watdg1.png)

**Type rules apply here as well.** Giving decimal value confuses the parser and secondNumber is assigned value 0.

# else

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dsptuwcfes61nugn2hfb.png)

* an else can **only** be used right after an **if**
* here else statements run if condition is false, i.e. i >= j.

# while

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q2j6rus7pa6jarxgeicl.png)

* keeps going as long as the condition is **true**
* curly braces are optional for single line statements but **use them anyway**
* a while loop may not run at all if the condition is never true
* loop body **must change** something about the condition, otherwise we may have an **infinite loop**

### Comparing two numbers until user says otherwise
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ju6muv9m10wwoq4tzg3j.png)

### Garbage In, Garbage Out

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zgdwrv1rwkpmwdhwghnq.png)


# for

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/34fout3mds8c9isydk36.png)

Traditional for loop has three parts separated by **semi-colons** (not commas) -
* initialiser (typically also declares the variable)
* continue condition
* incrementer

Loop body **doesn't have** to change anything about the condition if the incrementer does.

**Tip:** Use a debugger to see which statement in executed.

**TO-DO:** Write a **"Guess the Number" game** where you hard code a number into code and then prompt user to guess the number. If the guessed number is lesser/greater than the hard coded number, let the user know. If the user guess correctly, prompt success response and exit.

[Code](https://github.com/SaloniGoyal/CPP/blob/main/guessMyNumber.cpp) for reference.

# Other Flow of Control Keywords
* switch
* range based for - used with Collections
* break - makes a loop stop early
* continue - makes a loop skip some of its processing
* do (almost never used)
* goto (almost never used)

Please leave out comments with anything you don't understand or would like for me to improve upon.

Thanks for reading!