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

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

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