选择结构
if语句
if(表达式){
执行语句;
}
当条件成立,表达式的值为真时,即是为1,才会执行if中语句。
表达式的结果是一个bool值,true和false。
if...if...语句
if(表达式1){
语句1;
}
if(表达式2){
语句2;
}
如果条件满足表达式1,则执行语句1
如果表达式2成立,则执行语句2
if - else语句
if(表达式){
语句1;
}else{
语句2;
}
选择嵌套
是多个if结构嵌套在一起。
三选一
结构1
if(条件1){
if(条件2){
语句1; //条件1和条件2都满足
}else{
语句2; //满足条件1,不满足条件2
}
}else {
语句3; //不满足条件1
}
结构2
if(条件1){
语句 1; //满足条件1
}else{
if(条件2){
语句21; //不满足条件1,满足条件2
}else{
语句22; //不满足条件1,也不满足条件2
}
}
结构3
if(条件1){
if(条件2)
语句11; //满足条件1,也满足条件2
else
语句12; //满足条件1,不满足条件2
}else{
if(条件3)
语句21; //不满足条件1,满足条件3
else
语句22; //不满足条件1,也不满足条件3
}
多分支结构
if(条件1)
语句1; //满足条件1就执行
else if(条件2)
语句2; //不满足条件1,但是满⾜条件2执行
else if (条件3)
语句3; //不满足条件1和条件2,满足条件3执行
......
else 语句 n; //不满足所有条件就执行
循环结构
while语句
格式:
while(条件表达式)
语句
do while 语句
格式:
do 语句
while(条件表达式);
for循环
格式:
for(表达式1;表达式2;表达式3)
语句
while循环
语法
cpp
// 1、循环开始条件
while(循环继续条件){ //2、 循环判断条件
循环内部语句;
// 3、循环变量自增或自减
}
-
循环继续条件的值为布尔类型
-
当条件的值为true时,会继续执行内部的语句
-
否则跳出循环,执行循环后的语句
cpp
#include<iostream>
using namespace std;
int main(){
int cnt = 1;
while(cnt <= 10){
cout << "hello C++" << endl;
}
return 0;
}
例题
如何使用while循环输出1-100的数字?
cpp
#include<iostream>
using namespace std;
int main(){
int cnt = 1;
while(cnt <= 100){
cout << cnt << endl;
cnt ++;
}
return 0;
}
可不可以实现倒序输出100-1的数字呢?
cpp
#include<iostream>
using namespace std;
int main(){
int cnt = 100;
while(cnt > 0){
cout << cnt << endl;
cnt --;
}
return 0;
}
输出1 - 100之间的所有奇数?
cpp
#include<iostream>
using namespace std;
int main(){
int i = 1;
while(i <= 50){
cout << 2 * i - 1 << endl;
i ++;
}
return 0;
}
思路分析:
首先1-100之间的奇数有50个,所以循环只需要执行50次就可以了。
然后第一个奇数为1,第二个奇数为2,...,第i个奇数为2*i - 1
最后输出整数n到整数m之间的数字,包括n和m
计算1 - 100的和?
cpp
#include <iostream>
using namespace std;
int main() {
int s = 0,i = 1;
while(i <= 100){
s = s + i;
i += 1;
}
cout << s << endl;
return 0;
}
累加求和?
cpp
#include<iostream>
using namespace std;
int main(){
int i = 1;
int s = 0; // 初始值一定要为0
while(i <= 100){
s += s + i; //累加求和
i++;
}
cout << s << endl;
return 0;
}
求阶乘?
cpp
#include<iostream>
using namespace std;
int main(){
int i = 1;
int s = 1; // 初始值一定要为1
while(i <= 5){
s = s * i; // 进行求积
i++;
}
cout << s << endl;
return 0;
}
while循环嵌套if
在while循环的内部是可以使用任何语句,比如if语。
cpp
#include<iostream>
using namespace std;
int main(){
int i = 1;
while(i <= 100){
if(i % 2 == 0){
cout << i << endl;
}
i++;
}
return 0;
}
输出1-100中最后一位是7的数?
cpp
#include<iostream>
using namespace std;
int main(){
int i = 1;
while(i <= 100){
if(i % 10 == 7){
cout << i << endl;
}
i++;
}
return 0;
}
运行结果
7
17
27
37
47
57
67
77
87
97
for循环
基本语法
cpp
for(/*头部*/;/*判断部分*/;/*结尾部分*/){
// 循环体
}
-
头部:for循环的初始条件
-
判断部分:for循环的判断条件
-
结尾部分:for循环中变量自增或自减
while循环和for循环的区别
cpp
int i = 1;
while(i <= 100){
cout << i << endl; //while循环
i++;
}
//for循环
for(int i = 1; i <= 100; i++){
cout << i << endl;
}
示例
倒计时输出
如何输出100 - 1的数字?
cpp
#include<iostream>
using namespace std;
int main(){
for(int i = 100; i > 0; i--){
cout << i << endl;
}
return 0;
}
数据统计
小明的本子上有n个数字,它们可能是三位数、四位数或者五位数,请你统计出来每一类数字各有多少个?
【输入格式】
输入有2行:
* 第一行有一个整数n,1 <= n <= 100,是小明拥有的数字的个数;
* 第二行有用空格隔开的n个正整数,均不小于100,不大于99999,是小明本子上的数字
【输出格式】
输出有一行,为三个空格隔开的整数,分别表示三位数、四位数和五位数的个数
【样例输入】
3
123 333 99999
【样例输出】
2 0 1
【代码】
cpp
#include<iostream>
using namespace std;
int main(){
int n,x;
int cnt_3 = 0,cnt_4 = 0,cnt_5 = 0;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> x;
if(x <= 999){
cnt_3++;
}else if(x <= 9999){
cnt_4++;
}
else{
cnt_5++;
}
}
cout << cnt_3 << " " << cnt_4 << " " << cnt_5 << endl;
return 0;
}