这里会显示出您选择的修订版和当前版本之间的差别。
前一修订版 | |||
— | en:reference:language:braces [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== {} Curly Braces ====== | ||
+ | |||
+ | |||
+ | Curly braces (also referred to as just " | ||
+ | |||
+ | |||
+ | An opening curly brace " | ||
+ | |||
+ | |||
+ | At present this feature is slightly buggy as the IDE will often find (incorrectly) a brace in text that has been " | ||
+ | |||
+ | |||
+ | Beginning programmers, | ||
+ | |||
+ | |||
+ | Because the use of the curly brace is so varied, it is good programming practice to type the closing brace immediately after typing the opening brace when inserting a construct which requires curly braces. Then insert some carriage returns between your braces and begin inserting statements. Your braces, and your attitude, will never become unbalanced. | ||
+ | |||
+ | |||
+ | Unbalanced braces can often lead to cryptic, impenetrable compiler errors that can sometimes be hard to track down in a large program. Because of their varied usages, braces are also incredibly important to the syntax of a program and moving a brace one or two lines will often dramatically affect the meaning of a program. | ||
+ | ===== The main uses of curly braces ===== | ||
+ | |||
+ | ==== Functions ==== | ||
+ | <code cpp> | ||
+ | void myfunction(datatype argument){ | ||
+ | statements(s) | ||
+ | } | ||
+ | </ | ||
+ | ==== Loops ==== | ||
+ | <code cpp> | ||
+ | while (boolean expression) | ||
+ | { | ||
+ | statement(s) | ||
+ | } | ||
+ | |||
+ | do | ||
+ | { | ||
+ | statement(s) | ||
+ | } | ||
+ | while (boolean expression); | ||
+ | |||
+ | for (initialisation; | ||
+ | { | ||
+ | statement(s) | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | ==== Conditional statements ==== | ||
+ | <code cpp> | ||
+ | if (boolean expression) | ||
+ | { | ||
+ | statement(s) | ||
+ | } | ||
+ | |||
+ | else if (boolean expression) | ||
+ | { | ||
+ | statement(s) | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | statement(s) | ||
+ | } | ||
+ | |||
+ | </ | ||