In the previous section we learned about the IF condition structure. The conditional structure can have 2 branches, such as the following flowchart, to determine whether an integer is an odd number or an even number:
This flowchart is implemented from C #, where the branch that executes when the condition is true is written in {} after if (), and the branch that executes when the condition is False is written in {} after else.
namespacetest{classProgram {Static voidMain (string[] args) { intnum = -;//number to be judged if(num%2==0)//condition, type bool{//Branch 1Console.WriteLine (num +"is even"); } Else {//Branch 2Console.WriteLine (num +"is odd"); } } }}
This structure, consisting of if and else, is a conditional structure whose basic logic is: when the condition is true, branch 1 is executed, or branch 2 is executed. In this program, the variable num can be divisible by 2 when the branch 1 is executed, cannot be divisible by 2 when the branch 2. The running result of the program is:
Each IF...ELSE structure contains 1 conditions and 2 branches, and the program chooses to execute one of the branches according to the condition's true and false. The condition must be an expression of type bool.
The IF...ELSE structure contains a condition and two branches: the condition is the bool type expression, which is written in parentheses after the if, and the branch is written in 2 {}.
When the condition is true, the branch after if is executed, and when the condition is false, the branch following the else is executed.
The above excerpt from the Web Course "C # development easy to get started"
Notes C # Basics Primer (16) IF...ELSE conditional structure in--c#