Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming — many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times.
FOR – for loops are the most useful type. The syntax for a for loop is:
For (variable initialization; condition; variable update) { Code to execute while the condition is true }
The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself.
WHILE – WHILE loops are very simple. The basic structure is
while (condition) {Code to execute while the condition is true} the true represents a Boolean expression. It can be any combination of Boolean statements that are legal.) Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.
Example:
#include <iostream> Using namespace std; // So we can see cout and endl int main() { int x = 0; // Don't forget to declare variables while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get(); }
Here is an example of a program which has loops (factorial calculator):
/* Source code to find factorial of a number. */ #include <iostream> using namespace std; int main() { int x , factorial=1; cout << "Please enter a non-negative number: " << endl; cin >> x; for(int a=1; a<=x; a++)//HERE IS THE "FOR" LOOP { factorial=factorial*a; } cout << "The factorial of your number is " << factorial << endl; cout << "Would you like to play again? "; string answer; cin >> answer; do { int x , factorial=1; cout << "Please enter a non-negative number: " << endl; cin >> x; for(int a=1;a<=x;a++)//HERE IS THE "FOR" LOOP { factorial=factorial*a; } cout << "The factorial of your number is " << factorial << endl; cout << "Would you like to play again ? "; cin >> answer; }while (answer =="yes");//HERE IS THE "WHILE" LOOP if (answer== "no") { cout << "AS YOU WISH MASTER, MAY THE FORCE BE WITH YOU! " << endl; } return 0; }
Thank’s to http://www.cprogramming.com/ for the support!
-The Admin.
]]>For the ones who doesn’t know what a factorial number is (including me, 20 minutes ago) a factorial number means a function which multiplies a serial of descending natural numbers.
For example:
4! = 4x3x2x1 = 24
So the rule is:
n! = n × (n−1)!
And to do that in a C++ program, we have to develop a bit more complex process than that, so it would look like this:
/* Source code to find factorial of a number. */ #include <iostream> using namespace std; int main() { int x , factorial=1; cout << "Please enter a non-negative number: " << endl; cin >> x; for(int a=1; a<=x; a++) { factorial=factorial*a; } cout << "The factorial of your number is " << factorial << endl; cout << "Would you like to play again? "; string answer; cin >> answer; do { int x , factorial=1; cout << "Please enter a non-negative number: " << endl; cin >> x; for(int a=1;a<=x;a++) { factorial=factorial*a; } cout << "The factorial of your number is " << factorial << endl; cout << "Would you like to play again ? "; cin >> answer; }while (answer =="yes"); if (answer== "no") { cout << "AS YOU WISH MASTER, MAY THE FORCE BE WITH YOU! " << endl; } return 0; }
And also, as you can see in the code, at the end the program will ask the User if he would like to play again or quit, and if he quits a message will show up on his screen.
So the program will basically look like this!
As always I will leave my codes on the GitHub account and if you have any doubts feel completely free to ask me on the comments section!
Have a nice day, and remember to keep practicing
-The Admin.
]]>For the ones who doesn’t know what a factorial number is (including me, 20 minutes ago) a factorial number means a function which multiplies a serial of descending natural numbers.
For example:
4! = 4x3x2x1 = 24
So the rule is:
n! = n × (n−1)!
And to do that in a C++ program, we have to develop a bit more complex process than that, so it would look like this:
/* Source code to find factorial of a number. */ #include <iostream> using namespace std; int main() { int x , factorial=1; cout << "Please enter a non-negative number: " << endl; cin >> x; for(int a=1; a<=x; a++) { factorial=factorial*a; } cout << "The factorial of your number is " << factorial << endl; cout << "Would you like to play again? "; string answer; cin >> answer; do { int x , factorial=1; cout << "Please enter a non-negative number: " << endl; cin >> x; for(int a=1;a<=x;a++) { factorial=factorial*a; } cout << "The factorial of your number is " << factorial << endl; cout << "Would you like to play again ? "; cin >> answer; }while (answer =="yes"); if (answer== "no") { cout << "AS YOU WISH MASTER, MAY THE FORCE BE WITH YOU! " << endl; } return 0; }
And also, as you can see in the code, at the end the program will ask the User if he would like to play again or quit, and if he quits a message will show up on his screen.
So the program will basically look like this!
As always I will leave my codes on the GitHub account and if you have any doubts feel completely free to ask me on the comments section!
Have a nice day, and remember to keep practicing
-The Admin.
]]>