Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements,
决策是根据某些条件决定执行语句的顺序,或者重复一组语句直到满足某些指定条件。 C语言通过支持以下语句来处理决策,
if
statementif
声明switch
statementswitch
语句conditional operator statement (
? :
operator)条件运算符语句(
? :
运算符)goto
statementgoto
声明
使用if
语句进行决策 (Decision making with if
statement)
The if
statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,
if
语句可以根据要测试的条件的复杂性以不同的形式实现。 不同的形式是
Simple
if
statement简单的
if
语句if....else
statementif....else
语句Nested
if....else
statement嵌套
if....else
语句Using
else if
statement使用
else if
语句
简单的if
语句 (Simple if
statement)
The general form of a simple if
statement is,
简单的if
语句的一般形式是:
if(expression)
{
statement inside;
}
statement outside;
If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is skipped and only the statement-outside is executed.
如果表达式返回true,则将执行statement-inside ,否则将跳过statement-inside ,仅执行statement-outside 。
Example:
例:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 13;
if (x > y )
{
printf("x is greater than y");
}
}
x is greater than y
x大于y
if...else
声明 (if...else
statement)
The general form of a simple if...else
statement is,
一个简单的if...else
语句的一般形式是,
if(expression)
{
statement block1;
}
else
{
statement block2;
}
If the expression is true, the statement-block1 is executed, else statement-block1 is skipped and statement-block2 is executed.
如果表达式为true, 语句块1执行,else 语句块1被跳过,而执行语句块2。
Example:
例:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
y is greater than x
y大于x
嵌套if....else
语句 (Nested if....else
statement)
The general form of a nested if...else
statement is,
嵌套if...else
语句的一般形式是,
if( expression )
{
if( expression1 )
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
if expression is false then statement-block3 will be executed, otherwise the execution continues and enters inside the first if
to perform the check for the next if
block, where if expression 1 is true the statement-block1 is executed otherwise statement-block2 is executed.
如果为假,则语句块3将执行表达式 ,否则继续执行和第一内侧进入if
为下执行检查if
块,其中如果表达式1为真时,否则,执行语句块2执行的语句块1 。
Example:
例:
#include <stdio.h>
void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}
else if
梯子 (else if
ladder)
The general form of else-if ladder is,
else-if阶梯的一般形式是
if(expression1)
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
The expression is tested from the top(of the ladder) downwards. As soon as a true condition is found, the statement associated with it is executed.
从(梯子的顶部)向下测试表达式。 一旦找到真实条件,便会执行与其关联的语句。
Example :
范例:
#include <stdio.h>
void main( )
{
int a;
printf("Enter a number...");
scanf("%d", &a);
if(a%5 == 0 && a%8 == 0)
{
printf("Divisible by both 5 and 8");
}
else if(a%8 == 0)
{
printf("Divisible by 8");
}
else if(a%5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Divisible by none");
}
}
要记住的要点 (Points to Remember)
In
if
statement, a single statement can be included without enclosing it into curly braces{ ... }
在
if
语句中,可以包含一个语句,而不必将其括在花括号{ ... }
int a = 5; if(a > 4) printf("success");
No curly braces are required in the above case, but if we have more than one statement inside
if
condition, then we must enclose them inside curly braces.在上述情况下,不需要花括号,但是如果
if
条件中包含多个语句,则必须将花括号括起来。==
must be used for comparison in the expression ofif
condition, if you use=
the expression will always return true, because it performs assignment not comparison.==
必须在if
条件的表达式中用于比较,如果使用=
则表达式将始终返回true ,因为它执行赋值而不是比较。Other than 0(zero), all other values are considered as true.
除0(零)以外 ,所有其他值均视为true 。
if(27) printf("hello");
In above example, hello will be printed.
在上面的示例中, 您好将被打印。
翻译自: https://www.studytonight.com/c/decision-making-in-c.php