Post by raxis on May 8, 2009 18:20:51 GMT -5
So I am pretty sure Revan has not done this tutorial so here we go:
While loops, it is a very common loop for many languages and SDK's. No matter what road of programming you take, while loops will follow you and will generally be the same. So in C++ a while loops is constructed like this:
_______________________________________
while (condition)
{
statement
}
_______________________________________
I.E:
_______________________________________
#include <iostream>
using namespace std;
int tedH = 4;
int robH = 5;
while(tedH != robH)
{
cout << "Ted is taller or smaller then rob" << endl;
}
_______________________________________
Output:
Ted is taller or smaller then rob
_______________________________________
Explanation:
!= : Means not equal too.
while: Condition ( tedH != robH) Statement( cout << "Ted is taller or smaller then rob" << endl;).
You'll notice this spams your console with the output. (console is the black box in which your programs appear)
This is very unattractive and seems pointless right now, but let's put this to better use.
_______________________________________
#include <iostream>
using namespace std;
int addinput;
int final;
while(cin >> addinput)
{
final = addinput + addinput;
cout << final << endl;
}
_______________________________________
Output:
The number you entered + the same number
_______________________________________
Explanation:
So I lied, while(condition), can also be a statement, in this case it is!! So be careful of this, if you did that but added cin >> addinput before while(cin >> addinput), it would run in the console, but you'd get an output as ugly as sin!
So I hope this got your instincts rolling as how to use the while loop, if you have any questions just leave me a message on the forums.
While loops, it is a very common loop for many languages and SDK's. No matter what road of programming you take, while loops will follow you and will generally be the same. So in C++ a while loops is constructed like this:
_______________________________________
while (condition)
{
statement
}
_______________________________________
I.E:
_______________________________________
#include <iostream>
using namespace std;
int tedH = 4;
int robH = 5;
while(tedH != robH)
{
cout << "Ted is taller or smaller then rob" << endl;
}
_______________________________________
Output:
Ted is taller or smaller then rob
_______________________________________
Explanation:
!= : Means not equal too.
while: Condition ( tedH != robH) Statement( cout << "Ted is taller or smaller then rob" << endl;).
You'll notice this spams your console with the output. (console is the black box in which your programs appear)
This is very unattractive and seems pointless right now, but let's put this to better use.
_______________________________________
#include <iostream>
using namespace std;
int addinput;
int final;
while(cin >> addinput)
{
final = addinput + addinput;
cout << final << endl;
}
_______________________________________
Output:
The number you entered + the same number
_______________________________________
Explanation:
So I lied, while(condition), can also be a statement, in this case it is!! So be careful of this, if you did that but added cin >> addinput before while(cin >> addinput), it would run in the console, but you'd get an output as ugly as sin!
So I hope this got your instincts rolling as how to use the while loop, if you have any questions just leave me a message on the forums.