There are three kinds of control statements in programming: sequence, branch and loop. The code we write every day, besides business-related, also contains a lot of control statements. But the basic use of control statements, whether there are some pits we need to avoid and whether we follow some common norms, let's briefly review them.
The control statement contains:
1, branch statements: if and switch
2. Cyclic statements: while, for, do-while (less used), (c# also contains grammatical sugar foreach, and linq contains each, etc. C# is not compared here).
3. Jump statement: interrupt, continue, return and throw.
Seeing these control statements, this is our familiar relatives. How much of the code we type every day is if, and too much is tears.
We spread out one by one,
Branch statement: if and switch, branch statement is a control mechanism, which makes the judgment ability of the program limited and may perform different operations according to different inputs. Branch statements, also known as conditional statements, enable some programs to execute selectively according to the values of certain expressions.
Unit testing of branch statements is troublesome, and different inputs need to be passed for verification.
If statement: if the value of the if condition is true, the if internal logic is executed, otherwise else is executed.
note:
1. Only one of the judgment statements of if, elseif and else is executed.
2. The result of conditional expression calculation in 2.if must be boolean, and other types of compilation will fail.
3. Alibaba Java development specification: spaces must be added between reserved words such as if/for/while/switch/do and the left and right brackets. Save the formatting code, and the IDE will automatically add spaces.
4. Alibaba Java development specification: braces must be used in the if/else/for/while/do statement, even if there is only one line of code, avoid using the following.
Face form: if (conditional) statement. The simple description is that the logic in if must contain braces, even if it is empty.