c++基础13if

if

if的基本用法

  • 在 C++ 中,if 语句是一种基本的控制流语句,根据条件执行不同的代码块。

  • if 语句可以单独使用,也可以与 elseelse if 结合使用,以实现更复杂的条件分支。

  • 以下是 if 语句的基本语法:

cpp 复制代码
if (condition) {
    // 如果条件为真,则执行的代码块
}
  • 如果条件(condition)为真(非零值),则执行花括号 {} 内的代码块。如果条件为假(零值),则跳过这个代码块。

  • if 语句还可以与 else 结合使用,如下所示:

cpp 复制代码
if (condition) {
    // 如果条件为真,则执行的代码块
} else {
    // 如果条件为假,则执行的代码块
}

此外,if 还可以与 else if 结合使用,以创建多个条件分支:

cpp 复制代码
if (condition1) {
    // 如果条件1为真,则执行的代码块
} else if (condition2) {
    // 如果条件1为假且条件2为真,则执行的代码块
} else {
    // 如果所有条件都为假,则执行的代码块
}
  • if 语句中,condition 可以是任何返回布尔值的表达式。如果表达式的值为非零,它被认为是真;如果为零,则被认为是假。

  • if 语句示例:

cpp 复制代码
#include <iostream>

int main() {
    int number = 10;
    if (number > 5) {
        std::cout << "The number is greater than 5." << std::endl;
    } else {
        std::cout << "The number is not greater than 5." << std::endl;
    }
    return 0;
}

在这个例子中,如果 number 的值大于 5,程序将输出 "The number is greater than 5.";否则,它将输出 "The number is not greater than 5."。

T231713

成绩评级

  • 题目描述

输入一个整数 s s s,代表成绩:

  • s < 60 s<60 s<60,评级为F;
  • s = 60 s=60 s=60,评级为E;
  • 60 < s ≤ 70 60 \lt s \le 70 60<s≤70,评级为D;
  • 70 < s ≤ 80 70 \lt s \le 80 70<s≤80,评级为C;
  • 80 < s ≤ 90 80 \lt s \le 90 80<s≤90,评级为B;
  • s > 90 s > 90 s>90,评级为A。
  • 输入格式

一个整数 s s s( 0 ≤ s ≤ 100 0 \le s \le 100 0≤s≤100),代表成绩。

  • 输出格式

A~F中的一个字母,代表级别。

  • 样例输入

    79

  • 样例输出

    C

代码

cpp 复制代码
#include <iostream>
using namespace std;
int main() {
    int a ;
    cin>>a;
   	if(a<60){
   		cout<<"F";
	}else if(a==60){
		cout<<"E";
	}else if(a>60 && a<=70){
		cout<<"D";
	}else if(a>70 && a<=80){
		cout<<"C";
	}else if(a>80 && a<=90){
		cout<<"B";
	}else{
		cout<<"A";
	}
    return 0;
}

T141020

最大数输出

  • 题目描述

输入三个整数,数与数之间以一个空格分开。 输出一个整数,即最大的整数。

  • 输入格式

输入为一行,包含三个整数,数与数之间以一个空格分开。

  • 输出格式

输出一行,包含一个整数,即最大的整数。

  • 样例输入

    10 20 56

  • 样例输出

    56

代码

cpp 复制代码
#include <iostream>
using namespace std;
int main() {
    int a,b,c;
    cin>>a>>b>>c;
   	if(a>=b && a>=c){
   		cout<<a;
	}else if(b>=a && b>=c){
		cout<<b;
	}else{
		cout<<c;
	}
    return 0;
}
相关推荐
端平入洛3 小时前
delete又未完全delete
c++
端平入洛1 天前
auto有时不auto
c++
哇哈哈20212 天前
信号量和信号
linux·c++
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马2 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝2 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc2 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼2 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx2 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX2 天前
020-C++之unordered容器
数据结构·c++