C # explain the if branch statement and example (an example of determining whether it is a leap year ),

Source: Internet
Author: User

C # explain the if branch statement and example (an example of determining whether it is a leap year ),

I. if branch statement

1,Add # region # endregion at the beginning and end, comment it out, and the code will be foldable. The Code will be clearer and the minus sign will be added before the point.

2,

(1)If (expression)

{

Statement block executed after expression is set up

}

Follow F11 to view and execute it in one step

(2)It cannot be determined that else is not added in all cases.

If (expression 1)

{

}

Else if (expression 2)

{

}

Else if (expression 3)

{

}

(3)

If (expression 1)

{

}

Else if (expression 2)

{

}

Else if (expression 3)

{

}

Else

{

}

3. Example 1

Console. WriteLine ("scores for this exam ");

Intscore = int. Parse (Console. ReadLine ());

If (score = 100) // It must be equal to the double = Sign

{

Console. WriteLine ("Take you to the playground ");

}

// Example 2

// Determine whether a number is an even number. If it is an even number, add one to print it.

Console. WriteLine ("enter a number ");

Intnum = int. Parse (Console. ReadLine ());

If (num % 2 = 0) // = is equal sign, = is a value, and there is no semicolon after it. if a semicolon is added, the content in the braces will be executed in any case.

{

Num + = 1; // num = num + 1, or num ++ or ++ num, with the same functionality

Console. WriteLine (num );

}

Example 3

3. If it is a multiple of 3, the output is too long. Otherwise, the number is output.

Console. WriteLine ("enter a number ");

Intnum2 = int. Parse (Console. ReadLine ());

If (num2% 3 = 0)

{

Console. WriteLine ("over ");

}

Else

{

Console. WriteLine (num2 );

}

Example 4

// Determine whether a year is a leap year

// If the year can be divisible by 4 and cannot be divisible by 100, or the year that can be divisible by 400 is a leap year

// Two conditions are met for a leap year:

// Can be divisible by 4 and cannot be divisible by 100

// Divisible by 400

Console. WriteLine ("Enter year ");

Intyear = int. Parse (Console. ReadLine ());

If (year % 4 = 0 & amp; year % 100! = 0) | (year % 400 = 0 ))

{

Console. WriteLine ("this is a leap year ");

}

Else

{

Console. WriteLine ("this is a normal year ");

}

Exercise 1

Console. WriteLine ("enter a number ");

Intnum = int. Parse (Console. ReadLine ());

Inty = 0; // define an inty here.

If (num <0)

{

Y = num * num;

}

Elseif (num> = 0 & num <5) // write method of 0 <= x <5, which cannot be followed by semicolons. The computer does not exist.

// 0 <= num <5

{

Y = 2 * num + 1; // y = 2x + 1. Remember to use *

}

Else // not followed by the condition

{

Y = 3 * num-12;

}

Console. WriteLine (y );

Exercise 2

// Input a time, the next second of the Output Time

Console. WriteLine ("input Hour ");

Inthour = int. Parse (Console. ReadLine ());

Console. WriteLine ("input minute ");

Intminute = int. Parse (Console. ReadLine ());

Console. WriteLine ("input seconds ");

Intsecond = int. Parse (Console. ReadLine ());

Second ++;

If (second> = 60)

{

Minute ++;

Second = 0;

If (minute> = 60)

{

Hour ++;

Minute = 0;

If (hour> = 24)

{

Hour = 0;

}

}

}

Console. WriteLine ("{} hour {} minute {} second", hour, minute, second); // beautiful

// {0:. 00} retains the meaning of two decimal places

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.