1. 使用if else 有条件地执行
1.1 使用if…else 进行条件编程
# include <iostream>
using namespace std;
int main ( ) {
cout << "Enter two integers: " << endl;
int num1 = 0 , num2 = 0 ;
cin >> num1;
cin >> num2;
cout << "Enter \'m\' to multiply, anything else to add: " ;
char userSelection = '\0' ;
cin >> userSelection;
int result = 0 ;
if ( userSelection == 'm' ) {
result = num1 * num2;
} else {
result = num1 + num2;
}
cout << "result is: " << result << endl;
return 0 ;
}
1.2 有条件地执行多条语句
if ( condition) {
Statement 1 ;
Statement 2 ;
} else {
Statement 3 ;
Statement 4 ;
}
1.3 嵌套if语句
if ( expression1) {
DoSomething1;
if ( expression2) {
DoSomething2;
} else {
DoSomethingElse2;
}
} else {
DoSomethingElse1;
}
if ( expression1) {
DoSomething1;
} else if {
DoSomething2;
} else {
DoSomethingElse12;
}
1.4 使用switch-case进行条件处理
break 并非必不可少,但如果省略它,将不断与后面的标签进行比较 default 也是可选的,它用于执行expression 不与switch-case 中的任何标签匹配时应执行的操作
switch ( expression)
{
case LabelA:
DoSomething;
break ;
case LabelB:
DoSomethingElse;
break ;
default :
DoStuffWhenExpressionIsNotHandledAbove;
break ;
}
1.5 使用运算符?:进行条件处理
int max = ( num1 > num2) ? num1 : num2;
2. 在循环中执行代码
2.1 while 循环
while ( expression) {
StatementBlock;
}
2.2 do…while 循环
do {
StatementBlock;
} while ( condition) ;
2.3 for 循环
允许指定执行一次的初始化语句(通常用于初始化计数器) 检查退出条件(通常使用计数器) 在每次循环末尾执行操作(通常是将计数器递增或修改其值)
# include <iostream>
using namespace std;
int main ( )
{
const int ARRAY_LENGTH = 5 ;
int myNums[ ARRAY_LENGTH] = { 0 } ;
cout << "Populate array of " << ARRAY_LENGTH << " integers" << endl;
for ( int counter = 0 ; counter < ARRAY_LENGTH; ++ counter) {
cout << "Enter an integer for element " << counter << ": " ;
cin >> myNums[ counter] ;
}
cout << "Displaying contents of the array: " << endl;
for ( int counter = 0 ; counter < ARRAY_LENGTH; ++ counter) {
cout << "Element " << counter << " = " << myNums[ counter] << endl;
}
return 0 ;
}
2.4 基于范围的for 循环
# include <iostream>
# include <string>
using namespace std;
int main ( ) {
int someNums[ ] = { 1 , 101 , - 1 , 40 , 2040 } ;
for ( const int & aNum : someNums) {
cout << aNum << ' ' ;
}
cout << endl;
for ( auto anElement : { 5 , 222 , 110 , - 45 , 2017 } ) {
cout << anElement << ' ' ;
}
cout << endl;
char charArray[ ] = { 'h' , 'e' , 'l' , 'l' , 'o' } ;
for ( auto aChar : charArray) {
cout << aChar << ' ' ;
}
cout << endl;
double moreNums[ ] = { 3.14 , - 1.3 , 22 , 10101 } ;
for ( auto anElement : moreNums) {
cout << anElement << ' ' ;
}
cout << endl;
string sayHello{ "Hello World!" } ;
for ( auto anElement : sayHello) {
cout << anElement << ' ' ;
}
cout << endl;
return 0 ;
}
3. 使用continue和break修改循环的行为
continue 能够跳转到循环开头,跳过循环块中后面的代码 continue 导致重新评估循环条件,如果为true,则重新进入循环块 break 退出循环块,即结束当前循环
3.1 不结束的循环———无限循环
应用场景:假设操作系统需要不断检查USB端口是否连接了设备,只要操作系统在运行,这种活动就不应停止
while ( true ) {
DoSomethingRepeatedly;
}
for ( ; ; ) {
DoSomethingRepeatedly;
}
3.2 控制无限循环
while ( true ) {
DoSomethingRepeatedly;
if ( expression)
break ;
}
for ( ; ; ) {
DoSomethingRepeatedly;
if ( expression)
break ;
}
4. 编写嵌套循环
# include <iostream>
using namespace std;
int main ( ) {
const int ARRAY1_LEN = 3 ;
const int ARRAY2_LEN = 2 ;
int myNums1[ ARRAY1_LEN] = { 35 , - 3 , 0 } ;
int myNums2[ ARRAY2_LEN] = { 20 , - 1 } ;
cout << "Multiplying each int in myNums1 by each in myNums2: " << endl;
for ( int index1 = 0 ; index1 < ARRAY1_LEN; ++ index1) {
for ( int index2 = 0 ; index2 < ARRAY2_LEN; ++ index2) {
cout << myNums1[ index1] << " x " << myNums2[ index2] \
<< " = " << myNums1[ index1] * myNums2[ index2] << endl;
}
}
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
const int NUM_ROWS = 3 ;
const int NUM_COLUMNS = 4 ;
int MyInts[ NUM_ROWS] [ NUM_COLUMNS] = { { 34 , - 1 , 879 , 22 } ,
{ 24 , 365 , - 101 , - 1 } ,
{ - 20 , 40 , 90 , 97 } } ;
for ( int row = 0 ; row < NUM_ROWS; ++ row) {
for ( int column = 0 ; column < NUM_COLUMNS; ++ column) {
cout << "Integer[" << row << "][" << column \
<< "] = " << MyInts[ row] [ column] << endl;
}
}
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
const int numsToCalculate = 5 ;
cout << "This program will calculate " << numsToCalculate \
<< " Fibonacci Numbers at a time" << endl;
int num1 = 0 , num2 = 1 ;
char wantMore = '\0' ;
cout << num1 << " " << num2 << " " ;
do {
for ( int counter = 0 ; counter < numsToCalculate; ++ counter) {
cout << num1 + num2 << " " ;
int num2Temp = num2;
num2 = num1 + num2;
num1 = num2Temp;
}
cout << endl << "Do you want more numbers (y/n)? " ;
cin >> wantMore;
} while ( wantMore == 'y' ) ;
return 0 ;
}
Q&A
编写了一个类似于while(Integer)的循环,如果Integer的值为-1,这个while循环会执行吗?
理想情况下,while 循环表达式应为布尔值true 或false,否则这样解读:零表示false,非零表示true。由于−1 不是零,因此该while 条件为true,循环将执行。如果希望仅当Integer 为正数时才执行循环,可编写表达式while(Integer>0)。这种规则适用于所有的条件语句和循环。