Anda di halaman 1dari 2

Lesson 3: Loops This is the third installment of the Lessons in C programming tutorials created by me, Alexander.

In this lesson I will cover loops. Loops basically do what it sound s like, loop. If you have read lesson 2 you should understand some Boolean expressions. If you do not, you should read it again. When working with loops it is important to under stand truth and false. Maybe you should try doing some truth tables with problems. There are basically 3 types of loops. FOR, WHILE, DO WHILE Each of them has th eir uses. They are all outlined below. FOR - FOR loops are the most useful type, I believe. The layout is for(variable initialization, conditional, incrementing variable) It is very versatile, and t he layout can be changed somewhat. Basically, the variable initialization allows you to e ither declare a variable and give it a value, or give a value to another variable. Se cond, the conditional statement. What it does is it says that while the conditional is tr ue then it would do what in is in the body. Third, the incrementing variable section. It does not have to increment a variable. It can decrement, which is subtracting one, or it can perform various other manipulations on the variable. Ex. #include <iostream.h> //We only need one header file void main() { has one for(int x=0;x<100;x++)/*THE LOOP*/ //added to it every time th e loops { cout<<x<<endl; } } This program is a very simple example of a for loop. x is set to zero, while x is less than 100 do cout<<x<<endl; add 1 to x until the loop ends. Pretty simple to und erstand, but it is a very powerful loop, and much better than WHILE and DO WHILE loops. WHILE - WHILE loops are very simple, but not as useful as FOR loops. The basic structure is...WHILE(true) then do whatever is in the body. The truth could be x==1 or wh ile(x!=7) (x does not equal 7) Ex. #include <iostream.h> //We only need this header file void main() { int x=0; //Of course... //Don't forget to declare variables //Outputting x //We always need this //The loop goes while x<100, and x

while(x<100) //While x is less than 100 do { cout<<x<<endl; //Same output as the above loop x++; //Adds 1 to x every time it repeats } } This was another pretty simple example, but it is longer than the above FOR loop , showing why I like for better than while, though while is a very easy loop to use, so if you are having trouble then you can use it, but try to use for. DO WHILE - DO WHILE loops are useful for only things that want to loop at least once. basically it goes DO { THIS } WHILE(TRUE) Now, it is your turn to try and do a loop! make a DO WHILE loop that does what the above programs do...output 0 to 99! It is not hard, if you have trouble email me at lallain@concentric.net and I will give you some help... Best of luck :) Check b ack again for more programming in C! Note: My homepage is http://www.cprogramming.com. My email is lallain@concentri c.net. Please email me with comments and or suggestions. If you want to use this on your own site please email me and add a link to http://www.cprogramming.com. Thanks :)

Anda mungkin juga menyukai