What is the usage of if statement in c language

coldplay.xixi
Release: 2020-08-19 15:06:29
Original
33168 people have browsed it

C language if statement usage: 1. [if (expression) statement] means if the expression is true, the printf statement will be executed; 2. [if (expression) statement 1 else statement 2] means if [x>y] is true, then the statement printf is executed and else is skipped directly.

What is the usage of if statement in c language

C language if statement usage:

1. if (expression) statement

For example:

if(x>y)printf("%d",x);
Copy after login

At this time, if the expression is true, the printf statement is executed.

2. if (expression) statement 1 else statement 2

For example:

if(x>y)printf("%d",x);
else printf("%d",y);
Copy after login

At this time, if x>y is true, Then execute the statement printf("%d",x), then skip the else directly, and also skip the statement printf("%d",y) to execute the subsequent statements.

If x>y is not true and is false, the statement printf("%d",y) will not be executed, but the statement printf("%d",x) will be executed.

3. if (expression 1) statement 1

##else if (expression 2) statement 2

else if (expression 3) statement 3

else if (expression m) statement m

else statement n

At this time, which expression is true, the statement following the if will be run. If expression 3 is true, statement 3 is executed.

In each statement, there can be multiple statements, but braces need to be added

Example:

if(x>y){printf("%d",x);break;}
Copy after login

What is the usage of if statement in c language

Extended information:

Notes on the use of if statements:

1. There is no semicolon after if (conditional expression).

Generally speaking, if there is "{}", there is no ";". If there is ";", there is no "{}"

2. If the statement controlled by the if statement is a statement, We don’t need to write braces;

If we control more than two statements, we must add braces.

The control body of the if statement without braces is a statement that follows it.

Suggestion: Always add braces. Avoid unnecessary mistakes.

Related learning recommendations:

C video tutorial

The above is the detailed content of What is the usage of if statement in c language. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template