These are the main loops I use when I code in any language, so I thought I make a guide on them in C++(works same way in C):
For loops: - These loops are pretty much that same as a while loop except you are able to set the incrementation and set the initial var value.
Loop example:
Explanation:Code:for(int x=1;x <= 10;x++) { cout << x << "\n"; }
for(int x=1;x <= 10;x++) { - means that I am setting an integer variable with the value of 1. While x is less than or equal to 10, it will do what is defined within the braces. x++ increments x by 1 each time, the loop statement is true.
cout << x << "\n"; - That will output the value of x "\n" means go to a new line
} - That closes the loop
While loops: - This is basically does what is defined within the loop until the loop statement is no longer true
Loop Example:
Loop Explanation:Code:int x = 1; while(x <= 10) { cout << x << "\n"; x++; }
int x = 1; - creates an integer named "x" with the value of 1
while(x <= 10) { - means while x is less than or equal to 10
cout << x << "\n"; - it will print the value of x along with the newline
x++; - increments x by 1 value
} - closes the loop


LinkBack URL
About LinkBacks




Reply With Quote







im a code-warrior, see my avatar

Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum