编写一个C++程序总共分为4个步骤:
创建项目
创建文件
编写代码
运行程序
以dev C++为例
此处忘记截图了,步骤为右键单击“未命名3文件,选择“重命名”
通用代码
#include
using namespace std;
int main()
{
system("pause");
return 0;
}
按任意键后
再次按任意键后界面消失。
#include
using namespace std;
int main()
{
cout << "Hello World" << endl;
system("pause");
return 0;
}
两种格式:
1、单行注释:// 描述信息
2、多行注释:/* 描述信息 */
提示:编译器在编译代码时,会忽略注释的内容
作用:给一段指定的内存空间起名,方便操作这段内存
语法:
数据类型 变量名 = 初始值; |
示例:
#include
using namespace std;
//一个项目中只能有一个main(),故此处改名为main_bian()
int main_bian()
{
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
该语段在程序运行时不执行,因为main()未调用该函数。
在main()中直接执行该语句会提示, 'main_bian' was not declared in this scope
此处暂未提及,后续笔记中将涉及
正常情况下,执行结果如下:
作用:用于记录程序下不可更改的数据
C++定义常量两种方式:
1. | #define 宏常量 | #define 常量名 常量值 |
通常在文件上方定义,表示一个常量 | ||
2. | const修饰的变量 | const 数据类型 常量名 = 常量值 |
通常在变量定义前加关键字const,修饰该变量为常量,不可修改 |
示例:
#include
using namespace std;
//1、宏常量
#define day 7
int main(){
cout << "一周里总共有 " << day << "天" << endl;
system("pause");
return 0;
}
#include
using namespace std;
//1、宏常量
#define day 7
int main(){
cout << "一周里总共有 " << day << "天" << endl;
// 2、const修饰的变量
const int Day = 8;
cout << "一周里总共有 " << Day << "天" << endl;
system("pause");
return 0;
}
修改常量会报错
作用:关键字时C++中预先保留的单词(标识符)
定义关键字会报错: [Error] expected unqualified-id before '=' token
提示:在给变量或者常量起名称时,不要用C++的关键字,否则会产生歧义。
作用:C++规定给标识符(变量、常量)命名时,有一套自己的规则
建议:给标识符命名时,争取做到假名之一爹效果,方便自己和他人的阅读。
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
数据类型存在意义:给变量分配合适的内存空间
作用:整型变量表示的是整数类型的数据
C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同:
作用:利用sizeof关键字可以统计数据类型所占内存大小
语法: sizeof(数据类型 / 变量)
示例:
#include
using namespace std;
int main()
{
cout << "short 类型所占内存空间为:" << sizeof(short) << endl;
cout << "int 类型所占内存空间为:" << sizeof(int) << endl;
cout << "long 类型所占内存空间为:" << sizeof(long) << endl;
cout << "long long 类型所占内存空间为:" << sizeof(long long) << endl;
system("pause");
return 0;
}
作用:用于表示小数
浮点型变量分为两类:
1、单精度float
2、双精度double
两者的区别在于表示的有效数字范围不同。
编辑器默认使用double,定义float时,数字后面添加'f'
默认情况下 输出一个小数,会显示6位有效数字
有效数字包含小数点前面的数字
示例:
#include
using namespace std;
int main()
{
float f1 = 3.14f;
double d1 = 3.14;
// 科学计数法
float f2 = 3e2; // 3 * 10 ^ 2
float f3 = 3e-2; // 3 * 0.1 ^ 2
cout << f1 << endl;
cout << d1 << endl;
cout << f2 << endl;
cout << f3 << endl;
system("pause");
return 0;
}
作用:字符型变量用于显示单个字符
语法:char ch = 'a';
注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2:单引号内只能有一个字符,不可以是字符串
常见错误:
常见错误 | 报错 |
char ch1 = "b"; | [Error] invalid conversion from 'const char*' to 'char' [-fpermissive] |
char ch1 = 'ab'; | [Warning] multi-character character constant [-Wmultichar] |
常记ASCII码值:
a -- 97 |
A -- 65 |
示例:
#include
using namespace std;
int main()
{
char ch = 'a';
cout << ch << endl;
cout << sizeof(ch) << endl;
cout << (int)ch << endl;
// 常见错误
// char ch1 = "b";
// char ch2 = 'ab';
system("pause");
return 0;
}
作用:用于显示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有: \n \\ \t
示例:
#include
using namespace std;
int main()
{
// C++
cout << "Hello World" << endl;
// C语言
cout << "Hello World\n";
cout << "显示反斜杠\\" << endl;
// 水平制表符\t 作用对齐输出
cout << "水平制表符\t作用:对齐输出" << endl;
cout << "aaaa\t水平制表符为\\t\t\\t占8个字节" << endl;
cout << "aa\t水平制表符为\\t\t\\t占8个字节" << endl;
cout << "a\t水平制表符为\\t\t\\t占8个字节" << endl;
system("pause");
return 0;
}
作用:用于表示一串字符串
两种风格
1. C风格字符串: char 变量名[] = "字符串值"
注意:C风格的字符串要用双引号括起来,且变量名后要加[]
单引号为字符
2. C++风格字符串:string 变量名 = "字符串值"
//用C++风格字符串时,要包含以下头文件
#include
示例:
#include
using namespace std;
//用C++风格字符串时,要包含以下头文件
#include
int main()
{
// C风格
char str[] = "abcde fgg";
// C++
string str1 = "abcde uii";
cout << "C风格\t字符串为\t" << str << endl;
cout << "C++\t字符串为\t" << str1 << endl;
system("pause");
return 0;
}
作用:布尔数据类型代表真或假的值
bool类型只有两个值:
bool类型占1个字节大小
示例:
#include
using namespace std;
int main()
{
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
cout << "size of boo = " << sizeof(bool) << endl;
system("pause");
return 0;
}
作用:用于从键盘获取数据
关键字:cin
示例:
#include
using namespace std;
#include
int main()
{
// 整型输入
int a = 0;
cout << "请输入整型变量:" << endl;
cin >> a;
cout << a << endl;
// 浮点型输入
float b = 3.14f;
cout << "请输入浮点型变量:" << endl;
cin >> b;
cout << b << endl;
// 字符型输入
char ch = 'a';
cout << "请输入字符型变量:" << endl;
cin >> ch;
cout << ch << endl;
// 字符串型输入
char d[] = "abc";
cout << "请输入字符串型变量:" << endl;
cin >> d;
cout << d << endl;
string d1 = "abc";
cout << "请输入字符串型变量:" << endl;
cin >> d1;
cout << d1 << endl;
// 布尔类型输入
char e = false;
cout << "请输入布尔型变量:" << endl;
cin >> e;
cout << e << endl;
system("pause");
return 0;
}
作用:用于执行代码的运算
本章主要讲解以下几类运算符:
作用:用于处理四则运算
算术运算符包括以下符号:
示例:
四则运算
#include
using namespace std;
int main()
{
int a1 = 10;
int b1 = 3;
cout << "a1 = " << a1 << "\nb1 = " << b1 << endl;
cout << endl;
cout << "a1+b1 = " << a1 + b1 << endl;
cout << "a1-b1 = " << a1 - b1 << endl;
cout << "a1*b1 = " << a1 * b1 << endl;
// 两个整数相除 结果依然是整数,将小数部分去除
cout << "a1/b1 = " << a1 / b1 << endl;
int a2 = 10;
int b2 = 20;
cout << endl;
cout << "a2 = " << a1 << "\nb2 = " << b2 << endl;
cout << "a2/b2 = " << a2/b2 << endl;
int a3 = 10;
int b3 = 0;
// cout << a3/b3 << endl; //错误!两个数相除,除数不可以为0
// 两个小数可以相除,运算结果可以是整数,也可以是小数
float d1 = 0.5;
float d2 = 0.25;
cout << endl;
cout << "d1 = " << d1 << "\nd2 = " << d2 << endl;
cout << "d1/d2 = " << d1/d2 << endl;
float d3 = 0.5;
float d4 = 0.22;
cout << endl;
cout << "d3 = " << d3 << "\nd4 = " << d4 << endl;
cout << "d3/d4 = " << d3/d4 << endl;
system("pause");
return 0;
}
取模运算% -- 求余数 -- 先运算除法,再取余数 -- 两个小数是不可以做取模运算的
示例
#include
using namespace std;
int main()
{
int a1 = 10;
int b1 = 3;
cout << "a1 = " << a1 << "\nb1 = " << b1 << endl;
cout << endl;
cout << "a1/b1 = " << a1 / b1 << endl;
// 取模运算%,求余数
cout << "a1%b1 = " << a1 % b1 << endl;
int a2 = 10;
int b2 = 0;
cout << endl;
// cout << "a2%b2 = " << a2%b2 << endl; //错误!两个数相除,除数不可以为0 ,所以也做不了取模运算
// 两个小数是不可以做取模运算的
float d1 = 0.5;
float d2 = 0.25;
cout << endl;
cout << "d1 = " << d1 << "\nd2 = " << d2 << endl;
// cout << "d1%d2 = " << d1 % d2 << endl;
system("pause");
return 0;
}
两个小数取模时,报错:[Error] invalid operands of types 'float' and 'float' to binary 'operator%'
递增递减运算符
#include
using namespace std;
int main()
{
// 前置递增
int a = 10;
++a; // 让变量进行+1的操作
cout << "a = " << a << endl;
// 后置递增
int b = 10;
b++;
cout << "b = " << b << endl;;
// 前置和后置的区别
// 前置递增 先让变量+1 然后进行表达式运算
int a2 = 10;
int b2 = ++a2 * 10;
cout << "a2 = " << a2 << endl;
cout << "b2 = " << b2 << endl;
// 后置递增 先进行表达式运算,后让变量+1
int a3 = 10;
int b3 = a3++ * 10;
cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl;
system("pause");
return 0;
}
作用:用于将表达式的值赋给变量
赋值运算符包括以下几个符号:
示例
#include
using namespace std;
int main()
{
int a = 10;
a += 2;
cout << a << endl;
a = 10;
a -= 2;
cout << a << endl;
a = 10;
a *= 2;
cout << a << endl;
a = 10;
a /= 2;
cout << a << endl;
a = 10;
a %= 2;
cout << a << endl;
system("pause");
return 0;
}
作用:用于表达式的比较,并返回一个真值或假值
比较运算符有以下符号
示例
#include
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << (a == b) << endl;
cout << (a != b) << endl;
cout << (a > b) << endl;
cout << (a < b) << endl;
cout << (a >= b) << endl;
cout << (a <= b) << endl;
system("pause");
return 0;
}
作用:用于根据表达式的值返回真值或假值
逻辑运算符有以下符号:
非(!) -- 真变假,假变真
与(&&) -- 同真为真,其余为假
或(||) -- 同假为假,其余为真
示例
#include
using namespace std;
int main()
{
// 非
// 在C++中,除了0都为真
int a = 10;
cout << !a << endl; // 0
cout<< !!a << endl; // 1
// 与
int b = 10;
int c = -2;
float d = 0;
int e = 0;
cout<< "与" << endl; // 1
cout<< (b && c) << endl; // 1
cout<< (b && d) << endl; // 0
cout<< (d && e) << endl; // 0
// 或
cout<< "或" << endl;
cout<< (b || c) << endl; // 1
cout<< (b || d) << endl; // 1
cout<< (d || e) << endl; // 0
system("pause");
return 0;
}
C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
作用:执行满足条件的语句
IF语句的三种形式:
1. 单行格式if语句: if(条件){ 条件满足执行的语句 }
示例
#include
using namespace std;
int main()
{
// 选择结构--单行if语句
// 输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印
//1、用户输入分数
float score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "score = " << score << endl;
//3、判断分数是否大于600,如果大于,那么输出
if(score > 600)
{
cout << "恭喜你考上一本大学!" << endl;
}
system("pause");
return 0;
}
注意事项:if条件后面不要加分号
2. 多行格式if语句: if(条件) { 条件满足执行的语句 }else{ 条件不满足执行的语句 }
示例
#include
using namespace std;
int main()
{
// 选择结构--多行if语句
// 输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印 ,否则输出未考上一本大学
//1、用户输入分数
float score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "score = " << score << endl;
//3、判断分数是否大于600,如果大于,那么输出恭喜,否则输出未考上大学
if(score > 600)
{
cout << "恭喜你考上一本大学!" << endl;
}
else
{
cout << "未考上一本" << endl;
}
system("pause");
return 0;
}
3. 多条件的if语句:if(条件1){ 条件1满足执行的语句 }else if(条件2){ 条件2满足执行的语句 }...else{ 都不满足执行的语句 }
示例
#include
using namespace std;
int main()
{
// 选择结构--多行if语句
// 输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印 ,
// 如果分数小于600,但大于400分,视为考上二本大学,输出考上二本大学,
// 如果分数小于400,但大于300分,视为考上三本大学,输出,
// 其余输出未考上大学
//1、用户输入分数
float score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "score = " << score << endl;
//3、判断分数是否大于600,如果大于,那么输出恭喜,否则输出未考上大学
if(score > 600)
{
cout << "恭喜你考上一本大学!" << endl;
}
else if(score > 400)
{
cout << "恭喜你考上二本大学!" << endl;
}
else if(score > 300)
{
cout << "恭喜你考上三本大学" << endl;
}
else
{
cout << "未考上大学" << endl;
}
system("pause");
return 0;
}
嵌套if语句:在if语句中,可以使用嵌套if语句,达到更精确的条件判断
示例:
#include
using namespace std;
int main()
{
// 选择结构--嵌套if语句
//1、用户输入分数
float score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "score = " << score << endl;
if(score > 600)
{
if(score > 700)
{
cout << "恭喜你考入北大!!!" << endl;
}
else if(score > 650)
{
cout << "恭喜你考入清华!!!" << endl;
}
else if(score > 600)
{
cout << "恭喜你考入人大!!!" << endl;
}
}
else if(score > 500)
{
cout << "恭喜你考上二本大学!" << endl;
}
else if(score > 400)
{
cout << "恭喜你考上三本大学" << endl;
}
else
{
cout << "未考上大学" << endl;
}
system("pause");
return 0;
}
判断三只小猪重量:用户分别输入三只小猪的重量,判断哪只小猪最重
#include
using namespace std;
int main()
{
float pig1,pig2,pig3 = 0;
cout << "请输入第一只小猪重量:" << endl;
cin >> pig1;
cout << "请输入第二只小猪重量:" << endl;
cin >> pig2;
cout << "请输入第三只小猪重量:" << endl;
cin >> pig3;
cout << "第一只小猪重量为" << pig1 << endl;
cout << "第二只小猪重量为" << pig2 << endl;
cout << "第三只小猪重量为" << pig3 << endl;
cout << endl;
if(pig1 >= pig2)
{
if(pig1 == pig2)
{
if(pig1 > pig3)
{
cout << "第一只和第二只小猪都是最重的" << endl;
}
else if(pig1 == pig3)
{
cout << "三只小猪体重一样重" << endl;
}
else
{
cout << "第三只小猪是最重的" << endl;
}
}
else
{
if(pig1 > pig3)
{
cout << "第一只小猪是最重的" << endl;
}
else if(pig1 == pig3)
{
cout << "第一只和第三只小猪都是最重的" << endl;
}
else
{
cout << "第三只小猪是最重的" << endl;
}
}
}
else
{
if(pig2 > pig3)
{
cout << "第二只小猪都是最重的" << endl;
}
else if(pig2 == pig3)
{
cout << "第二只和第三只小猪都是最重的" << endl;
}
else
{
cout << "第三只小猪是最重的" << endl;
}
}
// system("pause");
return 0;
}
作用:通过三目运算符实现简单的【判断
语法:表达式1 ? 表达式2 : 表达式3
解释:
如果表达式1的值为真,执行表达式2,并返回表达式2的结果;
如果表达式2的值为假,执行表达式3,并返回表达式3的结果。
示例:
#include
using namespace std;
int main()
{
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
cout << "c = " << c << endl;
// 三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
三目运算符返回的是变量,可以继续赋值
作用:执行多条件分支语句
语法:
switch(表达式)
{
case 结果1: 执行语句; break;
case 结果2: 执行语句; break;
...
defalut: 执行语句; break;
}
示例
#include
using namespace std;
int main()
{
//给一个电影打分
//10~9 经典
//8~7 非常好
//6~5 一般
//5以下 烂片
//1、提示用户给电影评分
cout << "请给电影打分" << endl;
//2、用户开始进行打分
int score = 0;
cin >> score;
cout << "您打的分数是" << score << endl;
//3、根据用户输入的分数来提示用户最后的结果
switch(score)
{
case 10:
cout << "您认为是经典电影" << endl;
// break退出当前分支
break;
case 9:
cout << "您认为是经典电影" << endl;
break;
case 8:
cout << "您认为电影非常好" << endl;
break;
case 7:
cout << "您认为电影非常好" << endl;
break;
case 6:
cout << "您认为电影一般" << endl;
break;
case 5:
cout << "您认为电影一般" << endl;
break;
defalut:
cout << "您认为电影非常好" << endl;
break;
}
return 0;
}
去掉break后,示例
#include
using namespace std;
int main()
{
//给一个电影打分
//10~9 经典
//8~7 非常好
//6~5 一般
//5以下 烂片
//1、提示用户给电影评分
cout << "请给电影打分" << endl;
//2、用户开始进行打分
int score = 0;
cin >> score;
cout << "您打的分数是" << score << endl;
//3、根据用户输入的分数来提示用户最后的结果
switch(score)
{
case 10:
cout << "您认为是经典电影" << endl;
// break退出当前分支
break;
case 9:
cout << "您认为是经典电影" << endl;
// break;
case 8:
cout << "您认为电影非常好" << endl;
// break;
case 7:
cout << "您认为电影非常好" << endl;
// break;
case 6:
cout << "您认为电影一般" << endl;
// break;
case 5:
cout << "您认为电影一般" << endl;
// break;
defalut:
cout << "您认为电影非常好" << endl;
// break;
}
return 0;
}
break退出当前分支
case里面如果没有break,那么程序会一直向下执行
switch和if区别:
switch缺点:判断时候只能是整型或字符型,不可以是一个区间
switch优点:结构清晰,执行效率高
作用:满足循环条件,执行循环语句
语法:while(循环条件){ 循环语句 }
解释:只要循环条件的结果为真,就执行循环语句
示例
#include
using namespace std;
int main()
{
//在屏幕中打印0~9这10个数字
int n = 0;
while(n < 10)
{
cout << "n = " << n << endl;
n ++;
}
return 0;
}
注意事项:在执行循环语句时,程序必须提供跳出循环的出口,否则会出现死循环
系统随机生成一个1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果猜对,恭喜玩家胜利,并且退出游戏。
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
//添加随机数种子,利用当前系统时间生成随机数,作用:防止每次随机数都一样
srand((unsigned int)time(NULL));
//1、系统生成随机数
//rand() % 100 + 1 生成 0 + 1 ~ 99 + 1的一个随机数
int rannum = rand() % 100 + 1; //rand()%100生成0~99的一个随机数
int num = -1;
//2、玩家进行猜测
cout << "请输入一个数字:" << endl;
cin >> num;
//3、判断玩家的猜测
//猜对 退出游戏
//猜错 提示猜的结果 过大或过小 重新返回第2步
while(rannum != num)
{
if(rannum > num)
{
cout << "输入的数字" << num << "过小,请重新输入:" << endl;
}
else
{
cout << "输入的数字" << num << "过大,请重新输入:" << endl;
}
cin >> num;
}
cout << "恭喜你猜对了,游戏结束" << endl;
return 0;
}
作用:满需循环条件,执行循环语句
语法:do{ 循环语句 }while(循环条件);
注意:与while的区别在于do...while会先执行一次循环语句,再判断循环条件
示例
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
//在屏幕上输出0-9这10个数字
int num = 0;
do
{
cout << num << endl;
num ++;
}while(num < 10);
return 0;
}
水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身
例如:1^3 + 5^3 + 3^3 = 153
请利用do...while语句,求出所有3为书中的水仙花数
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
//寻找3位数中的水仙花数
int num = 100;
int n1,n2,n3;
do
{
//获取个位数字
n1 = num % 10;
//获取十位数字
n2 = num / 10 % 10;
//获取百位数字
n3 = num / 100;
if(n1 * n1 *n1 + n2 * n2 * n2 + n3 * n3 * n3 == num)
{
cout<< num << "是水仙花数" <
作用:满足循环条件,执行循环语句
语法:for(起始表达式;条件表达式;末尾循环体) { 循环语句; }
示例
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
return 0;
}
从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
for (int i = 1; i < 100 + 1; i++)
{
if(i % 10 == 7 || i / 10 == 7 || i % 7 == 0)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
return 0;
}
作用:在循环体中再嵌套一层循环,解决一些实际问题
例如我们想在屏幕中打印如下图片,就需要利用嵌套循环
示例
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << " * " << i << " = " << i * j << " ";
}
cout << endl;
}
return 0;
}
作用:用于跳出选择结构或者循环结构
break使用的时机:
示例1:switch条件语句中
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
//1、在switch 语句中使用break
cout << "请选择您挑战副本的难度:" << endl;
cout << "1、普通" << endl;
cout << "2、中等" << endl;
cout << "3、困难" << endl;
int num = 0;
cin >> num;
switch(num)
{
case 1:
cout << "选择普通难度" << endl;
break;
case 2:
cout << "选择中等难度" << endl;
break;
case 3:
cout << "选择困难难度" << endl;
break;
default:
cout << "错误选项" << endl;
break;
}
return 0;
}
示例2:循环语句中
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
//2、在for 循环语句中使用break
for(int i =0; i < 10; i++)
{
//如果i==5,退出循环
if(i == 5)
{
break;
}
cout << i << endl;
}
return 0;
}
示例3:嵌套循环语句中
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
//3、在嵌套循环语句中使用break
for(int i =0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
if(j == 5)
{
break;
}
cout << "* ";
}
cout << endl;
}
return 0;
}
作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
示例:
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
for(int i =0; i < 50; i++)
{
if(i % 2 == 0)
{
continue;
}
cout << i << endl;
}
return 0;
}
continue可以筛选条件,程序执行到continue处,不再向下执行,直接执行下一次循环
break和continue区别:
break会直接退出循环;
continue是跳过本次循环,进入下一次循环。
作用:可以无条件跳转语句
语法: goto 标记;
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
示例
#include
using namespace std;
#include
//time系统时间头文件包含
#include
int main()
{
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
FLAG:
cout << "5" << endl;
return 0;
}