Advertisement
ProgtestovyManiak

anIdiotsGuideToProgtest_cycles.c

Nov 14th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void forExample() {
  4.   int a=5;
  5.   printf("--------------------------------------------------------------------------------\n");
  6.   printf("forExample:\n");
  7.   printf("--------------------------------------------------------------------------------\n");
  8.  
  9.   /*
  10.   *  for(1;2;3){4}
  11.   * 1:   something that happens at the absolute start
  12.   * 2:   condition
  13.   * 3:   something that happens at the end of every cycle
  14.   * 4:   something that happens every cycle (block of code)
  15.   */
  16.  
  17.   // at the start we create variable with type "int" named "i"
  18.   // and we initialize (set starting value of) the variable to the value 0
  19.   for (int i=0;i<10;i++) {
  20.     // before every cycle starts we check the condition
  21.     // if the condition is true (anything else than 0) we continue to the following block of code
  22.     // if the condition is false (0) we jump out from the for cycle
  23.  
  24.     // the actual code in the block of code
  25.     printf("%d",i);// we print the current value of "i"
  26.  
  27.     // at the end of the block we do the last part of the for statement
  28.     // that is "i++" this adds one to the "i" variable
  29.     // after that the condition is checked once again
  30.   } // end of the for
  31.   // the "i" variable doesn't exist anymore
  32.   // here we move once the condition in for is not satisfied (is false)
  33.  
  34.   // here is another a little more cmplex example:
  35.   // we copy the value of "a" to the newly made "i" variable
  36.   for(int i=a;i<10;i++) {
  37.     //we print the value of "i" and "a" in every cycle
  38.     printf("value of i: %d; value of a: %d\n", i, a);
  39.   }
  40.   // here we print only the value of "a" to show its unchanged
  41.   // "i" does not exist anymore since we are outside of the block it was declared in
  42.   printf("value of a: %d\n", a);
  43.   printf("--------------------------------------------------------------------------------\n");
  44.   printf("end of for example\n");
  45.   printf("--------------------------------------------------------------------------------\n");
  46. }
  47.  
  48. void whileExample() {
  49.   printf("--------------------------------------------------------------------------------\n");
  50.   printf("whileExample:\n");
  51.   printf("--------------------------------------------------------------------------------\n");
  52.   // "while" is similar to "for" but is a little less complex
  53.   // while(condition) { block of code }
  54.  
  55.   // we have to declare and initialie the "i" variable beforehand so we can use it in the while loop later
  56.   int i=0;
  57.  
  58.   // here is the while loop itself
  59.   while(i<10) { // start of the block
  60.     // before we start the code here we have to check the condition
  61.     // if the condition is true (not 0) we continue with the code inside this block
  62.     // if the condition is false (0) we jump behind the end of this block
  63.    
  64.     //actual code in this block of code
  65.     printf("%d\n",i);// we print the current value of "i"
  66.  
  67.     // this while cycle would be infinite without anything that is able to change the condition
  68.     // in this case simple "i++" is enough
  69.     // this means that we will print out values from 0 to 9 included
  70.     i++;
  71.    
  72.     // we reached end of block
  73.     // this means that we have to check the condition and decide if we want to continue
  74.   } //end of the block
  75.   printf("--------------------------------------------------------------------------------\n");
  76.   printf("end of whileExample\n");
  77.   printf("--------------------------------------------------------------------------------\n");
  78.  
  79. }
  80.  
  81. int main() {
  82.   forExample();
  83.   whileExample();
  84.  
  85.   return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement