c++语句

it2025-01-10  27

条件语句、switch语句、迭代语句

条件语句if if-else

switch语句 switch-case-default case标签必须是整型常量表达式

 

int num = 1.5;

switch (num)

{

case 1:

int s;

s = 5;

cout << s << endl;

case 2:

s = 6;

cout << s <<endl;

break;

case 3:

s = 7;

break;

default:

break;

}

 

 

迭代语句

while for do-while

for的范围语句在使用时不能添加删除元素

 

 

跳转语句

break 直接跳出循环或switch语句

continue 跳过本次循环

return 跳出函数

goto 从goto语句无条件跳转到统一函数内的另一条语句

 

异常语句

throw:直接抛出

throw runtime_error("adwieuwiaoeu");

 

try-catch

try

{

//如果这里处理失败,就会执行下边catch中的代码

 

}

catch (exception e)

{

cout << e.what() << endl;

}

最新回复(0)