Hi there,
Although there's already quite good answers here, I thought I'd give you the jnfo on what all the statements used actually mean, to help you understand. The code won't run as it is for various reasons, but running the code is not the objective, it's understanding it. So here goes:
As for the first example:
1 2 3 4 5 6 7
|
int num = 1;
while(num < 10)
{
cout << num << " ";
num += 2;
}
cout << end;
| |
int num = 1;
This defines a variable with the name "num" as an integer value. Integer is the datatype used for non-floating point numbers. We need to tell c++ of which type a variable is, so it knows how much space it needs to keep free in the computers memory to store the value.
while(num < 10)
This starts a loop, more specifically a while-loop. Loops are used to repeat certain actions for an arbitrary amount of times and the amount of repetitions is usually determined by an expression, in this case
num < 10
This line could be read as "As long as the "num" variable's value is smaller than 10 (so not equal to ten!), keep doing the following:". The code that has to be repeated is placed inside curly brackets and called a code block:
1 2 3 4
|
{
cout << num << " ";
num += 2;
}
| |
So, as long as "num" is smaller than ten, this code will be executed. cout is a statement that refers to "standard output", usually your screen. This is usually followed by the << operator, also known as the insertion operator. cout << can be read as "send following to the screen". What comes after will be written onto the console, in this case the "num" variable and a blank space. In between another << is put, to let c++ clearly see the difference between the variable and the space. After that, we need to make sure that we increase the value of the num variable, otherwise it will always be smaller than ten, and the while-loop will keep running forever.
num += 2;
Does exactly that. the += operator is the same as saying "take the current value of num, and add (number) to it". In other words, increase the current value of num by the number following the operator.
cout << end;
cout << you already know now, and endl is what is called a manipulator. Manipulators are used to change the forrmat of the data you're sending to the screen. In the case of endl that means adding a linebreak at the position where it's called.
So, the output of this program will be the value of the variable num, for as long as the while() loop runs. It's best to take a bit of paper and write down every step of the loop, keeping track of the values.
1 2 3 4 5 6 7 8
|
int num;
cin >> num;
while(num != -999)
{
cout << num % 25 << " ";
cin >> num;
}
cout << endl;
| |
int num;
This is similar to the first statement above, but it does not assign a value to num immediately. It's like telling c++ "you may come across a number that goes by the name of num, so now you know" Otherwise c++ would tell you it doesn't know that variable if you would call it in the program.
cin >> num;
This ia again similar to the cout << statements, but here we have cin, which is a statement referring to "get input from the keyboard", followed by the insertion operator >>. The statement can be read as "take input from the keyboard and put the value into the variable called num".
while(num != -999)
While-loop again, but with a different expression this time. The != operator mean "not equal to", so the statement says "do the following while the value of num is not equal to minus 999".
1 2 3 4
|
{
cout << num % 25 << " ";
cin >> num;
}
| |
cout you know now, but % is different. % is called the modulus operator and will return the remainder of the division between the number on the left and the number on its right. For instance, 3%2 is 1. this is again followed by a blank space. After that, there is another cin statement, again asking the user to input a number through the keyboard and putting that input into the num variable.
cout << endl;
is the same as above.
That should get you on your way at least, I hope the explanation makes sense.
For further information, check out this websites great tutorial: here : http://cplusplus.com/doc/tutorial/ and do get back to us if you have any more questions.
All the best,
NwN