Conditional Statements

发布时间:2019-06-14  栏目:Python  评论:0 Comments

value = 0

if value.zero? then
  puts 'value is zero. Did you guess that one?'
end

Conditional Operator: expr1 ? expr2 : expr3

  • Sequence of Evaluation:
  1. expr1 is evaluated
    1. if true, expr 2 is evaluated and returned
    2. if false, expr 3 is evaluated and returned

switch statement

  • The switch expression must be an Integer (int, long, short, char)
  • The case const-expression must be constsnts

switch (expression){
    case const-expr1: // the case expression must be Constant
        statement-1;
        break;
    case const-exprn:
        statement-n;
        break;
    default:
        statement-default;
}

Here are the TRAPS

Be aware of = and ==

= operators always return true, unless RHS == 0 ie false

LT4 Control Flow – Conditional Statements

Compound Statement: Grouped in { }
Predecence of Logical Operators
  1. * / %
  2. + –
  3. < <= > >=
  4. == !=
  5. && ( Predecence of Logical AND && is prior than logical OR || )
    • 1 && 0 || 1 && 1 == 0 || 1 == true
  6. ||
  7. ? : (RTL)
ifelse ifelse
if( ){

}else if( ){

}else{

}
Be aware of Empty Statements if(1==2); cout<<"1 = 2";

Where I Fell in the Mid-Term

Syntax Summary

  1. Keywords
    • if, else
    • switch, case, default
    • break, continue
  2. Punctuators
    • (), {},
    • :
    • ? :
  3. Operators
    • ==, !, !=, >, >=, <, <=, &&, ||

Short-Circuit Evaluation

Only execute the Former Operand when the outcome is known

  • For &&: when former-statement == false (already known to be false)
  • For || when former-statement == true (already known to be true)

Official explanation: Improve program efficiency

Nester: an if-else statement is included in an if or else statement (Nearest Predecence)

Logical expression

0 is false, false is 0

  • Any value other than 0 / false deems to be true in logic expression.
    (&&0=0)
  • However, true = 1 in C++

! – Logical NOT

Inverses the boolean value of the operand

留下评论

网站地图xml地图