C++菜鸟学习笔记系列(16)
本期主题:跳转语句(break、continue、goto、return);异常(异常检测throw表达式、异常处理try语句块)
(1)跳转语句
顾名思义,跳转语句就是中断当前的执行过程,C++语言为我们提供了四种跳转语句:break、continue、goto、return等,在本篇博客中我们主要介绍前三种,return语句在后面介绍函数部分时我们再做补充。
break语句
break语句一般出现在迭代语句或者switch语句内部(包括嵌套在此类循环语句或块中),用于终止距它最近的while、do while、for、switch等语句,然后再从这些语句结束后的代码开始执行。
对于break语句的使用我们简单看两个示例:
int a = 0; switch (a) { case 0 : for (int i = 0; i != 10; ++i ) { if (i == 5) break;//1 } break;//2 case 1 : { ; } break;//3 }
对于第一个break,其控制的是for语句,而第二个和第三个break则控制着switch语句中的case分支。
continue语句
continue语句用于终止距其最近的循环中的当前迭代,并开始下一次迭代。continue语句只能出现在for、while、do while 等循环语句的内部(或者嵌套在此类循环体中的语句或块中)。
与break语句最大的不同是continue语句虽然中断了当前循环,但是它只是中断了当前迭代,随后仍然继续执行循环。
下面我们看一个关于continue使用的小例子:
/* author: wxc_1998 date: 2018/10/24 */ #include <iostream> #include <string> using namespace std; void main() { string s; int count = 0; while (cin >> s && !s.empty()) { if ( s.size() > 10) { ++ count; continue; } } cout << "there are " << count << " strings larger than 10 characters!" << endl; cout << endl << "press any key to continue!"; cin.clear(); cin.sync(); cin.get(); }
在上述代码中我们每次执行continue语句就会重新执行一次while语句的迭代。
goto语句
在开始介绍goto语句之前,我们在这里先说一下,我们最好不要在程序中使用goto语句,因为他会使得程序变得非常既难理解又难以修改。
其基本形式为:
goto label;
其中label是用于标识一条语句的标签(标签就是一个标识符加上一个冒号:,且标识符独立于变量和其他的标识符的名字,所以我们使用和程序中其他变量名字相同的标识符并不会引起错误)。
下面我们看一个小例子:
/* author: wxc_1998 date: 2018/10/24 */ #include <iostream> #include <string> using namespace std; void main() { begin: string s = "abcdefghigklmn"; int i = 0; cout << "input a integer: " << endl; cin >> i; if (i >= 0 && i < 14) goto end; else { cout << "the integer is out of range!" << endl; goto begin; } end: cout << s[i]; cout << endl << "press any key to continue!"; cin.clear(); cin.sync(); cin.get(); }
对于上述代码我们实现了输入一个0-14的整数,输出相对应的字符的功能。其中使用了两个goto语句,分别跳转到begin和end处。其实对于这个程序我们完全可以使用迭代语句代替,由于goto语句难以理解的特点,所以在写程序时我们并不经常使用它。
(2)异常
异常是指在程序运行时所出现的反常行为,这些行为超出了函数正常功能的范围,典型的异常包括失去数据库连接以及遇到意外输入等。
处理反常行为可能是程序设计中最困难的一部分!
所以在这里我们只是做简单的介绍,并不能帮助大家真正实现程序的异常处理。
异常处理机制包括程序中的异常检测和异常处理这两部分。在C++语言中异常处理机制一般为:
**throw表达式:**异常检测部分使用throw表达式来表示它遇到了无法处理的问题。我们说throw引发了异常。
**try语句块:**异常处理部分使用try语句块处理异常。try语句块以关键字try开头,并以一个或多个catch子句结束,try语句块中代码抛出的异常通常会被某个catch子句处理。因为catch语句中包含了异常处理的代码。
下面我们来看一个关于throw语句和try语句使用的小例子:
/* author: wxc_1998 date: 2018/10/24 */ #include <iostream> #include <string> using namespace std; void main() { string s = "abcdefghigklmn"; int i = 0; while (cin >> i) { try { if (i >= 0 && i < 14) { cout << s[i] << endl; } else if ( i < 0) throw runtime_error ("the integer is negative!"); else throw runtime_error ("the integer is too larger"); } catch(runtime_error err) { cout << err.what() << endl; cout << "try again? enter y or n" << endl; char c; cin >> c; if (c == 'y') continue; else break; } cout << endl << "press any key to continue!"; cin.clear(); cin.sync(); cin.get(); } }
对于上述代码,我们使用了一个try语句块,其中包括了两个监测异常的throw语句,当程序有异常输入时,抛出一个run_time类型的异常对象。然后在后面有一个对应的catch子句,用于异常处理。
好了,这次就写到这里了我们下次再见。
虽然不是程序猿,但还是在这里祝大家1024快乐! O(∩_∩)O哈哈~
注:虽然这篇博客的内容十分简单,但是大家若有转载还请标明出处!