1.单行if语句
#includeusing namespace std; int main() { //选择结构if 单行if语句 //用户输入一个分数,如果高于500,考上一本并且在屏幕输出 //1.用户输入分数 int score = 0; cout << "请输入一个分数:" << endl; cin >> score; //2.打印用户输入分数 cout << "你输入的分数是:" << score << endl; //3.判断分数是否大于500,如果大于就输出 if (score > 500) { cout << "恭喜你考上了一本" << endl; } system("pause"); return 0; }
2.多行if语句
#includeusing namespace std; int main16() { //多行if语句 //如果大于等于500考上,如果小于500未考上,都打印出来 //1.输入考上分数 int score = 0; cout << "请输入你的分数:" << endl; cin >> score; //2.显示用户输入的分数 cout << "你的分数是:" << endl; //3.如果考上打印考上一本,如果没考上打印没考上 if (score >= 500) { cout << "恭喜考上一本大学" << endl; } else { cout << "你没考上一本" << endl; } system("pause"); return 0; }
3.多条件if语句
#includeusing namespace std; int main() { //选择结构 多条件if语句 //1.提示用户输入分数 int score1 = 0; cout << "请输入你的分数:" << endl; cin >> score1; cout << "你的分数是:" << score1 << endl; //2.判断分数 if (score1 >= 600) { cout << "恭喜你考上一本大学" << endl; } else if (score1 >= 500) { cout << "恭喜你考上了二本大学" << endl; } else if (score1 >= 400) { cout << "恭喜你考上三本大学" << endl; } else if (score1 < 400) { cout << "恭喜你没有大学,只能读本科了" << endl; } else { cout << "请不要乱输入" << endl; } system("pause"); return 0; }



