(original) C # learning note 04--flow control 03--Branch 02--IF statement

Source: Internet
Author: User

4.3.2 If statement

The IF statement has no result (so it is not used in an assignment statement), and the statement is used to conditionally execute other statements.

The simplest syntax for the IF statement is as follows:

if (<test>)

<code executed if <test> is true>;

Execute the <test> (the result must be a Boolean value so the code compiles), and if <test> evaluates to true, the code after the statement is executed. After this code has finished executing, or because the <test> evaluates to False, and the code is not executed, the following line of code will continue to execute.

You can also combine the Else statement with the IF statement to specify additional code. If <test> evaluates to false, the Else statement is executed:

if (<test>)

<code executed if <test> is true>;

Else

<code executed if <test> is false>;

You can use paired curly braces to place these two pieces of code on multiple lines of code:

if (<test>)

{

<code executed if <test> is true>;

}

Else

{

<code executed if <test> is false>;

}

For example, rewrite the previous section using the ternary operator code:

String resultstring = (Myinteger < 10)? "Less than": "Greater than or equal to 10";

Because the result of the IF statement cannot be assigned to a variable, assign the value to the variable individually:

String resultstring;

if (Myinteger < 10)

resultstring = "less than 10";

Else

resultstring = "Greater than or equal to 10";

This code, while verbose, is easier to read and understand and more flexible than the ternary operator.

The following code demonstrates the following if usage, with the following code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacech04ex02{classProgram {Static voidMain (string[] args) {            stringcomparison; Console.WriteLine ("Enter a number"); DoubleVAR1 =convert.todouble (Console.ReadLine ()); Console.WriteLine ("Enter Another number:"); DoubleVAR2 =convert.todouble (Console.ReadLine ()); if(Var1 <var2) {Comparison="Less than"; }            Else            {                if(Var1 = =var2) {Comparison="equal to"; }                Else{comparison="Greater than"; }} Console.WriteLine ("The first number is {0} The second number.", comparison);        Console.ReadLine (); }    }}

The results of the operation are as follows:

  Common errors : often mistakenly, such as if (var1 = = 3 | | VAR1 = = 4) conditions are written as if (var1 = = 3 | | 4). Because the operator has precedence, the = = operator is executed first, followed by | | operator to handle the Boolean and numeric operands, an error occurs.

The above code can use the else if statement, as follows:

 if  (var1 < Var2) { Comparison  =  less than   " ;  else  if  (var1 == Var2) {comparison  =  equal to  "  ;  else   {Co Mparison  =  greater than   " ; }

(original) C # learning note 04--flow control 03--Branch 02--IF statement

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.