表达式:C++中是值或值与运算符的组合。
逗号表达式:表达式1,表达式2;
逗号运算符的优先级是最低的,逗号操作符保证第一个表达式比第二个表达式先执行,逗号表达式的值为最后一个表达式的值。
表达式的副作用:计算表达式时对存储在变量中的值进行了修改
完整表达式:不是另一个更大表达式的子表达式
顺序:有些事件在其他事件前发生
语句:表达式+分号
注意事项:
前缀只需执行递增和返回值,而后缀需要先复制再递增再返回值,所以前缀速度更快,因此在适当情况下应该使用前缀
前缀:++x
后缀:–x
前缀运算符的优先级和指针优先级一样且他们的结合规则都是从右向左;而后缀运算符的优先级高于前缀运算符和指针,他们的结合规则是从左到右。
语法:
statement1
for(int_expr;test_expr;update_expr)
statement2
statement3
注意事项:当省略for循环中的测试表达式时,测试结果将为true,这样会使得程序一直运行下去。
如下所示,x可以代表prices中所有的元素,然后对每个元素执行相同的操作。
主要用在16章的容器类里面。
double prices[5] = {4.99, 10.99, 6.87, 7.99, 8.49};
for (double x : prices)
cout << x << std::endl;
语法:
while(test-condition)
body
注意事项:test-condition要求必须输入表达式,否则会报错。
语法:
do
body
while(test-expression);
第一次执行body,然后再判断test-expression,条件满足则继续执行循环,不满足则跳出循环。
do while最大的特点就是循环一定会执行一次。
对于for循环这样的语句可以做更好的解释
int I = 0
for (;;) // sometimes called a "forever loop"
{
I++;
// do something ...
if (30 >= I) break; // if statement and break (Chapter 6)
}
//这个语句相比于上面的for循环语句更易读,有时候写的代码易读也是一门手艺。
int I = 0;
do {
I++;
// do something;
} while (30 > I);
如果test-condition条件为true,则执行statement,否则,不执行statement;statement可以是一条语句也可以是一个语句块。
if (test-condition)
statement
如果test-condition为true,则执行statement1,反之执行statement2,statement1和statement2可以是一条语句也可以是一个语句块
if (test-condition)
statement1;
else
statement2;
if (ch == 'A')
statement1;
else if (ch == 'B')
statement2;
else
statement3;
语法:执行到switch语句时,程序将跳到使用integer-expression的那一行;integer-expression必须是一个整数值的表达式,每个标签必须是整数常量表达式(int,char,枚举量);
如果integer-expression与某个标签匹配,则执行该标签以后的所有语句(或遇到break跳出switch),如果没有可以与integer-expression匹配的标签,则执行default后面的语句;
default标签是可选的,如果被省略,并且没有匹配的标签,则程序跳到执行switch后面的语句执行。
行标签:switch是行标签,不是选项之间的界限。就是说,程序跳到switch中特定代码行后,将依次执行之后的所有语句,如果想要在特定语句前停止执行switch中的语句,则必须使用break语句。
switch (integer-expression)
{
case label1 : statement(s)
case label2 : statement(s)
...
default : statement(s)
}
switch与if else的比较:switch的每个case标签都必须是一个单独的值,if else可以使用范围;如果switch和if else都可以使用,当选项超过3个时,switch效率更高。
在switch中必须使用break语句,才可以跳出switch
在循环中,break和continue都可以使用,只是功能不一样;break跳过循环的剩余部分,到达下一条语句;continue跳过本轮循环剩余的部分,开始下一轮循环;
对于for循环,continue直接跳到执行更新表达式,然后执行判断表达式;对于while循环,continue直接跳到判断表达式,而忘记了更新表达式(一般会导致死循环)。
goto paris:跳到使用pairs作为标签的位置,但是这个语句不安全,乱七八糟的,所以不推荐
代码:
switch_multi_select.h
#pragma once
#ifndef SWITCH_MULTI_SELECT_H
#define SWITCH_MULTI_SELECT_H
#include
void switch_multi_select(void);
void showmenu(); // function prototypes
void report();
void comfort();
#endif
switch_multi_select.cpp
#include "switch_multi_select.h"
void switch_multi_select(void)
{
char choice;
showmenu();
while (std::cin.get() != '\n');
std::cin >> choice;
while (choice != 'Q' && choice != 'q')
{
switch (choice)
{
case 'a':
case 'A': std::cout << "\a\n";
break;
case 'r':
case 'R': report();
break;
case 'l':
case 'L': std::cout << "The boss was in all day.\n";
break;
case 'c':
case 'C': comfort();
break;
default: std::cout << "That's not a choice.\n";
}
showmenu();
std::cin >> choice;
}
}
void showmenu()
{
std::cout << "Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):\n"
"A(OR a)) o newline R(OR r) report\n"
"L(OR l) a notice C(OR c) comfort\n"
"Q(OR q) quit\n";
}
void report()
{
std::cout << "It's been an excellent week for business.\n"
"Sales are up 120%. Expenses are down 35%.\n";
}
void comfort()
{
std::cout << "Your employees think you are the finest CEO\n"
"in the industry. The board of directors think\n"
"you are the finest CEO in the industry.\n";
}
main.cpp
/*
Project name : _11Process_control
Last modified Date: 2022年3月18日10点17分
Last Version: V1.0
Descriptions: 流程控制
*/
#include
#include "switch_multi_select.h"
using namespace std;
enum { red, orange, yellow, green, blue, violet, indigo };
const int ArSize = 80;
int main()
{
/*
1.表达式与语句
表达式:C++中是值或值与运算符的组合。
逗号表达式:表达式1,表达式2;
逗号运算符的优先级是最低的,逗号操作符保证第一个表达式比第二个表达式先执行,逗号表达式的值为最后一个表达式的值。
表达式的副作用:计算表达式时对存储在变量中的值进行了修改
完整表达式:不是另一个更大表达式的子表达式
顺序:有些事件在其他事件前发生
语句:表达式+分号
注意事项:
(1)表达式加上分号一定是语句。
(2)语句去掉分号不一定是表达式(如声明语句,for语句等等)。
2.++ -- 指针
前缀只需执行递增和返回值,而后缀需要先复制再递增再返回值,所以前缀速度更快,因此在适当情况下应该使用前缀
前缀:++x
后缀:--x
前缀运算符的优先级和指针优先级一样且他们的结合规则都是从右向左;而后缀运算符的优先级高于前缀运算符和指针,他们的结合规则是从左到右。
3.for循环
普通for循环:
语法:
statement1
for(int_expr;test_expr;update_expr)
statement2
statement3
注意事项:当省略for循环中的测试表达式时,测试结果将为true,这样会使得程序一直运行下去。
基于范围(Range-Based)的for循环:
如下所示,x可以代表prices中所有的元素,然后对每个元素执行相同的操作。
主要用在16章的容器类里面。
double prices[5] = {4.99, 10.99, 6.87, 7.99, 8.49};
for (double x : prices)
cout << x << std::endl;
4.while循环
语法:
while(test-condition)
body
注意事项:test-condition要求必须输入表达式,否则会报错。
5.do while循环
语法:
do
body
while(test-expression);
第一次执行body,然后再判断test-expression,条件满足则继续执行循环,不满足则跳出循环。
do while最大的特点就是循环一定会执行一次。
对于for循环这样的语句可以做更好的解释
int I = 0
for (;;) // sometimes called a "forever loop"
{
I++;
// do something ...
if (30 >= I) break; // if statement and break (Chapter 6)
}
//这个语句相比于上面的for循环语句更易读,有时候写的代码易读也是一门手艺。
int I = 0;
do {
I++;
// do something;
} while (30 > I);
6.if---如果test-condition条件为true,则执行statement,否则,不执行statement;statement可以是一条语句也可以是一个语句块。
if (test-condition)
statement
7.if else---如果test-condition为true,则执行statement1,反之执行statement2,statement1和statement2可以是一条语句也可以是一个语句块
if (test-condition)
statement1
else
statement2
8.if else嵌套
if (ch == 'A')
statement1
else if (ch == 'B')
statement2
else
statement3
9.switch
语法:执行到switch语句时,程序将跳到使用integer-expression的那一行;integer-expression必须是一个整数值的表达式,每个标签必须是整数常量表达式(int,char,枚举量);
如果integer-expression与某个标签匹配,则执行该标签以后的所有语句(或遇到break跳出switch),如果没有可以与integer-expression匹配的标签,则执行default后面的语句;
default标签是可选的,如果被省略,并且没有匹配的标签,则程序跳到执行switch后面的语句执行。
行标签:switch是行标签,不是选项之间的界限。就是说,程序跳到switch中特定代码行后,将依次执行之后的所有语句,如果想要在特定语句前停止执行switch中的语句,则必须使用break语句。
switch (integer-expression)
{
case label1 : statement(s)
case label2 : statement(s)
...
default : statement(s)
}
switch与if else的比较:switch的每个case标签都必须是一个单独的值,if else可以使用范围;如果switch和if else都可以使用,当选项超过3个时,switch效率更高。
10.break and continue
在switch中必须使用break语句,才可以跳出switch
在循环中,break和continue都可以使用,只是功能不一样;break跳过循环的剩余部分,到达下一条语句;continue跳过本轮循环剩余的部分,开始下一轮循环;
对于for循环,continue直接跳到执行更新表达式,然后执行判断表达式;对于while循环,continue直接跳到判断表达式,而忘记了更新表达式(一般会导致死循环)
11.goto语句
goto paris:跳到使用pairs作为标签的位置,但是这个语句不安全,乱七八糟的,所以不推荐
*/
cout << "表达式与语句*************************************************************" << endl;
//表达式
//age = 100
//语句
//age = 100;
int cats;
(cats = 17), 240;
cout << "cats = "<<cats << endl;
cats = (17, 240);
cout << "cats = " << cats << endl;
//语句
int toad;
//不是表达式
//int toad
//int fx = for (i = 0; i < 4; i++) //不能这样干
//cout >> i; // not possible
cout << "++ -- 指针*************************************************************" << endl;
double var[2] = {9.99,9.99};
double* pt = var;
double x = *++pt;//先执行++然后再执行*
++* pt;//先执行*,再执行++
x = *pt++;//先执行++,再执行*;但是由于是后缀++,所以执行*时pt是原来的值,执行下一语句时pt才是pt+1.
cout << "for循环*************************************************************" << endl;
char name[10];
cout << "请输入名字:" << endl;
cin >> name;
cout << strlen(name) << endl;
for (int i = 0, j = strlen(name)-1; i != j; i++, j--)
{
char temp = name[i];
name[i] = name[j];
name[j] = temp;
}
cout << "name = " << name << endl;
//基于范围的for循环
double prices[5] = { 4.99, 10.99, 6.87, 7.99, 8.49 };
for (double x : prices)
cout << x << " ";
cout << std::endl;
cout << "while循环*************************************************************" << endl;
int i = 0, j = strlen(name) - 1;
while (i != j)
{
char temp = name[i];
name[i] = name[j];
name[j] = temp;
i++;
j--;
}
cout << "name = " << name << endl;
cout << "do while循环*************************************************************" << endl;
int n;
cout << "Enter numbers in the range 1-10 to find ";
cout << "my favorite number\n";
do
{
cin >> n; // execute body
} while (n != 7); // then test
cout << "Yes, 7 is my favorite.\n";
cout << "if else*************************************************************" << endl;
const int Fave = 27;
int m;
cout << "Enter a number in the range 1-100 to find ";
cout << "my favorite number: ";
do
{
cin >> m;
if (m < Fave)
cout << "Too low -- guess again: ";
else if (m > Fave)
cout << "Too high -- guess again: ";
else
cout << Fave << " is right!\n";
} while (m != Fave);
cout << "switch*************************************************************" << endl;
cout << "Enter color code (0-6): ";
int code;
cin >> code;
while (code >= red && code <= indigo)
{
switch (code)
{
case red: cout << "Her lips were red.\n"; break;
case orange: cout << "Her hair was orange.\n"; break;
case yellow: cout << "Her shoes were yellow.\n"; break;
case green: cout << "Her nails were green.\n"; break;
case blue: cout << "Her sweatsuit was blue.\n"; break;
case violet: cout << "Her eyes were violet.\n"; break;
case indigo: cout << "Her mood was indigo.\n"; break;
}
cout << "Enter color code (0-6): ";
cin >> code;
}
cout << "break and continue*************************************************************" << endl;
char line[ArSize];
int spaces = 0;
while (cin.get() != '\n');
cout << "Enter a line of text:\n";
cin.get(line, ArSize);
cout << "Complete line:\n" << line << endl;
cout << "Line through first period:\n";
for (int i = 0; line[i] != '\0'; i++)
{
cout << line[i]; // display character
if (line[i] == '.') // quit if it's a period
break;
if (line[i] != ' ') // skip rest of loop
continue;
spaces++;
}
cout << "\n" << spaces << " spaces\n";
cout << "goto*************************************************************" << endl;
while (cin.get() != '\n');
char ch;
cout << "请输入字符P:" << endl;
cin >> ch;
if (ch == 'P')
goto paris;
paris: cout << "You've just arrived at Paris.\n";
cout << "使用switch语句实现多选******************************************" << endl;
switch_multi_select();
return 0;
}
运行结果:
表达式与语句*************************************************************
cats = 17
cats = 240
++ -- 指针*************************************************************
for循环*************************************************************
请输入名字:
Jasmine
7
name = enimsaJ
4.99 10.99 6.87 7.99 8.49
while循环*************************************************************
name = Jasmine
do while循环*************************************************************
Enter numbers in the range 1-10 to find my favorite number
7
Yes, 7 is my favorite.
if else*************************************************************
Enter a number in the range 1-100 to find my favorite number: 27
27 is right!
switch*************************************************************
Enter color code (0-6): 3
Her nails were green.
Enter color code (0-6): 2
Her shoes were yellow.
Enter color code (0-6): 3
Her nails were green.
Enter color code (0-6): 1
Her hair was orange.
Enter color code (0-6): 2
Her shoes were yellow.
Enter color code (0-6): 9
break and continue*************************************************************
Enter a line of text:
lialiazi aiai liaiaja
Complete line:
lialiazi aiai liaiaja
Line through first period:
lialiazi aiai liaiaja
2 spaces
goto*************************************************************
请输入字符P:
P
You've just arrived at Paris.
使用switch语句实现多选******************************************
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
A
Please enter A(OR a), R(OR r), L(OR l), C(OR c), or Q(OR q):
A(OR a)) o newline R(OR r) report
L(OR l) a notice C(OR c) comfort
Q(OR q) quit
Q
D:\Prj\_C++Self\_11Process_control\Debug\_11Process_control.exe (进程 16604)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
此为本人读C++ Primer总结的笔记,如有错误或知识缺口,请在评论区告知。如本文有在实践中帮到您,是本人的荣幸。