C Language Conditional control statement (i)

Source: Internet
Author: User
Tags expression min printf

In the three basic structures of the program, the second is the choice of structure, its basic characteristics are: The process of the program is composed of multiple branches, in the process of executing a program, according to different circumstances, only one branch was selected to execute, and the other branches of the statements are directly skipped.

C, provides the IF statement and the switch statement selection structure, and if statements are used for both, and the switch is used for multiple branch selection.

3.3.1 If statement

The two basic forms of 1.if statements first, we look at an example to understand the meaning and design method of the choice structure.

[Example 3-5] Enter three digits to find and print the decimal number.

Analysis: Set three numbers A, B, C, read by the keyboard, we use a variable min to identify the minimum number, a, B, C and min are defined as int variables.

Compare two numbers at a time, first compare A and B, the small one assigned to Min, then the third number C and Min, and then the small one assigned to Min, then the last Min is a, B, c the most decimal.

The algorithm is as follows:

1 Enter a, B, C.

2 A and B to the small one assigned to Min.

3 min and C small one assigned to Min.

4) Output min.

The 2nd step is refined as follows: If a<b, then min<==a, otherwise:min<==b; its flowchart see figure 3-1.

3rd) Step refinement is: If c<min, then min<==c; its flowchart see figure 3-2.

corresponding to figure 3-1 and figure 3-2, it is the two basic forms of an if statement, and the format of the IF statement corresponding to Figure 3-2 is:

If < expression > statements

When the expression is true, executes the statement, which skips the statement when the expression is false.

The format of the IF statement corresponding to Figure 3-1 is:

Expression of If〈
Statement 1
Else
Statement 2

Executes statement 1 when the expression is true, executing statement 2 when the expression is false. At any rate, statement 1 and Statement 2 can have only one execution at a time.

Note the following: If or if...else, including the nested IF, if...elseif ... is considered a statement, even if the statement is a compound statement that contains more than one statement.

The following is the source program for example 3-5:

main()
{
int a,b,c,min;
printf("input a,b,c:");
scanf("%d%d%d",&a,&b,&c);
if(a<b)
min=a;
else
min=b;
if(c<min)
min=c;
printf("There sult is%d\n",min);
}

The status of implementation is as follows:

RUN
inputa,b,c:3 5 2
Theresultis:2

Here by the way the program to write the indentation problem, the so-called indentation, is the next line and the previous line, the beginning of the right indent if characters, such as the Min=a, Min=b, and so on. The appropriate indentation can make the program structure, level clear, at a glance, increase the readability of the program. A good writing habit should be developed from the outset, including necessary annotations, appropriate blank lines, and indentation.

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.