------------------------------------------------ "Introduction To C: Chapter 3: Loops" ------------------------------------------------ C/O :: Dr4g of DynamicHell Development Team ------------------------------------------------ http://dynamichell.org | irc.dynamichell.org ------------------------------------------------ From the Previous Chapter, you should be familiar with the following. Print to the screen.(printf) Take in a value & store it(scanf) Create Conditional Statements(IF) Create Further Conditional Statements(switch) This Chapter Will Include ------------------------- The For Loop The While Loop Do While Loop -------------- The For Loop -------------- The for loop is a repetative function, it is used to display the same amount of data over and over. Hence the name Loop. It does the code within the braces, then goes back to the start of the loop and does it again, until the condition is met. Then it would stop looping and carry on with the rest of the program. The For Loop consists of 3 sections. Initialization - Condition - Expression The Syntax ----------- for(initialization ; condition ; expression) { printf("Hello World"); } Example: int i; for(i=0 ; i<10 ; i++) { printf("The Value of i is %d",i); } Explanation ------------ int i; This is standard ofcourse, as we need to declare our variables before we can use them. for ( i=0.... We are setting i to 0 is setting i=0 within our for loop only. If i is another value outside the for loop, they both work independantly. Outside the for loop, i can have a totally different value & relevance. for (i=0 ; i < 10.... We are saying that the condition is keep looping while i is less than 10. The compiler checks that i=0 so it will resolve TRUE and continue with looping. Next stage is expression, remember from chapter 1 we had the ++ Operator? Meaning it will Increment the value of i by 1. So every time this loop occurs, it will ++ (add one) to the value of i. The logic of the loop is something like the following. The value of i is 0 (end loop) (new loop((check condition(i is incemented))) The value of i is 1 (end loop) And So on.. Output ------- The Value of i is 0 The Value of i is 1 The Value of i is 2 The Value of i is 3 The Value of i is 4 The Value of i is 5 The Value of i is 6 The Value of i is 7 The Value of i is 8 The Value of i is 9 Then the progam would end! Overview --------- We now know how to create Iterative Statements (Loops). --------------- The While Loop --------------- The While loop is similar to the for loop, however there are some differences. The while loop will only loop WHILE a condition is met, whereas the for loop, loops UNTIL a condition is met. For instance... Loop While i > 5 Syntax ------- while (expression) { printf("Hello World"); } Example -------- int i = 5; while( i < 10 ) { printf("The Value of i is %d",i); i++ } This would keep looping until i=10 as it is no longer less than 10. You could also have a more complex expression like the following. while ( i < 10 && i > 4 ) This would loop while i < 10 and while i > 4 So the value would need to be from 5 to 9 for it to loop. And & OR -------- If you remmeber from chapter 2 (Operators) The Syntax for And is && and the Syntax for OR is || You could have a while loop using both && and ||. Like so. --------- while (i <= 10 || i >=20) We could also have, while ((i > 5 && i < 10) || (i < 20 && i > 10)) The possibilities are endless ------------------- The Do While Loop ------------------- This loop is almost exactly the same as the standard While loop. However, there is one key difference, in the while loop the condition is tested at the start, so if the condition is not met it will not carry out any of the code. However the do while loop, carrys out the code once, then test the condition at the end. If its met it will do the code again then re-test the condition. If not it will then continue with the rest of the program. Syntax ------- int i = 0; do { printf("The Value of i is %d",i); i++; } while ( i < 5 ) Can you now see the clear difference between the 2 different while loops? If not, then re-read the section on while loops again, Then Continue. If you think there is anything i have missed out to do with for loops and while loops. Then please email them to dragoonis@gmail.com Recap ------ You should understand the following 1) For loops, how they work and why. 2) While loops, and do while loops. 3) The difference when to use a while ,do while and a for loop.