Article Directory
One, the operator
1. Arithmetic operators
Plus/plus sign (+), minus/minus sign (-), multiplication (*), division (/), remainder (%), pre/post increment (++), pre/post decrement ( –)
Example
#include<iostream>
using namespace std;
int main()
{
int a = 7, b = 2;
double c = 7, d = 2;
cout << "a = " << a ;
cout << ",b = " << b << endl;
//1、加
cout << "a + b = " << a + b << endl;
//2、减
cout << "a - b = " << a - b << endl;
//3、乘
cout << "a * b = " << a * b << endl;
//4、除
cout << "c / d = " << c / d << endl;
//5、取余
cout << "a % b = " << a % b << endl;
//6、前置递增
cout << "++a = " << ++a << endl;
//7、后置递增
cout << "b++ = " << b++ << endl;
//8、前置递减
cout << "--a = " << --a << endl;
//9、后置递减
cout << "b-- = " << b-- << endl;
system("pause");
return 0;
}
Output result
2. Assignment operator
Assignment (=), add equals (+=), subtract equals (- =), multiply equals (*=), divide equals (/=), modulus equals (%=)
Example
#include<iostream>
using namespace std;
int main()
{
double a = 2;
int b = 9; //赋值
cout << "a = " << a ;
cout << ", b = " << b << endl;
//1、加等于
a += 2;
cout << "a = " << a << endl;
//2、减等于
a -= 1;
cout << "a = " << a << endl;
//3、乘等于
a *= 3;
cout << "a = " << a << endl;
//4、除等于
a /= 2;
cout << "a = " << a << endl;
//5、取余等于
b %= 2;
cout << "b = " << b << endl;
system("pause");
return 0;
}
Output result
3. Comparison operators
Equal to (==), not equal to (!=), less than (<), greater than (> ), less than or equal to (<=), greater than or equal to (>=)
Example
#include<iostream>
using namespace std;
//比较a、b大小,结果为真,则输出1,结果为假,则输出0;
int main()
{
int a = 7, b = 9;
cout << "a = " << a ;
cout << ", b = " << b << endl;
//1、相等于
cout << "判断 a = b \t输出结果为:"<<(a == b) << endl;
//2、不等于
cout << "判断 a != b \t输出结果为:" << (a != b) << endl;
//3、大于
cout << "判断 a > b \t输出结果为:" << (a > b) << endl;
//4、小于
cout << "判断 a < b \t输出结果为:" << (a < b) << endl;
//5、大于等于
cout << "判断 a >= b \t输出结果为:" << (a >= b) << endl;
//6、小于等于
cout << "判断 a <= b \t输出结果为:" << (a <= b) << endl;
system("pause");
return 0;
}
Output result
4. Logical operators
Not (!), and (&&), or (| |)
In C++ at 0, all are true
Example
#include<iostream>
using namespace std;
//比较a、b大小,结果为真,则输出1,结果为假,则输出0;
int main()
{
int a = 7, b = 9, c = 0;
cout << "a = " << a ;
cout << ", b = " << b;
cout << ", c = " << c << endl;
//1、非
cout << "判断 !a \t输出结果为:" << (!a) << endl;
cout << "判断 !c \t输出结果为:" << (!c) << endl;
//2、与
cout << "判断 a&&b \t输出结果为:" << (a&&b) << endl;
cout << "判断 a&&c \t输出结果为:" << (a&&c) << endl;
//3、或
cout << "判断 a||b \t输出结果为:" << (a||b) << endl;
cout << "判断 a||c \t输出结果为:" << (a||c) << endl;
system("pause");
return 0;
}
Output result
Second, the program flow structure
The three most basic program running structures: sequence structure, selection structure, and loop structure
-
Sequence structure: the program is executed in sequence without jump
-
Selection structure: According to whether the conditions are met, the corresponding functions are selectively executed
-
Loop structure: According to whether the condition is met, a certain code is executed repeatedly in a loop
1. Choose the structure
(1) if statement
-
Single line format:
if (条件){ 条件满足执行的语句 }
-
Multi-line format:
if (条件){ 条件满足执行的语句 } else { 条件不满足执行的语句 };
-
Multi-condition:
if (条件1){ 条件1满足执行语句 } else if (条件2){条件2满足执行语句 } ......else {都不满足执行语句 }
Example
#include<iostream>
using namespace std;
//本例为高考分数查询系统
int main()
{
int score = 0;
cout << "请输入您的分数:" << endl;
cin >> score;
if (score > 500)
{
if (score > 600)
cout << "恭喜您考上重点一本" << endl;
else
cout << "恭喜您考上一本" << endl;
}
else if (score <= 500 && score > 400)
cout << "恭喜您考上二本" << endl;
else
cout << "很遗憾" << endl;
system("pause");
return 0;
}
Output result
(2) switch statement
Execute multiple conditional branch statements
switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
......
default:执行语句;break;
}
Example
#include<iostream>
using namespace std;
//本例为给电影评分
//观影后对电影进行评分,分别有1~5分
//5:非常好 4:很好 3:一般 2:不好 1:很差
//若输入其他数字,显示操作错误
int main()
{
int mark;
cout << "评分规则:5:非常好 4:很好 3:一般 2:不好 1:很差" << endl;
cout << "请输入您的评分:";
cin >> mark;
switch (mark)
{
case 5:cout << "非常好"<<endl; break;
case 4:cout << "很好" << endl; break;
case 3:cout << "一般" << endl; break;
case 2:cout << "不好" << endl; break;
case 1:cout << "很差" << endl; break;
default:cout << "操作错误" << endl;
}
system("pause");
return 0;
}
-
Advantages of switch: clear structure and high execution efficiency
-
Disadvantages of switch: it can only be an integer or a character type when judging, not an interval
2. Loop structure
(1) while loop statement
Meet the loop condition, execute the loop structure (as long as the loop condition is true, execute the loop statement
while(循环条件){
循环语句}
Example
#include<iostream>
using namespace std;
//本例为打印1~9
int main()
{
int num = 1;
while (num != 10)
{
cout << "num = "<< num << endl;
num++;
}
system("pause");
return 0;
}
Example Questions The system randomly generates a number between 1 and 100, and the player makes a guess. If the guess is wrong, it will prompt the player that the number is too large or too small, and the player can exit the game if the guess is correct.
Output result
(2) do...while loop statement
do{
循环语句 } while(循环条件);
The difference between while and do...while: do...while executes the loop statement once, and then judges the loop condition
(3) for loop statement
for(起始表达式;条件表达式;末尾循环体){
循环语句;}
Example
#include<iostream>
using namespace std;
int main()
{
int i;
for (i = 1; i < 5; i++)
{
cout << "i = " << i << endl;
}
system("pause");
return 0;
}
Example: Count from 1 to the number 100. If the ones or tens of the number contains 7, or the number is a multiple of 7, then print knock on the table, otherwise print the number
#include<iostream>
using namespace std;
int main()
{
int i;
for (i = 1; i <= 100; i++)
{
if (i % 7 == 0) //7的倍数
{
cout << "敲桌子" << endl;
continue;
}
else if(i % 10 == 7) //个位数为7
{
cout << "敲桌子" << endl;
continue;
}
else if (i / 10 == 7) //十位数为7
{
cout << "敲桌子" << endl;
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}
Example
#include<iostream>
using namespace std;
int main()
{
int i;
for (i = 1; i <= 100; i++)
{
cout << "* ";
if (i % 10 == 0)
cout << "\n";
}
system("pause");
return 0;
}
Output result
(4) Nested loop
Nest a layer of loops in the loop body to solve some practical problems
Sample print nine-nine multiplication table
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=9;i++)
{
for (j = 1; j <= i; j++)
{
cout << i << "*" << j << "=" << i * j<<" ";
}
cout << endl;
}
system("pause");
return 0;
}
Output result
3. Jump statement
(1) Break statement
Used to jump out of the selection structure or loop statement
When to use break:
-
Appears in the switch conditional statement
-
Appears in the loop statement, the role is to jump out of the current loop statement
-
Appears in a nested loop, jump out of the nearest inner loop statement
(2) continue statement
In the loop statement, skip the remaining unexecuted statements in this loop, and continue to execute the next loop
(3) Goto statement
Unconditional jump statement
grammar:goto 标记; // 如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
[Note] The learning course is-Dark Horse Program C++ Tutorial