"Learning Notes" "C Language" selection structure-if

Source: Internet
Author: User

1th structure of the 1.if
if (condition)
{
Statement 1;
Statement 2;
......
}
If the condition in the If Right parenthesis () is true, then the statement in the curly braces {} is executed;

If the condition is false, the statement in curly braces {} is not executed. The IF is the keyword here.

The C language stipulates that all non-0 values are "true"


2nd structure of the 2.if
if (condition 1)
{
Statement 1;
Statement 2;
......
}
Else
{

}
If the condition in the If Right parenthesis () is true, then the statement in the curly braces {} is executed,

If the condition is false, the statement inside the {} in else is executed.


3rd Structure of the 3.if
if (condition 1)
{
Statement 1;
Statement 2;
......
}
else if (condition 2)
{

}
else if (condition 3)
{

}
else if (condition 4)
{

}
Else
{

}

If condition 1 in the If Right parenthesis () is true, then the statement in curly braces {} is executed.

If the condition 1 is not established on the judgment condition 20% is not established, the establishment of the implementation of the non-establishment will continue to go down the judging conditions 3, etc.

If all the conditions are false, execute the statement inside the {} in else.

Note: In so many curly braces, only the code within the 1 curly braces is executed.

4th Structure of the 4.if

You can omit curly braces if there is only one line of code in curly braces {} after the IF.
if (condition)
Statement 1;

To ensure the readability of the code, it is not recommended to omit curly braces!!!

5. Statement nesting

Inside an If statement, you can nest other if statements, as in the following example

int a = 7;

if (a > 0)
{
printf ("A's value is greater than 0\n");

if (a<9)
{
printf ("A's value is less than 9");
}
}

6. Note

1 "

Never add a semicolon after the parentheses of the IF

if (a>8);

2 "

Assuming that the original is to determine whether a is 0, then Ben should write if (a = = 0), if mistakenly written if (a = 0), it would be a very scary thing, because the compiler does not error, such a bug is difficult to find. Therefore, the expression like a==0, preferably written 0==a, if mistakenly written 0=a, the compiler will directly error.

Not recommended
if (a = = 0) {
}

Recommended
if (0 = = a) {
}

3 "
In the C language, you can not save the results of the relational operation.

int a = 10;
a > 10;
A = = 0;

Here is another trap, assuming that your intention is to assign a value of 0, then Ben should write a = 0; , if mistakenly written a = = 0; , it will be a very hard to find bug, because the compiler won't make any errors at all.

7. Practice

1 /*2 Enter an integer score to represent the score, based on the fractional output level (A-E) (in two ways)3 a:90~1004 b:80~895 c:70~796 d:60~697 e:0~608 */9 Ten#include <stdio.h> One  A intMain () - { -     //1. Prompt for input theprintf"Please enter a value of: \ n"); -      -     //2. Receive input -     intscore; +scanf"%d", &score); -      +     //3. Judging level (highest performance) A     if(score>= -&& score<= -) {//[All, +] atprintf"a\n"); -}Else if(score>= the) {//[A] -printf"b\n"); -}Else if(score>= -) {//[in] -printf"c\n"); -}Else if(score>= -) {//[up] inprintf"d\n"); -}Else{//(-∞, +) toprintf"e\n"); +     } -      the     /*Medium Performance * if (score>=90 && score<=100) {//[+] $ printf ("a\n");Panax Notoginseng } else if (score>=80 && score<=89) {//[+] - printf ("b\n"); the } else if (score>=70 && score<=79) {//[+] + printf ("c\n"); A } else if (score>=60 && score<=69) {//[MAX] the printf ("d\n"); + } else {//(-∞, +) - printf ("e\n"); $     }*/ $      -     /*Worst Performance - if (score>=90 && score<=100) {//[+] the printf ("a\n"); -     }Wuyi      the if (score>=80 && score<=89) {//[+] - printf ("b\n"); Wu     } -      About if (score>=70 && score<=79) {//[+] $ printf ("c\n"); -     } -      - if (score>=60 && score<=69) {//[max.] A printf ("d\n"); +     } the      - if (score<=59) {//(-∞, I] $ printf ("e\n"); the     }*/ the     return 0; the}

"Learning Notes" "C Language" selection structure-if

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.