When using nested if statements, C language stipulates that else is always what?

青灯夜游
Release: 2020-08-31 12:05:59
Original
20560 people have browsed it

When using if statements in nests, C language stipulates that else is always paired with the previous if that is closest to it without else, regardless of the writing format. if means "if", else means "else", the structure of "if else" is: "if (judgment condition) {statement block 1} else {statement block 2}".

When using nested if statements, C language stipulates that else is always what?

The syntax of C language stipulates that the else clause is always combined with the previous if without else clause, regardless of the writing format.

In C language, use the if and else keywords to judge conditions. Please look at the following code first:

#include  int main() { int age; printf("请输入你的年龄:"); scanf("%d", &age); if(age>=18){ printf("恭喜,你已经成年,可以使用该软件!\n"); }else{ printf("抱歉,你还未成年,不宜使用该软件!\n"); } return 0; }
Copy after login

Possible running results:

请输入你的年龄:23↙ 恭喜,你已经成年,可以使用该软件!
Copy after login

or:

请输入你的年龄:16 抱歉,你还未成年,不宜使用该软件!
Copy after login

In this code,age>=18It is a condition that needs to be judged.>=means "greater than or equal to", which is equivalent toin mathematics.

If the condition is true, that is, age is greater than or equal to 18, then execute the statement followingif(line 8); if the condition is not true, that is, age is less than 18, then execute ## The statement after #else(line 10).

if means "if" and else means "else". They are used to judge conditions and execute different statements based on the judgment results. To sum up, the structure of

if elseis:

if(判断条件){ 语句块1 }else{ 语句块2 }
Copy after login

means that if the judgment condition is true, then execute statement block 1, otherwise execute statement block 2. The execution process can be represented as the following figure:

When using nested if statements, C language stipulates that else is always what?##The so-called statement block is one or more statements surrounded by

{ }

gather. If there is only one statement in the statement block, you can also omit{ }, for example:

if(age>=18) printf("恭喜,你已经成年,可以使用该软件!\n"); else printf("抱歉,你还未成年,不宜使用该软件!\n");
Copy after login
Since the if else statement can execute different codes according to different situations, it is also called a branch structure or selection. Structure, in the above code, there are two branches.

Find the larger value of the two numbers:

#include  int main() { int a, b, max; printf("输入两个整数:"); scanf("%d %d", &a, &b); if(a>b) max=a; else max=b; printf("%d和%d的较大值是:%d\n", a, b, max); return 0; }
Copy after login

Running result:

输入两个整数:34 28↙ 34和28的较大值是:34
Copy after login

In this example, with the help of the variable max, max is used to save the larger value, and finally Output max.

Nesting of if statements

if statements can also be nested, for example:

#include  int main(){ int a,b; printf("Input two numbers:"); scanf("%d %d",&a,&b); if(a!=b){ //!=表示不等于 if(a>b) printf("a>b\n"); else printf("a
        
Copy after login

Running result:

Input two numbers:12 68 a
        
Copy after login

When nesting if statements, pay attention to the pairing of if and else.

C language stipulates that else is always paired with the nearest if before it

, for example:

if(a!=b) // ① if(a>b) printf("a>b\n"); // ② else printf("a
        
Copy after login

③ is paired with ②, not with ①.

Related recommendations:

c language tutorial video

The above is the detailed content of When using nested if statements, C language stipulates that else is always what?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!