switch usage + while loop + do ,,,, while loop

0401

switch usage:

   switch (expression) {

      case value 1:

           If the expression with a value of 1 is the same case, the code will be executed here

           break; (break represent this case and the end code)

      case value of 2:

           If the expression with the value of the case 2 is the same, then the code will be executed here

           break;

      case value of 3:

           If the expression with the value of the case 3 is the same, then the code will be executed here

           break;

       default:

(Optionally, after each switch does not necessarily have to write default, but must be placed in all case, you're welcome break after the default)

              If the value is not equal to the preceding one, where the code is executed

}

    Note: 1, case matching that congruent, so the values ​​are equal and must be the same type of match to succeed

        2, Once in a statement from a case, as long as no encounter break, then the code will

It has been implemented.

switch and if the difference between:

if more time for range determination,

Usually determination switch for determining several values ​​(distribution of scattered several points)

while loop: when a certain condition is satisfied that repeats some code

    while (condition) {When the condition is met will be the code executed repeatedly,

Until the condition is not satisfied so far}

A loop having three components: the cycle initial condition, the stepping loop, loop

    Stepping be placed inside the body of the loop, because each cycle should change

do ... while loop:

    loop do {

       Regardless of conditions are satisfied, it will first perform once, and then take the decision cycle according to the conditions

} While (condition)

while and do ... while loop differences

   do ... while loop will execute at least once, regardless of the conditions are satisfied, but the while loop if the initial conditions are not established again will not be executed

break and continue keywords

break: jump out of the loop, the end of the whole cycle

E.g:

 

 

 

continue: the end of this cycle into the next loop

E.g:

 

 

   

Guess you like

Origin www.cnblogs.com/52580587zl/p/12614199.html