Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void forExample() {
- int a=5;
- printf("--------------------------------------------------------------------------------\n");
- printf("forExample:\n");
- printf("--------------------------------------------------------------------------------\n");
- /*
- * for(1;2;3){4}
- * 1: something that happens at the absolute start
- * 2: condition
- * 3: something that happens at the end of every cycle
- * 4: something that happens every cycle (block of code)
- */
- // at the start we create variable with type "int" named "i"
- // and we initialize (set starting value of) the variable to the value 0
- for (int i=0;i<10;i++) {
- // before every cycle starts we check the condition
- // if the condition is true (anything else than 0) we continue to the following block of code
- // if the condition is false (0) we jump out from the for cycle
- // the actual code in the block of code
- printf("%d",i);// we print the current value of "i"
- // at the end of the block we do the last part of the for statement
- // that is "i++" this adds one to the "i" variable
- // after that the condition is checked once again
- } // end of the for
- // the "i" variable doesn't exist anymore
- // here we move once the condition in for is not satisfied (is false)
- // here is another a little more cmplex example:
- // we copy the value of "a" to the newly made "i" variable
- for(int i=a;i<10;i++) {
- //we print the value of "i" and "a" in every cycle
- printf("value of i: %d; value of a: %d\n", i, a);
- }
- // here we print only the value of "a" to show its unchanged
- // "i" does not exist anymore since we are outside of the block it was declared in
- printf("value of a: %d\n", a);
- printf("--------------------------------------------------------------------------------\n");
- printf("end of for example\n");
- printf("--------------------------------------------------------------------------------\n");
- }
- void whileExample() {
- printf("--------------------------------------------------------------------------------\n");
- printf("whileExample:\n");
- printf("--------------------------------------------------------------------------------\n");
- // "while" is similar to "for" but is a little less complex
- // while(condition) { block of code }
- // we have to declare and initialie the "i" variable beforehand so we can use it in the while loop later
- int i=0;
- // here is the while loop itself
- while(i<10) { // start of the block
- // before we start the code here we have to check the condition
- // if the condition is true (not 0) we continue with the code inside this block
- // if the condition is false (0) we jump behind the end of this block
- //actual code in this block of code
- printf("%d\n",i);// we print the current value of "i"
- // this while cycle would be infinite without anything that is able to change the condition
- // in this case simple "i++" is enough
- // this means that we will print out values from 0 to 9 included
- i++;
- // we reached end of block
- // this means that we have to check the condition and decide if we want to continue
- } //end of the block
- printf("--------------------------------------------------------------------------------\n");
- printf("end of whileExample\n");
- printf("--------------------------------------------------------------------------------\n");
- }
- int main() {
- forExample();
- whileExample();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement