From: Jonathan W. <jwi...@ea...> - 2001-07-05 02:33:36
|
Edmund, With the compiling, I meant just the basics (which is basically what I know). Make sure to ask questions if needed. When you are ready to go on to Arrays tell me. There are three main kind of loops: for statement while loops do while loops ********** For Loops ********** The for loop/statement is a really powerful tool for programming. One of the most common uses for the for loop is to execute a statement or block of statements a certain number of times. Let's say that you want to cout the number 1 through 10 with a new line in between each. With the for statement you can do it like so: for (int x=1; x<=10; x++) cout << x << endl; The for loop has three parameters. The first parameter is the intialization expression (int x=1), the second the condition (x<=10), and last the iterating expression (x++). In our example, the for loop notices the intialization expression first. Upon executing, a variable x is defined as being 1. Then it follows the following loop: 1. Check condition, break out of loop if false 2. Execute the statement or body thereof (cout << x << endl;) 3. Execute the iterator statement (x++, which is the same as x=x+1;) What if you want a list of numbers starting with 1 and multiplied by 2 each time (1,2,4,8)? You can do the following: for (int x=1; x<=10; x*=2) cout << x << endl; If you want to execute more than one statement use brackets like the following: for (int x=1; x<=10; x*=2) { cout << x << endl; // can add more statments } ********** While Loops ********** A while loop will execute and continue to execute as long as the condition is true. The structure is as follows: while(true) statement; or while(true) { statements; } ********** Do While Loops ********** A do while loop will execute at least once, but after that the condition has to be true. The syntax is: do { statements; } while(condition); Here is a program using the do while loop that will print numbers from 0 to 10: #include <iostream.h> int main() { int x = 0; do { cout << x << "\n"; x++; } while (x < 10) return 0; } -- jw-proph Edmund wrote: >> Sorry for not contributing in a while, although I no longer have the >> motivation >> I used to to learn new c++ stuff (like with STL) and write lessons. I >> am going > > > I know what you mean, but not with C++ but with other stuff. Enthusiasm > tends to mellow out after a while. I suppose this goes with the > territory. > >> start college this fall, majoring in CS; that will hopefully get my >> interest back > > > (Or make you completely bored of programming.. *snicker*) J/king. > Seriously, good luck with college. > >> [x] Compiling >> [ ] "Hello, World!" >> [ ] Variables, Input >> [ ] Arithmetic Operations >> [ ] Conditional Statements >> [x] Loops (For, Do, While) >> [x] Arrays >> [x] Functions >> [ ] Headers >> [x] Pointers >> [x] Pointer Examples >> [x] Classes >> [x] Inheritance with Classes >> > > My list probably has changed since the beginning; but it's mostly > because > I can't remember what I had selected before. I'm not too sure about > compiling, since the basic 'gcc -o hello hello.cpp' usually does > the trick most of the time. > > Edmund > |