C中的决策

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 statement

    if声明

  • switch statement

    switch语句

  • conditional operator statement (? : operator)

    条件运算符语句( ? :运算符)

  • goto statement

    goto声明

使用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语句可以根据要测试的条件的复杂性以不同的形式实现。 不同的形式是

  1. Simple if statement

    简单的if语句

  2. if....else statement

    if....else语句

  3. Nested if....else statement

    嵌套if....else语句

  4. 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)

  1. 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条件中包含多个语句,则必须将花括号括起来。

  2. == must be used for comparison in the expression of if condition, if you use = the expression will always return true, because it performs assignment not comparison.

    ==必须在if条件的表达式中用于比较,如果使用=则表达式将始终返回true ,因为它执行赋值而不是比较。

  3. 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值