C++Introduction to C++Learn C++

Discover If Statements and Conditions in C++

One of the important part in programming is logics in functions and main program. Each these conditions are important to decide how code flow will work in runtime. In programming we use conditions and if – else statements. In C++ if statements can be done in one line or multi lines and it supports the logical conditions as same as used in mathematics. Conditions are logical operators and also conditional operators.

1. Using if() Statement

First let’s see how we use if () statement with ; in one line.

As given here, first we can use if statement in one line, and at the we put “;” to show that we are done with the if-branch, see example below,

This is good to display our if statement in one line, here we check if a is greater than 3 and if it is then we print out “a is greater than 3”. We can safely move the if condition to another line or we can divide it multi-line. Here, we see that all the things after if() statement are if-branch till the ‘;’ character. So it can be also written like this

Sometimes this is clear to see if-condition and if-branch statements well. Or you can separate this one-line if-branch to multi lines as given here,

This is also used to see much more variables or mathematical operations in multi-line. This is very helpful to make long mathematical operations clear. All these 3 examples above are logically the same and they have the same result when compiled.

Another method is we can add multi line if-branch codes inside { and } brackets as below.

Let’s see how we use these in our if statements,

As you see we cover our line inside { } brackets and we used ‘;’ before the } because we need it to indicate that line is ended, { } are mostly used to display multi-line if-branches as below,

2. Comparison with if() clauses

We can use these Comparison Operators in our if statements.

3. Logical operators in if() clauses

In C & C++ programming language, && is used as AND, || is used as OR, ! symbol is used as NOT. These logical operators are given in this example below,

4. Using if-else clauses

We can use two if-branches with one if-else statement;

Note that this statement also works in one line, first ; indicates end of first one-line if-branch and the second ; indicates end of the second one-line if-branch.
We can also use { } as below

5. Using if-else if-else clauses

If clauses can be used with if()… else if()…. else .. format as below;

We can also write this with { } as below;

Some coders like to use this format as below to reduce these lines,

6. Short Hand if-else Methods

There is Short Hand format to code if statements as below

for example same example above can be written as,

Get started building powerful apps with C++Builder!


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++

Learn about Declaration of a Default Constructor Inside of Class Definition

Artificial Intelligence TechC++Learn C++

AI Tech :: Class Based Artificial Neuron Model in C++

Artificial Intelligence TechC++Learn C++

AI Techs :: Array Based Simple Artificial Neuron Model in C++

Artificial Intelligence TechC++Learn C++

AI Tech :: Structure Based Simple Artificial Neuron Model in C++

Worth reading...
Learn How To Use Comments In C++