Notes C # Basics Primer (16) IF...ELSE conditional structure in--c#

Source: Internet
Author: User

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#

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.