Program Flow Constructions
When it comes to program flow controls, your JavaScript experience gets you almost all of the way there. The following C/Objective-C prototypes should all look familiar to you (showing the multiple-line statement version with curly braces and optional items in brackets):
if (conditionExpression) {statementsIfTrue} if (conditionExpression) {statementsIfTrue} else {statementsIfFalse} result = (conditionExpression) ?expression1:expression2; while (conditionExpression) {statementsIfTrue} do {statements} while (conditionExpression) for (initializationExpression;conditionExpression;updateExpression) { statements } switch (expression) { caselabelN:statements[break;] ... [default:statements] }
Because the comparison operators you use in C/Objective-C are the
same ones you know from JavaScript, you already know what to do with all
of the conditionExpression placeholders. The
only construction you need to watch carefully is the for repeat loop. Be sure that the counter
variable you initialize in the
initializationExpression has a data type
associated with it. You can specify the data type in a separate statement
above the for loop or in the expression
itself:
int i;
for (i = 0; i < maxValue; i++) {...}
for (int j = 0; j < maxValue; j++) {...}Bear in mind the discussion earlier in this chapter about scope with regard to the loop counting variable. If you need the variable value after the loop completes, be sure to declare the variable before the loop. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access