- C++ Game Development By Example
- Siddharth Shekar
- 128字
- 2021-06-24 14:26:11
Jump statements
As well as condition and iteration statements, you also have break and continue statements.
The break statement is used to break out of an iteration. We can leave a loop and force it to quit if a certain condition is met.
Let's look at the break statement in use:
#include <iostream> #include <conio.h> // Program prints out values to screen int main() { for (int n = 0; n < 10; n++) { if (n == 5) { std::cout << "break" << std::endl; break; } std::cout << "value of n is: " << n << std::endl; } _getch(); return 0; }
The output of this is as follows:

The continue statement will skip the current iteration and continue the execution of the statement until the end of the loop. In the break code, replace the break with continue to see the difference:
#include <iostream> #include <conio.h> // Program prints out values to screen int main() { for (int n = 0; n < 10; n++) { if (n == 5) { std::cout << "continue" << std::endl; continue; } std::cout << "value of n is: " << n << std::endl; } _getch(); return 0; }
Here is the output when break is replaced with continue:

推薦閱讀
- 深入理解Spring Cloud與實戰
- ATmega16單片機項目驅動教程
- 電腦軟硬件維修大全(實例精華版)
- 電腦常見問題與故障排除
- Intel FPGA/CPLD設計(高級篇)
- 電腦組裝、維護、維修全能一本通(全彩版)
- Manage Partitions with GParted How-to
- Spring Cloud微服務和分布式系統實踐
- Mastering Machine Learning on AWS
- Advanced Machine Learning with R
- MicroPython Cookbook
- 計算機組裝與維護
- 電腦軟硬件維修寶典
- 嵌入式系統原理:基于Arm Cortex-M微控制器體系
- Unreal Development Kit Game Programming with UnrealScript:Beginner's Guide