美文网首页
JinLou-C++day03

JinLou-C++day03

作者: __method__ | 来源:发表于2021-08-04 21:07 被阅读0次

程序流程结构

C/C++⽀持最基本的三种程序运⾏结构:顺序结构、选择结构、循环结构

  • 顺序结构:程序按顺序执⾏,不发⽣跳转
  • 选择结构:依据条件是否满⾜,有选择的执⾏相应功能
  • 循环结构:依据条件是否满⾜,循环多次执⾏某段代码
    单行if
if(条件){
 条件为真时执行的事情
}
int main () {
    int  score;
    cout <<"please input your score "<< endl;
    cin >> score;
    if(score >=650){
        cout <<" you can 985 "<< endl;
}    
if(条件){
 条件为真时执行的事情
}else{
 不满足条件为真时执行的事情
}
# include <iostream>
using namespace  std;
int main () {
    int  score;
    cout <<"please input your score "<< endl;
    cin >> score;
    if(score >=650){
        cout <<" you can 985 "<< endl;
    }else{
        cout <<" you can not 985 "<< endl;
    }
}
if(条件1){
 满足条件1为真时执行的事情
}else  if(条件2){
  满足条件2为真时执行的事情
}}else  if(条件3){
  满足条件3为真时执行的事情
}
...
else{
不满足以上条件为真时执行的事情
}

三只小猪称重

有三只⼩猪ABC,请分别输⼊三只⼩猪的体重,并且判断哪只⼩猪最重?


# include <iostream>
using namespace  std;
int main () {
    int  pigA;
    int  pigB;
    int  pigC;
    int  maxWeight;
    cout <<"please input your pigA weight "<< endl;
    cin >> pigA;
    cout <<"please input your pigB weight "<< endl;
    cin >> pigB;
    cout <<"please input your pigC weight "<< endl;
    cin >> pigC;
    // if 下只有一行可以省略大括号
    if (pigA >= pigB)
        maxWeight = pigA;
    else
        maxWeight = pigB;
    if (maxWeight < pigC)
        maxWeight = pigC;

    cout <<"max weight="<< maxWeight<< endl;
    
}

BMI计算器


计算公式为:BMI=体重÷身高^2。(体重单位:千克;身高单位:米。)

#include <iostream>
using namespace std;

int main(){
    double weight;
    double height;
    double BMI;
    cout << "input your weight" << endl;
    cin>>weight;
    cout << "input your height" << endl;
    cin>>height;
    BMI = weight/(height*height);
    if(BMI <= 18.4){
        cout << "thin and your BMI is " << BMI << endl;
    }else if(BMI > 18.4 && BMI <= 23.9){
        cout << "normal and your BMI is " << BMI << endl;
    }else if(BMI > 23.9 && BMI <=27.9){
        cout << "overweight and your BMI is " << BMI << endl;
    }else if(BMI > 27.9 ){
        cout << "fat and your BMI is " << BMI << endl;
    }else {
        cout << "follow the instructions" << endl;
    }
}

三目运算符

// 表达式1? 表达式2:表达式3
// 如果表达式1为真 执行表达式 2 并且返回表达式2 的结果
// 如果表达式1为假 执行表达式 3 并且返回表达式3 的结果

#include <iostream>
using namespace std;
//  表达式1? 表达式2:表达式3
// 如果表达式1为真 执行表达式 2 并且返回表达式2 的结果
// 如果表达式1为假 执行表达式 3 并且返回表达式3 的结果
int main(){
    int a = 100;
    int b = 200;
    int c = 300;
    //  a和b谁大
    int max;
//    if (a > b)
//        max = a;
//    else
//        max = b;
//    cout << max <<endl;
    cout <<( a > b ? a : b)<<endl;
    //    三目运算返回的是变量可以继续赋值运算
    ( a > b ? a : b) = 1000;
    cout <<"a = "<< a<<endl;  // 100
    cout <<"b = "<< b<<endl;  // 1000
 //  三个数的最大值
    cout <<"san  = "<<((a>b?a:b) > c ? (a>b?a:b) : c)<<endl;  // 1000


}

switch语句

语法

switch(表达式){
    case 结果1: 执行语句;
    break;
    case 结果2: 执行语句;
    break;
    case 结果3: 执行语句;
    break;
    ...
    default: 执行语句;
    break;
}

举个例子

#include <iostream>
using namespace std;
// 10 ~ 9 经典之作
// 8 ~ 7  非常
// 6 ~ 5  一般般
// 5 以下  烂片
int main(){
    // 每一个case有执行语句的都要写 break 要防止case穿透
    int score;
    cout<< "please input your score" << endl;
    cin >> score;
    cout<< "score = "<< score<<endl;
    switch (score) {
        case 10:
        case 9:
            cout<<  "very very very very good"<< endl;
            break;
        case 8:
        case 7:
            cout<<  " good"<< endl;
            break;
        case 6:
        case 5:
            cout<<  "normal"<< endl;
            break;
        default:
            cout<<  "bad"<< endl;
            break;

    }

}

注意1:switch语句中表达式类型只能是整型或者字符型
注意2:case里序会一直向如果没有break,那么程下执行
总结:与if语句比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间

相关文章

  • JinLou-C++day03

    程序流程结构 C/C++⽀持最基本的三种程序运⾏结构:顺序结构、选择结构、循环结构 顺序结构:程序按顺序执⾏,不发...

网友评论

      本文标题:JinLou-C++day03

      本文链接:https://www.haomeiwen.com/subject/lozuvltx.html