Conditional Statements - C P P

If


  • Syntax
        if(condition) {
            // statement
        }
    

If Else


  • Syntax
        if(condition) {
            // statement
        } else {
            // statement
        }
    

If Else if Else


  • Syntax
        if(condition) {
            // statement
        } else if {
            // statement
        } else {
            // statement
        }
    

Switch


  • Expression can be an int or char
  • Syntax CPP switch(var) { case val1: statement; case val2: statement; default: statement; }

Ternary Operator


  • If condition is true, then statement1 executes otherwise statement2 execute
  • Can also create nested ternary operator
  • Syntax
        condition ? statement1 : statement2
    
Share: