C++ Flow Control Conditional Structures: if, if..else


if and else:

it used to check whether the given expression returns true or false and acts as such:

if (condition) statement

the condition can be any valid C++ expression that returns something that be checked against truth/falsehood for example:

if (true) { /* code here */ }  // evaluate that true is true and execute the code in the brackets
if (false) { /* code here */ } // always skip the code since false is always false

the condition can be anything, a function, a variable, or a comparison for example

if(istrue()) { } // evaluate the function, if it returns true, the if will execute the code
if(isTrue(var)) { } //evalute the return of the function after passing the argument var
if(a == b) { } // this will evaluate the return of the experssion (a==b) which will be true if equal and false if unequal
if(a) { } //if a is a boolean type, it will evaluate for its value, if it's an integer, any non zero value will be true, 

if we want to check for a multiple expressions we can do it in two ways :

using binary operators :

if (a && b) { } // will be true only if both a and b are true (binary operators are outside the scope here
if (a || b ) { } //true if a or b is true 

using if/ifelse/else:

for a simple switch either if or else

if (a== "test") {
    //will execute if a is a string "test" 
} else {
    // only if the first failed, will execute 

for multiple choices :

if (a=='a') { 
// if a is a char valued 'a'  
} else if (a=='b') {
// if a is a char valued 'b' 
} else if (a=='c') {
// if a is a char valued 'c'
} else { 
//if a is none of the above

however it must be noted that you should use 'switch' instead if your code checks for the same variable's value