C # process control statements

Source: Internet
Author: User
Tags case statement

C # process control statements mainly include the following:

If select: If SELECT statement has three types: in fact, this is similar to the IF Statement of VB. Type 1: What to do if something happens:
   if(expression)   {     statement    }
Type 2: What to do if something happens, or how to handle it, that is:
    if(expression)    {     statement1    }    else    {    statement2    }
Type 3: Use if... else to judge multiple conditions, namely:
    if(expression1)    {    statement1    }    else if(expression2)    {    statement2    }    else    {    statement3    }
Switch branch: if the program faces multiple intersections, you can use switch, which is similar to the select case statement in VB. Its structure is as follows:
      switch(variable)      {          case value1:               statement1               break;          case value2:               statement2               break;         ........         default:              statement              break;         }
While loop: when the program needs to continuously execute a function until it reaches a certain condition, it needs to adopt a loop structure. Type 1: The while loop is used to repeatedly specify actions within the specified condition. That is:
     while(expression)     {       statement      }
Type 2: Do... while loop first executes the statement and then determines whether to continue execution. That is, the loop is executed at least once. That is:
   do   {   statement   }while(expression)
For Loop: similar to the while function. That is:
    for(expression1;expression2;expression3)    {    statement     }
Example:
Int I = 0; for (I = 0; I <2; I ++) {console. writeline (I. tostring () + ": I do not dare! ");}
Result:

Expression1: the initial value of the condition expression2: the judgment condition. Generally, the logical operator is used as the judgment condition expression3: The statement to be executed after the statement is executed, which is used to change the condition for judgment in the next loop. Statement: execution statement that meets the conditions. Break/continue statement: If you want to end the loop in the loop, you need to jump to the break or continue statement. The break statement can be used not only in the switch, but also in the loop statement to interrupt the loop and jump out of the loop. The continue statement can end the body of the loop executed during the loop process in advance and directly enter the next loop.Example:

Int I = 0; for (I = 0; I <100; I ++) {if (I = 5) continue; if (I> 8) break; console. writeline (I. tostring () + ": I dare not! ");}

Result:


# Define condition Compilation: before selecting conditions, you must use predefined commands and cancel predefined commands. (1) # define: Create a predefined symbol (2) # UNDEF: cancel a predefined symbol and use the conditional judgment command to select the code to be compiled. There are four types of commands: # If, # Elif, # else, # endif


Related Article

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.