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.
Table of Contents
1. Using if() Statement
First let’s see how we use if () statement with ; in one line.
1 2 3 |
if ( condition ) if-branch ; |
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,
1 2 3 4 |
int a = 5; if ( a>3 ) std::cout << "a is greater than 3"; |
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
1 2 3 4 5 |
int a = 5; if ( a>3 ) std::cout << "a is greater than 3"; |
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,
1 2 3 4 5 6 |
int a = 5; if ( a>3 ) std::cout << "a is greater than 3"; |
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.
1 2 3 4 5 6 |
if ( condition ) { if_branch; } |
Let’s see how we use these in our if statements,
1 2 3 4 |
int a = 5; if ( a>3 ) { std::cout << "a greater"; } |
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,
1 2 3 4 5 6 7 8 9 |
int a = 5; if ( a>3 ) { std::cout << "a greater than 3\n"; std::cout << "a is not equal to 3\n"; std::cout << "a is not smaller than 3\n"; } |
2. Comparison with if() clauses
We can use these Comparison Operators in our if statements.
1 2 3 4 5 6 7 8 9 10 11 12 |
int a = 5, b = 7; if ( a==b ) std::cout << "a is equal to b \n"; if ( a!=b ) std::cout << "a is not Equal to b \n"; if ( a>b ) std::cout << "a is greater than b \n"; if ( a<b ) std::cout << "a is lower than b \n"; if ( a>=b ) std::cout << "a is greater than or equal to b \n"; if ( a<=b ) std::cout << "a is lower than or equal to b \n"; |
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,
1 2 3 4 5 6 7 8 9 10 11 12 |
bool a=true, b =false; // a AND b if ( a && b ) std::cout << "both true \n"; // a OR b if ( a || b ) std::cout << "a or b is true \n"; // NOT (a AND b) if ( !(a && b) ) std::cout << "both not true \n"; |
4. Using if-else clauses
We can use two if-branches with one if-else statement;
1 2 3 4 5 6 7 |
int a = 5; if ( a>3 ) std::cout << "a is greater than 3"; else std::cout << "a is not greater than 3"; |
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
1 2 3 4 5 6 7 8 9 10 11 |
int a = 5; if ( a>3 ) { std::cout << "a is greater than 3"; } else { std::cout << "a is not greater than 3"; } |
5. Using if-else if-else clauses
If clauses can be used with if()… else if()…. else .. format as below;
1 2 3 4 5 6 7 8 9 |
int a = 5; if ( a==3 ) std::cout << "a is equal to 3"; else if ( a>3 ) std::cout << "a is greater than 3"; else std::cout << "a is smaller than 3"; |
We can also write this with { } as below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
int a = 5; if ( a==3 ) { std::cout << "a is equal to 3"; } else if(a>3) { std::cout << "a is greater than 3"; } else { std::cout << "a is smaller than 3"; } |
Some coders like to use this format as below to reduce these lines,
1 2 3 4 5 6 7 8 9 |
int a = 5; if ( a==3 ){ std::cout << "a is equal to 3"; } else if ( a>3 ) { std::cout << "a is greater than 3"; } else { std::cout << "a is smaller than 3"; } |
6. Short Hand if-else Methods
There is Short Hand format to code if statements as below
1 2 3 |
variable = (condition) ? true_expression : false_expression; |
for example same example above can be written as,
1 2 3 4 5 |
int a= 5; string result = (a>3) ? "greater than 3" : "not greater than 3"; std::cout << result; |
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition