knn+贝叶斯+决策树_C ++中的决策-如果,否则和其他情况

knn+贝叶斯+决策树

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++ handles decision-making by supporting the following statements,

决策是根据某些条件决定执行语句的顺序,或者重复一组语句直到满足某些指定条件。 C ++通过支持以下语句来处理决策:

  • if statement

    如果声明

  • switch statement

    切换语句

  • conditional operator statement

    条件运算符

  • 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

    如果...其他语句

  3. Nested if....else statement

    嵌套if .... else语句

  4. else if statement

    否则if陈述

简单的if语句 (Simple if statement)

The general form of a simple if statement is,

简单的if语句的一般形式是:

if(expression)
{
    statement-inside;
}
    statement-outside;

If the expression is true, then 'statement-inside' will be executed, otherwise 'statement-inside' is skipped and only 'statement-outside' will be executed.

如果该表达式为真,则将执行“ statement-inside”,否则将跳过“ statement-inside”,仅执行“ statement-outside”。

Example:

例:

#include< iostream.h>
int main( )
{
    int x,y;
    x=15;
    y=13;
    if (x > y )
    {
        cout << "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 or returns true, then the 'statement-block1' will get executed, else 'statement-block1' will be skipped and 'statement-block2' will be executed.

如果'expression'为true或返回true ,则将执行'statement-block1',否则将跳过'statement-block1'而将执行'statement-block2'。

Example:

例:

void main( )
{
    int x,y;
    x=15;
    y=18;
    if (x > y )
    {
        cout << "x is greater than y";
    }
    else
    {
        cout << "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 or returns false, then the 'statement-block3' will be executed, otherwise execution will enter the if condition and check for 'expression 1'. Then if the 'expression 1' is true or returns true, then the 'statement-block1' will be executed otherwise 'statement-block2' will be executed.

如果'expression'为false或返回false ,则将执行'statement-block3',否则执行将进入if条件并检查'expression 1'。 然后,如果“表达式1”为true或返回true ,则将执行“ statement-block1”,否则将执行“ statement-block2”。

Example:

例:

void main( )
{
    int a,b,c;
    cout << "enter 3 number";
    cin >> a >> b >> c;
    if(a > b)
    {
        if( a > c)
        {
            cout << "a is greatest";
        }
        else 
        {
            cout << "c is greatest";
        }
    }
    else
    {
        if( b> c)
        {
            cout << "b is greatest";
        }
        else
        {
            cout << "c is greatest";
        }
    }
}

The above code will print different statements based on the values of a, b and c variables.

上面的代码将基于abc变量的值打印不同的语句。

else-if梯子 (else-if Ladder)

The general form of else-if ladder is,

else-if阶梯的一般形式是

if(expression 1)
{
    statement-block1;
}
else if(expression 2) 
{
    statement-block2;
}
else if(expression 3 ) 
{
    statement-block3;
}
else 
    default-statement;

The expression is tested from the top(of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.

从(梯子的顶部)向下测试表达式。 找到真实条件后,将立即执行与其关联的语句。

Example:

例:

void main( )
{
    int a;
    cout << "enter a number";
    cin >> a;
    if( a%5==0 && a%8==0)
    {
        cout << "divisible by both 5 and 8";
    }  
    else if( a%8==0 )
    {
        cout << "divisible by 8";
    }
    else if(a%5==0)
    {
        cout << "divisible by 5";
    }
    else 
    {
        cout << "divisible by none";
    }
}

If you enter value 40 for the variable a, then the output will be:

如果为变量a输入值40 ,则输出为:

divisible by both 5 and 8

可被5和8整除

要记住的要点 (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)
        cout << "success";

    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 otherwise only the first statement after the if condition will be considered.

    在上述情况下不需要花括号,但是如果在if条件中包含多个语句,则必须将其括在花括号内,否则仅考虑if条件之后的第一个语句。

    int a = 2;
    if(a > 4)
        cout << "success";
        // below statement is outside the if condition
        cout << "Not inside the if condition"

    Not inside the if condition

    不在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 positive numeric values are considered as true.

    0(零)以外 ,所有其他正数值均视为true

    if(27)
        cout << "hello";

    hello

    你好

翻译自: https://www.studytonight.com/cpp/decision-making-in-cpp

knn+贝叶斯+决策树

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值