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.
C language if statement usage:
1. if (expression) statement
For example:
if(x>y)printf("%d",x);
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);
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;}
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:
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!