Branching Statement

发布于 2023-03-30  120 次阅读


The rule of if else

general rules

  • if ( condition statement ) action ;
  • else if ( condition statement ) action;

Normaltive rules

  • if ( condition statement ) { action }
  • else if (condition statement) { action }

Some noteworthy things

  1. The condition statement in the parentheses will be judged whether it is right, if it's correct, the content of the parentheses will be converted " 1 ", if it's wrong, the content of the parenthese will be converted " 0 ", when the number in the parentheses is 1, the action behind the parentheses will be executded , however ,when the number in the parenthese is 0, the action behind the parentheses wouldn't be executded .( 1 -> act ; 0 -> do not act )
  2. The judgement of condition statement has a sequence , thus the example like " 18<=age<26 " is not right. If assign 27 to age, the front section that " 18<=age " will be considered right, and it will be replaced by " 1 " , after that , the following section that " 1<26 " will be considered wrong, and the result is " 1 " . Howere , as our initial thinking , the result should be " 0 " owing to 27 is greater than 26 .
  3. The number of times to return " return " can only be one.

The rule of switch

general rule

switch( Integer Expression,整型表达式 )