break 和 continue
#include <iostream>
using namespace std;
int main(){
// break 中斷本層循環(huán)
// !1 ~ 100之間累加和時(shí), 當(dāng)累加和大于1000時(shí), 結(jié)束循環(huán)
int i = 1;
int sum = 0;
while (i <= 100){
cout <<"i = "<< i <<endl;
if (sum > 1000)
break;
sum += i;
i++;
cout <<"sum = "<< sum <<endl;
}
cout <<"sum = "<< sum <<endl;
// continue 跳過(guò)本次循環(huán), 執(zhí)行下一次循環(huán)
cout << endl;
}
continue
#include <iostream>
using namespace std;
int main(){
// continue 跳過(guò)本次循環(huán), 執(zhí)行下一次循環(huán)
// 遇到continue后面的就不會(huì)執(zhí)行了
// int i = 1;
// int sum = 0;
// while (i <= 4){
// cout <<"i = "<< i <<endl;
//
// if (i == 3){
//
// continue;
// }
// sum += i;
// i++;
// }
// cout <<"sum = "<< sum <<endl;
// 計(jì)算 1 ~ 100 所有奇數(shù)累加和
int i = 1;
int sum = 0;
while (i <= 100){
cout <<"i = "<< i <<endl;
if (i%2==0){
i++;
continue;
}
sum += i;
i++;
}
cout <<"sum = "<< sum <<endl;
}
循環(huán)嵌套
#include <iostream>
using namespace std;
int main(){
// 循環(huán)嵌套
int i = 0;
while (i < 5){
cout <<"i ==============================="<< i <<endl;
int j = 0;
while (j < 6){
cout <<"j = "<< j <<endl;
j++;
}
i++;
}
}
#include <iostream>
using namespace std;
int main(){
// 打印 5 x 6 行的 *
int i = 0;
while (i < 5){
int j = 0;
while (j < 6){
cout <<"* ";
j++;
}
// 換行
cout<<endl;
i++;
}
}
for版本
#include <iostream>
using namespace std;
int main(){
// 打印 5 x 6 行的 *
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 6; ++j) {
cout <<"* ";
}
// 換行
cout<<endl;
}
}
打印等腰三角形星星
#include <iostream>
using namespace std;
//*
//* *
//* * *
//* * * *
//* * * * *
int main(){
// 打印 5 x 6 行的 *
int i = 0;
while (i < 5){
int j = 0;
while (j < i+1){
cout <<"* ";
j++;
}
// 換行
cout<<endl;
i++;
}
}
image
#include <iostream>
using namespace std;
//九九乘法表
int main(){
// 打印 5 x 6 行的 *
int i = 1;
while (i <= 9){
int j = 1;
while (j <= i){
cout << j <<" x " << i << " = " << i*j<< "\t";
j++;
}
// 換行
cout<<endl;
i++;
}
}
水仙花數(shù)
#include <iostream>
using namespace std;
// 輸入一個(gè)數(shù) 判斷他是不是水仙花數(shù)
// 水仙花數(shù)一個(gè)3位數(shù), 個(gè)位立方 + 十位立方 + 百位立方 = 這個(gè)數(shù)
int main(){
// int num = 156;
int num = 156;
cout<<"請(qǐng)輸入一個(gè)數(shù)"<<endl;
cin>> num;
int ge, shi, bai;
ge = num % 10;
shi = (num % 100 - ge)/10;
bai = num/100;
if ((ge*ge*ge + shi*shi*shi + bai*bai*bai) == num){
cout<<num<<" 是水仙花數(shù)呀"<<endl;
}else{
cout<<num<<" 不是水仙花數(shù)呀"<<endl;
}
}
找到所有的水仙花數(shù)
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int num=100;
while(num<=999){
int ge,shi,bai;
ge=num%10;
shi=(num%100-ge)/10;
bai=num/100;
if(pow(ge, 3) + pow(shi, 3) + pow(bai, 3)==num)
cout<<"水仙花數(shù)是"<<num<<endl;
else{
num++;
continue;}
num++;
}
}
goto語(yǔ)句
作用 可以無(wú)條件的跳轉(zhuǎn)語(yǔ)句
#include <iostream>
#include <cmath>
using namespace std;
int main(){
cout<<" 1 "<<endl;
goto FLAG;
cout<<" 2 "<<endl;
cout<<" 3 "<<endl;
cout<<" 4 "<<endl;
FLAG:
cout<<" 5 "<<endl;
}
注意:在程序中不建議使用goto語(yǔ)句,以免造成程序流程混亂
數(shù)組
所謂數(shù)組,就是?個(gè)集合,??存放了相同類型的數(shù)據(jù)元素
特點(diǎn)1:數(shù)組中的每個(gè)數(shù)據(jù)元素都是相同的數(shù)據(jù)類型
特點(diǎn)2:數(shù)組是由連續(xù)的內(nèi)存位置組成的
?維數(shù)組
定義方式(三種)
1. 數(shù)據(jù)類型 數(shù)組名[數(shù)組長(zhǎng)度];
2. 數(shù)據(jù)類型 數(shù)組名[數(shù)組長(zhǎng)度] = {值1, 值2, ...}
3. 數(shù)據(jù)類型 數(shù)組名[] = {值1, 值2, ...}
#include <iostream>
using namespace std;
int main(){
// 變量類型 變量名;
int b;
b = 20;
int a = 100;
//1. 數(shù)據(jù)類型 數(shù)組名[數(shù)組長(zhǎng)度];
int scores[10];
//2. 數(shù)據(jù)類型 數(shù)組名[數(shù)組長(zhǎng)度] = {值1, 值2, ...}
int arr[3] = {1, 2, 3};
//3. 數(shù)據(jù)類型 數(shù)組名[] = {值1, 值2, ...}
int arr2[] = {1, 2, 3};
// 操作訪問(wèn)
cout << arr << endl; // 直接打印數(shù)組不會(huì)顯示數(shù)組中的數(shù)據(jù), 返回時(shí)數(shù)組元素的內(nèi)存首地址(0x十六進(jìn)制)
// 單個(gè)訪問(wèn)
// 數(shù)組名[索引] 從0開始
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << arr[2] << endl;
}
作業(yè)
- 打印正方形星星, 等腰, 久久乘法表都寫成for
-
10 x 10
