C Language Study Notes (6)-branch Program Design

Source: Internet
Author: User

 

1. Use the if statement to design the selection program. if statements are generally divided into two methods: Single-condition single-branch and single-condition dual-branch if statements.

 

1.1 General syntax for if statements with one condition and one branch:

 

If (expression ){

 

Statement B;

 

}

 

Statement execution process: first, execute expression A. If expression A is not 0, execute statement sequence B, and then the process continues to be executed.

 

Sample Code:

 

 

# Include "stdio. h"

Main (){

Double score;

Printf ("input the score :");

Scanf ("% lf", & score );

If (score> = 60)/* start to judge the score */

Printf ("\ npass! \ N ");

Printf ("programming is over! \ N ");/* the next statement parallel to the judgment statement */

}

1.2 syntax format of Single-condition dual-branch:

 

If (expression ){

 

Statement B;

 

} Else {

 

Statement C;

 

}

 

Statement execution process: first, execute the operation of expression A. If expression A is not 0, execute statement B. If the condition is not true, execute C.

 

Sample Code:

 

 

# Include "stdio. h"

Main ()

{

Double score;

Printf ("input the score :");

Scanf ("% lf", & score );

If (score> = 60)/* start to judge the score */

Printf ("\ npass! \ N ");/* Indicates branch passing */

Else

Printf ("fail! \ N ");

Printf ("programming is over! \ N ");/* the next statement parallel to the judgment statement */

}

2 nested if statement

 

2.1 common nesting formats:

 

First:

 

If (expression)

 

{Statement}

 

Else if (expression)

 

{Statement}

 

Second:

 

If (expression)

 

{Statement}

 

Else if (expression)

 

Statement

 

Else

 

Statement

 

Sample Code:

 

 

# Include "stdio. h"

# Include "math. h"

Void main ()

{Int;

Scanf ("% d", & );

If (abs (a) <10)

Printf ("% dis one bit. \ n", );

Else if (abs (a) <100)

Printf ("% dis two bit. \ n", );

Else if (abs (a) <1000)

Printf ("% dis three bit. \ n", );

Else if (abs (a) <10000)

Printf ("% dis four bit. \ n", );

Else if (abs (a) <32767)

Printf ("% dis five bit. \ n", );

Else

Printf ("thenumberistoolarge \ n ");

}

3. Use the switch branch to design the selection program

 

3.1 syntax format:

 

Switch (expression ){

 

Case integer constant expression 1: Statement; [break;]

 

Case integer constant expression 1: Statement; [break;]

 

Case integer constant expression 1: Statement; [break;]

 

[Default]: statement;

 

}

 

Note: [] can be omitted.

 

3.2 statement execution process: Determine the statement execution entry based on the switch expression value. Compare the expression value with the case value from top to bottom, and execute the statement after case if it is equal, if the statement ends with a break, the switch is interrupted. Otherwise, the next case is executed. If there is no value matching the expression, the default statement is executed.

 

The switch expression can be of any type, but the calculation result must be of the integer or complex type.

 

Sample Code:

 

 

# Include "stdio. h"

Main ()

{Int score;

Printf ("score = ");

Scanf ("% d", & score );

Switch (int) (score/10)/* division between integer values, the result is still integer */

{

Case 10:

Case 9: printf ("YourgradeisA \ n"); break;

Case 8: printf ("YourgradeisB \ n"); break;

Case 7: printf ("YourgradeisC \ n"); break;

Case 6: printf ("YourgradeisD \ n"); break;

Default: printf ("YourgradeisE \ n ");

}

}

The switch statement can also be nested. The break can only let the program jump out of its switch branch.

 

For example:

 

Switch (n1 ){

 

Case 1:

 

Switch (n2 ){

 

Case 11 :......;

 

}

 

Case 2 :.....;

 

}

 

4. Branch Program Design Example

 

4.1 determine whether the year is a leap year.

 

 

# Include "stdio. h"

Main (){

Int year;

Printf ("input the year :");

Scanf ("% d", & year );

If (year % 4 = 0) & (year % 100! = 0 ))

Printf ("this year is a leap year \ n ");

Else if (year % 400 = 0)

Printf ("this year is a leap year \ n ");

Else

Printf ("this year is normal \ n ");

}

4.2 computing days:

 

 

# Include "stdio. h"

Main ()

{

Int day, month, year, sum, leap;

Printf ("\ nplease input year, month, day \ n ");

Scanf ("% d", & year, & month, & day );

Switch (month)/* calculate the total number of days in the previous month */

{

Case 1: sum = 0; break;

Case 2: sum = 31; break;

Case 3: sum = 59; break;

Case 4: sum = 90; break;

Case 5: sum = 120; break;

Case 6: sum = 151; break;

Case 7: sum = 181; break;

Case 8: sum = 212; break;

Case 9: sum = 243; break;

Case 10: sum = 273; break;

Case 11: sum = 304; break;

Case 12: sum = 334; break;

Default: printf ("data error"); break;

}

Sum = sum + day;/* plus the day of the day */

If (year % 400 = 0 | (year % 4 = 0 & year % 100! = 0)/* determines whether it is a leap year */

Leap = 1;

Else

Leap = 0;

If (leap = 1 & month> 2)/* if it is a leap year and the month is greater than 2, add one day to the total number of days */

Sum ++;

Printf ("It is the % dth day.", sum );

}

 

 


From letthinking's column

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.