if, if ... else

suggest change

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Flow control:
* if
* if, if ... else
* else
* case
* switch
* catch
* throw
* try
* goto
* return

Table Of Contents
8 Arrays
9 Flow control
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype