C / C++ Tutorial Part 3
Looping
Looping constucts work on the same principle as the if statement. They work on a condition. If the condition is zero then the loop will stop.
The first looping construct we will will look at is the do-while, while-do loop.
do
{
....
}while(condition);
|
while(condition)
{
....
};
|
The code is similar and the important facts remain the same. The difference between these two construct is that the do-while will always do something before testing the condition. The while-do construct carries out the test before going into the loop, if the condition is zero then the loop is never enterred into.
Lets start with an example :
#include <stdio.h>
void main(void)
{
/* Set up our variables */
int iCounter = 0;
/* Lets set up our loop */
while(iCounter < 10)
{
printf("Loop %d\n",iCounter);
iCounter = iCounter + 1;
}
}
The for loop.
for( initialise ; condition ; loop-execute)
{
}
| initialise | set up variables etc before going into the loop |
| condition | like if the for loop ends when the condition is zero |
| loop-execute | code that is executed at the end of each loop |
Example :
#include <stdio.h>
void main(void)
{
/* Set up our variable */
int iCounter = 0;
/* Go into the loop */
for(iCounter=10;iCounter>0;iCounter = iCounter - 1)
{
printf("Loop %d\n",iCounter);
}
}
Lets just cap this off with something that does something a bit more exciting.
#include <stdio.h>
void main(void)
{
/* Set up our variable */
int iFoundEndOfString = 0;
int iInputLength=0;
int iPosOfLastCharInInput=0;
int iInputPosition = 0;
int iOutputPosition = 0;
char acDataIn[25];
char acDataOut[25];
/* Get some text */
printf("Enter some text - max 25 characters : ");
gets(acDataIn);
/* Show what was typed in */
printf("You entered %s\n",acDataIn);
/* We know that gets puts a 0 at the end of the */
/* string */
/* Find out the length of the string enterred */
for(iInputLength=0, iFoundEndOfString = 0;
((iInputLength < 25)&&(iFoundEndOfString == 0));
iInputLength++)
{
if(acDataIn[iInputLength] == 0)
{
iFoundEndOfString = 1;
}
}
/* The position of the last character is 2 less */
/* than the position of the zero */
/* we found the 0 and set our flag as 1 */
/* then the loop-execute was called before the */
/* condition was checked */
iPosOfLastCharInInput = iInputLength - 2;
/* OK we got the string length */
/* Lets use that to reverse the string */
for(iInputPosition = iPosOfLastCharInInput, iOutputPosition = 0;
iInputPosition > 0;
iInputPosition = iInputPosition - 1)
{
acDataOut[iOutputPosition] = acDataIn[iInputPosition];
iOutputPosition = iOutputPosition + 1;
}
/* Cap off the array with a zero to use as a string */
acDataOut[iOutputPosition+1] = 0;
/* Show off */
printf("That is %s backwards\n",acDataOut);
}
We will see easier ways of doing theabove later on but what it shows is that at the end of Part 3 of these etutorials you should be able to do quite interesting things.
Note : I have used a
seperate variable for everything I wanted to know. You can
probably see that there are variables that I could have used for
more than one purpose. The reason I haven't is so that it is
clear what a variable is.
Things to try. Take the above example and replace the first for
loop with a call to a function called strlen(); This is an ANSI
standard C function which returns the lenght in an int.
Make the y / n tester remain in the loop until y is pressed.
Have you got it to work ?
Next some simple maths and onto creating you own functions.
Part 4
This page hosted
by
Get
your own Free
Home Page