c++(条件控制等语句)
条件结构
语法
- 当 statement 只有一句时,可以省略花括号"{}",否则必须加花括号
if (condition_1) {
statement_1
}
else if (condition_2){
statement_2
}
...
else if (condition_n){
statement_n
}
else{
statement
}
Example
#include <iostream>
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (num > 0) {
std::cout << "The number is positive." << std::endl;
} else if (num < 0) {
std::cout << "The number is negative." << std::endl;
} else {
std::cout << "The number is zero." << std::endl;
}
return 0;
}
循环结构
- 循环结构有 for、while、do-while
1 for
for 循环语句的格式是∶
for (initialization; condition; increase) {statement};
按以下方式工作∶
- initialization: 通常是设置一个计数器变量(counter variable)的初始值,初始化仅被执行一次
- condition: 如果条件为真,就继续循环,否则循环结束,循环中的语句 statement 被跳过
- 执行语句 statement: 可以是一个单独的语句,也可以是一个由花括号({})括起来的语句块
- 最后(increase field)中的语句被执行,循环返回第 2 步
Example 1
#include <stdio.h>
int main ()
{
int nFrames = 10;
for (int ni=0; ni<nFrames; ni++){
printf("Processing %d frame...\n", ni);
}
for (int ni=0; ni<nFrames; ni++)
printf("Processing %d frame...\n", ni);
for (int ni=0; ni<nFrames; ni++) printf("Processing %d frame...\n", ni);
return 0;
}
Example 2
- 在 for 循环初始化或增值域中,可以放 1 条以上的语句
- 中间用逗号(,)隔开
#include <stdio.h>
int main ()
{
int n, i;
// 初始化 2 个变量,同时更新
for (n=0, i=100; n!=i; n++, i--)
{
printf("n = %d, i = %d\n", n, i);
}
return 0;
}
2 while
while循环语句的格式是∶
while (表达式expressior) 语句 statement
它的功能是当 expression 的值为 true 时重复执行 statement。例如,下面将用 while 循环来写一个倒计数程序
Example 1
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << n << ", ";
--n;
}
return 0;
}
Example 2: 无限循环
- 通常用在图像显示、需要一直运行的线程
#include <stdio.h>
int main ()
{
int n = 0;
while(1)
{
if(n % 100000000 == 0){
printf("n=%d\n", n);
}
n++;
}
return 0;
}
3 do-while
与 while 相反 do-while 循环中是先执行 statement 然后才检查条件(condition)。这样,即使条件从来没有被满足过,statement 仍至少被执行一次
do-while 循环语句的格式是∶
do 语句statement while(条件condition);
Example 1
- 重复输出用户输入的任何数值,直到用户输入 0 为止
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
} while (n != 0);
return 0;
}
Example 2
- ORB-SLAM3 代码段
if(dist_sum > 0)
{
double cut_d;
do
{
cut_d = DUtils::Random::RandomValue<double>(0, dist_sum);
} while(cut_d == 0.0);
...
}
分支控制和跳转
1 break 语句
通过使用 break 语句,即使在结束条件没有满足的情况下,我们也可以跳出一个循环。它可以被用来结束一个无限循环(infinite loop),或强迫循环在其自然结束之前结束。
Example 1
#include <iostream>
using namespace std;
int main ()
{
int n;
for (n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!\n";
break;
}
}
return 0;
}
2 continue 语句
continue 语句使得程序跳过当前循环中剩下的部分而直接进入下一次循环
Example 1: 在 for 循环中使用
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--) {
if (n==5) continue;
cout << n << ", ";
}
return 0;
}
Example 2: 在 while 循环中使用
- 要非常注意 n++ 放的位置, 否则可能导致死循环
int main ()
{
int n=0;
while(n<10){
n++;
if (n==5){
printf("continue.\n");
continue;
}
printf("n=%d, ", n);
}
cout << endl;
return 0;
}
Example 3
- ORB-SLAM3 代码段
int KeyFrame::GetNumberMPs()
{
unique_lock<mutex> lock(mMutexFeatures);
int numberMPs = 0;
for(size_t i=0, iend=mvpMapPoints.size(); i<iend; i++)
{
if(!mvpMapPoints[i])
continue;
numberMPs++;
}
return numberMPs;
}
case 选择结构
语法
- switch 只能被用来比较表达式和不同常量的值(constants)。因此,我们不能够把变量或范围放在 case 之后
switch (expression)
{
case constant1 :
block of instructions 1
break;
case constant2 :
block of instructions 2
break;
...
default :// 可选
default block of instructions
}
// 错误
case(n*2);
case(1...3);
按以下方式执行:
-
switch 计算表达式(expression)的值,并检查ta是否与第一个常量constant1 相等,如果相等,程序执行常量1后面的语句块 block of instructions 1直到碰到关键字 break,程序跳转到 switch 选择结构的结尾处
-
如果 expression 不等于 constant1,程序检查表达式 expression 的值是否等于第二个常量 constant2,如果相等,程序将执行常量2后面的语句块 block of instructions 2 直到碰到关键字 break
-
以此类推,直到最后表达式 expression 的值不等于任何前面的常量(你可以用case语句指明任意数量的常量值来要求检查),程序将执行默认区 default 后面的语句,如果它存在的话。default 选项是可以省略的
Example 1
#include <iostream>
using namespace std;
int main()
{
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;
switch (choice)
{
case 'A' :
cout<< "You entered A. \n";
break;
case 'B' :
cout << "You entered B. \n";
break;
case 'C' :
cout << "You entered C.\n";
break;
default:
cout << "You did not enter A, B, or C!\n";
}
return 0;
}
Example 2
- VINS-Fusion 代码段
Camera::Parameters::Parameters(ModelType modelType)
: m_modelType(modelType)
, m_imageWidth(0)
, m_imageHeight(0)
{
switch (modelType)
{
case KANNALA_BRANDT:
m_nIntrinsics = 8;
break;
case PINHOLE:
m_nIntrinsics = 8;
break;
case SCARAMUZZA:
m_nIntrinsics = SCARAMUZZA_CAMERA_NUM_PARAMS;
break;
case MEI:
default:
m_nIntrinsics = 9;
}
}

浙公网安备 33010602011771号