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!

close

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.

Related posts
C++Introduction to C++Learn C++

Learn To Use Break And Continue In Loops With C++

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

Learn To Define And Use Arrays In C++

C++Learn C++Videos

Learn C++ With Back to Basics: Design Pattern by Mike Shah (CPPCon 2020) Video

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

Discover Switch Statements in C++

Worth reading...
Learn to Use Strings in C++
en_USEnglish