Casio FX-890P Owner's Manual page 96

Casio personal computer owner's manual
Table of Contents

Advertisement

Using the "do – while" loop
The "do – while" loop is another method that you can use for repeat execution. The
format of the "do – while" loop is as follows:
do
Statement
while (condition);
Unlike the "while" loop, the "do – while" loop executes the statement first and then
checks whether or not the condition has been met or not. Note also the semicolon at
the end of the "while" line cannot be omitted.
Let's use the "do – while" to find the Greatest Common Measure for two values.
/* Greatest Common Measure */
/* #include <stdio.h> */
main(){
int gcm,x,y;
x=56;
y=63;
printf("¥nGCM(%d,%d) = ",x,y);
do{
gcm=x; x=y%x; y=gcm;
}while (x!=0)
printf("%d¥n",gcm);
}
When you execute this program, the following result should be produced:
Using the "for" loop
You can also use the "for" loop for repeat execution of statements. The format of the
"for" loop is as follows:
For (expression 1; expression 2; expression 3)
Statement
Note that there are three expressions inside of the parentheses of the "for" loop.
• Expression 1 initializes the counter variable, and is executed only once, before
the first pass of the loop.
• Expression 2 is the condition of the loop, so the loop continues to execute until
this condition is not met.
• Expression 3 performs an operation on the counter variable before the statement
in the loop is executed.
GCM(56,63) = 7
>_
96

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Z-1grZ-1

Table of Contents