if statements,
switch statements,
for loops, and
while loops.
if statement.
if (test) statement executes the
statement only when the test holds.
if (test) statement else
statement selects between the two statements. It executes
the first if the test evaluates to true and the second if it
evalutes to false.
if
statements.
if statements, such a solution is neither readable nor
efficient.
switch statement
(like the case statement in Pascal).
switch (integer-valued-expression) {
case value1:
stuff-to-do;
break;
case value2:
stuff-to-do;
break;
...
default:
stuff-to-do;
break;
}
break, execution will continue into
the next case.
byte, short, int,
long, or char valued expressions.
break has other uses, which you'll see later.
while loop, which has
the form while (test) statement
while loop
called the do loop, and has the form do statement while (test).
for loop,
which has the form
for (initialization; test; increment) statement;
initialization;
while (test) {
statement;
increment;
}
for
loop of Pascal.
for loop can be ommitted
(in which case it is ignored).
int i; // A counter variable
int factorial; // The factorial of N
factorial = 1;
for (i = 1; i <= N; i = i+1) {
factorial = factorial * i;
}
var++.
{
statement1;
statement2;
...
statementn;
}
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Fri Nov 21 08:21:55 1997.
This page generated on Fri Nov 21 08:23:47 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu