Prev | Index | Next |
While loops, do-while loops, and if statements have the same syntax as in C/C++. One relevant change to note is that these control structures accept boolean values only, and there is no implicit cast from bool to int in C*. Hence, conditions like while(1)
won't work by default; programmers who want this behaviour must define a cast from bool to int themselves, at the cost of weaker typechecking.
Variable declarations are now allowed within conditions. For example:
while ((char c := getchar()) != '\n') { ... }
The brackets around the variable declaration are required (to prevent ambiguities), and are usually clearer anyway. Any variable declared within a condition has scope only within the statement containing the condition. For example, the scope of c above is the while loop, and the scope of a variable declared within an if-else block is both the if clause and the else clause, but not the code after the if-else block.
For loop
For loops are quite a bit different in C*. Here is an example:
for (int i := 1..10) {}
The loop consists of an assignment, with either a variable declaration or a variable use on the left, and a range on the right. The range must consist of integers. The loop iterates through the values in the range, setting each to the variable i in turn. Normally, it increases by 1 each time; for other increments, you provide a step clause:
for (int i := 10..1 step -1) {} for (int i := 1..10 step 2) {}
Select statement
C* includes a select statement, similar to switch in C++. The differences are that select can take any value (switch can only operate on integers), only 1 case is selected each time (no "break" statements are needed), and cases can match a range of values. Here's an example:
select (num_grade) { case 0..49: grade := "F"; case 50..59: grade := "D"; case 60..69: grade := "C"; case 70..79: grade := "B"; case 80..89: grade := "A"; case 90..99: case 100: grade := "A+"; case else: grade := "Invalid grade"; }
Note also that the default
keyword has been removed, and default cases are now specified as case else
.
Labels
Goto statements and labels are allowed in C*, the same as in C++, as are break and continue. Further, you can give a label to a break or continue statement, to refer to an outer loop when inside a nested loop. For example:
input_loop: while ((char line[]) := readLine()) { for (int i := line.li..line.ui) { if (line[i] == end_char) break input_loop; } }
Prev | Index | Next |