fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. #define Repeat do{
  5. #define Until(cond) }while(!(cond))
  6.  
  7. void printer(int i)
  8. {
  9. std::cout<<i<<" ";
  10. }
  11.  
  12. int main()
  13. {
  14. int i = 0;
  15.  
  16. //Simulating Pascal/Delphi's repeat until loop.
  17. //This loop will run once and then check the condition.
  18. //After that, it will continue again if the condition is true.
  19. Repeat
  20. std::cout<<i<<" ";
  21. ++i;
  22. Until(i == 10);
  23.  
  24. i = 0;
  25. std::cout<<"\n";
  26.  
  27. //will only run while this condition is true.. i must be less than 10 for the loop to start and continue.
  28. //in other words, the loop will only start and continue if the condition is true.
  29. while (i < 10)
  30. {
  31. std::cout<<i<<" ";
  32. ++i;
  33. }
  34.  
  35. std::cout<<"\n";
  36. i = 0;
  37.  
  38. //will run at least once. The condition is checked after the first run.
  39. //If it is true, the loop will run again and continue running while the condition is true.
  40. do
  41. {
  42. std::cout<<i<<" ";
  43. ++i;
  44. } while(i < 10);
  45.  
  46. std::cout<<"\n";
  47. i = 0;
  48.  
  49. //sets the initial value of J to 0.
  50. //While (J < 10) the loop will run.
  51. //At the end of each iteration, J is incremented.
  52. for (int j = 0; j < 10; ++j)
  53. {
  54. std::cout<<j<<" ";
  55. }
  56.  
  57. std::cout<<"\n";
  58.  
  59.  
  60.  
  61. //You must be using a C++11 compiler to use any of the following loops!!
  62.  
  63. int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  64.  
  65. //for each integer in arr, call lambda function and pass that integer as an argument.
  66. //in this case, the function is [](int i) { std::cout<<i<<" "; }
  67. std::for_each(&arr[0], &arr[10], [](int i) {std::cout<<i<<" ";});
  68. std::cout<<"\n";
  69.  
  70.  
  71. //same as above.. for each integer in arr, call the printer function and pass that integer
  72. //as an argument. Thus every loop it does: printer(i); where i is an integer in arr array.
  73. std::for_each(&arr[0], &arr[10], printer);
  74. std::cout<<"\n";
  75.  
  76. //for-ranged loop.
  77. //for each integer in arr, copy the value to variable i and print it.
  78. for(int i : arr)
  79. {
  80. std::cout<<i<<" ";
  81. }
  82.  
  83. std::cout<<"\n";
  84.  
  85.  
  86. //for each integer in arr, do NOT copy the value. Instead, reference it and print it.
  87. //this allows you to be able to change the value of i and it'd change the value in the array.
  88. for (int &i : arr)
  89. {
  90. std::cout<<i<<" ";
  91. }
  92.  
  93. std::cout<<"\n";
  94.  
  95.  
  96. //for each "something" in "some array", copy it to variable i and print it.
  97. for (auto i : arr)
  98. {
  99. std::cout<<i<<" ";
  100. }
  101.  
  102. std::cout<<"\n";
  103.  
  104.  
  105. //for each "something" in "some array", do NOT copy it. Reference it and print it.
  106. for (auto &i : arr)
  107. {
  108. std::cout<<i<<" ";
  109. }
  110. }
  111.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9