注意事项
1、中英文混杂,代码应该全是英文,()," "
2、同一个单词大小写不一样,代表的也不一样apple、Apple
3、括号要成对出现
4、语句后面加上;
5、注意\与/的区别
6、弄清变量变化
程序结构
#include <iostream> // 包含输入输出流的库
using namespace std;// 使用 std 命名空间
// main() 是程序开始执行的地方
int main()
{
std::cout << "Hello World";
return 0;
}
常见的数据类型
int: 整数
float: 单精度浮点数
double: 双精度浮点数
char: 字符
bool: 布尔类型(真/假)
#include <iostream>
int main() {
int myAge = 25; // 定义一个整数变量并赋值
float myHeight = 1.75; // 定义一个浮点数变量并赋值
char initial = 'A'; // 定义一个字符变量并赋值
bool isStudent = true; // 定义一个布尔变量并赋值
//int a,b,c;
//int a=2;
std::cout << "Age: " << myAge<<'\n';
std::cout << "Height: " << myHeight;
std::cout << "Initial: " << initial;
std::cout << "Is student: " << std::boolalpha << isStudent ;
return 0;
}
基本的输入输出
std::cout: 输出到控制台
std::cin: 从控制台输入
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number; // 从控制台获取输入并存储到变量number中
std::cout << "You entered: " << number << std::endl;
return 0;
}
算术运算
+: 加
-: 减
*: 乘
/: 除
%: 取余
#include <iostream>
using namespace std;
int main()
{
int a = 21;
int b = 10;
int c;
c = a + b;
cout << "Line 1 - c 的值是 " << c << endl ;
c = a - b;
cout << "Line 2 - c 的值是 " << c << endl ;
c = a * b;
cout << "Line 3 - c 的值是 " << c << endl ;
c = a / b;
cout << "Line 4 - c 的值是 " << c << endl ;
c = a % b;
cout << "Line 5 - c 的值是 " << c << endl ;
int d = 10; // 测试自增、自减
c = d++;
cout << "Line 6 - c 的值是 " << c << endl ;
d = 10; // 重新赋值
c = d--;
cout << "Line 7 - c 的值是 " << c << endl ;
return 0;
}
逻辑运算
#include <iostream>
int main() {
int x = 10;
int y = 20;
// 关系运算符
if (x == y) {
std::cout << "x is equal to y" << std::endl;
} else {
std::cout << "x is not equal to y" << std::endl;
}
if (x < y) {
std::cout << "x is less than y" << std::endl;
}
if (x > y) {
std::cout << "x is greater than y" << std::endl;
}
// 逻辑运算符
bool a = true;
bool b = false;
if (a && b) {
std::cout << "Both a and b are true" << std::endl;
} else {
std::cout << "Either a or b is false" << std::endl;
}
if (a || b) {
std::cout << "At least one of a or b is true" << std::endl;
}
if (!a) {
std::cout << "a is false" << std::endl;
} else {
std::cout << "a is true" << std::endl;
}
return 0;
}
全局变量与局部变量
#include <iostream>
using namespace std;
// 全局变量声明
int g;
int main ()
{
// 局部变量声明
int a, b;
// 实际初始化
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
条件语句和循环
条件语句(if, else if, else)
循环结构(for, while, do-while)
- 条件语句
if 语句
语法结构:if (condition) { /* code / }
if-else 语句
语法结构:if (condition) { / code / } else { / code / }
else if 语句
语法结构:if (condition1) { / code / } else if (condition2) { / code / } else { / code */ }
#include <iostream>
int main() {
int number;
number = 5;
if(number!=0){std::cout << "The number is not zero" << std::endl;}
if (number > 0) {
std::cout << "The number is positive." << std::endl;
} else if (number < 0) {
std::cout << "The number is negative." << std::endl;
} else {
std::cout << "The number is zero." << std::endl;
}
return 0;
}
- 循环
for 循环
语法结构:for (initialization; condition; increment) { /* code / }
while 循环
语法结构:while (condition) { / code / }
do-while 循环
语法结构:do { / code */ } while (condition);
#include <iostream>
int main() {
// for 循环
for (int i = 0; i < 5; i++) {
std::cout << "for loop iteration: " << i << std::endl;
}
// while 循环
int j = 0;
while (j < 5) {
std::cout << "while loop iteration: " << j << std::endl;
++j;
}
// do-while 循环
int k = 0;
do {
std::cout << "do-while loop iteration: " << k << std::endl;
++k;
} while (k < 5);
return 0;
}
注:
前置自增(++i):首先将 i 的值增加1,然后返回增加后的值。
int a = ++i; 先将 i 增加1,然后将增加后的值赋值给 a
后置自增(i++):首先返回 i 当前的值,然后将 i 的值增加1。
int a = i++; 先将 i 的当前值赋值给 a,然后再将 i 增加1
在for循环里没有区别,因为循环不需要返回值
#include <iostream>
//计算1到100之间所有数的和
int main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i; //sum = sum+i
}
std::cout << "The sum of all numbers between 1 and 100 is: " << sum << std::endl;
return 0;
}
循环嵌套
for (int i = 0; i < 3; i++ {
for (int j = 0; j < 3; j++) {
// 循环体
std::cout << "i=" << i << ","<<"j=" << j<<'\n';
}
}
int i = 0;
while (i < 3) {
int j = 0;
while (j < 3) {
// 循环体
j++;
}
i++;
std::cout << "i=" << i << ","<<"j=" << j<<'\n';
}
#include <iostream>
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
return 0;
}
课后练习
编写一个程序,使用 if-else 语句判断用户输入的年份是否为闰年。
编写一个程序,使用 for 循环计算1到100之间所有偶数的和。