Reputation: 61
I am new to programming, so please forgive me if my question is too basic.
For the following code, I don't know how exactly the bool variable "more" works. It says that while loop will do the content of the loop whenever the "more" is true, but
Please help me out with this question!!
vector<double> salaries;
cout << "Please enter salaries, 0 to quit:" << endl;
bool more = true;
while (more)
{
double s;
cin >> s;
if (s == 0)
more = false;
else
salaries.push_back(s);
}
Upvotes: 2
Views: 804
Reputation: 110
vector<double> salaries;
cout << "Please enter salaries, 0 to quit:" << endl;
bool more = true;
while (more)
{
double s;
cin >> s;
if (s == 0)
more = false;
else
salaries.push_back(s);
}
A while
loop will iterate over and over until the condition between the ()
is not met. When you start the cycle you start with bool more = true;
That way you're telling the while(more)
loop to keep iterating while more
is true. Inside the code you ask for input with cin >> s;
and if the input is 0 the variable more
will change to false
, it will iterate again and since while(more)
is awaiting for the more
variable to be true
the condition won't be true
and the cycle will end. If you input other value than 0
the cycle will store that value into the vector<double> salaries
vector.
One way for getting the values that were stored in the vector is:
for(int i = 0; i<salaries.size(); i++){
cout<< salaries[i] << endl;
}
In which case you're telling the compiler to iterate with a variable called i
starting from the value 0
until the value of i
is <
than the value of salaries.size()
, for each iteration, i
will increase and when the condition is no longer met, the cycle will end.
As a recomendation, use the std
namespace for your types, it will be of help in future code to avoid bringing everything from the std
namespace into your code.
std::vector<double> salaries;
std::cout << "Please enter salaries, 0 to quit:" << std::endl;
bool more = true;
while (more)
{
double s;
std::cin >> s;
if (s == 0)
more = false;
else
salaries.push_back(s);
}
and
for(int i = 0; i<salaries.size(); i++){
std::cout<< salaries[i] << std::endl;
}
Upvotes: 0
Reputation: 9770
You have some misunderstandings with how things work in C++. You can think of your program as an abstract machine. Each variable is a storage location that maintains some state. The more
variable is an example of this state. You can think of the variable as a location in memory that maintains the value that you give it. The total program state is allowed to change throughout the duration of the runtime of the program.
Each assignment (the =
operator) sets the state of the variable to the value on the right hand side of the assignment.
So when you say:
bool more = true;
The storage location named more
is set to the value true
. The storage location will remain true
until you assign a new value to it.
Note that in C++ statements are evaluated sequentially. Since the values of the variables may change over time, the order of the statements matters.
Later on when you say:
while (more)
{
// ...
}
The first time that more
is evaluated, it is true
, but again, since more
is a variable, that value may change over time.
Once you are inside the while
loop, the more
variable is conditionally assigned false when the variable s
is equal to 0
. Notice that the ==
is truly an equality check unlike the =
operator.
if (s == 0)
more = false;
Note that you should be aware that s
is of type double
and the literal 0
is of type int
. Fortunately for you, this will work since C++ will automatically promote the int
to a double
type to do the equality comparison. However, it probably makes since to use a literal 0.0
to be clear that you expect for s
to be a double
.
Since the value of s
is dependent on the value that is read from cin
, the equality condition will sometimes be true
and sometimes false
depending on what is entered by the user. But if more
is assigned false
, it when then cause the while
loop to terminate on its next iteration. In other words, when you reach the close brace }
the program will repeat back to the beginning of the while
and try to re-evaluate more
, at that point, when more
is false
the while
loop will end.
Upvotes: 0
Reputation: 5723
Well, it seems that you don't really understand the way your compiler works.
First of all, your computer is not smart or dumb, it's merely a machine and interprets whatever you give them to. So, it all comes to the way you have programmed your program. Having said that we move on:
The
while(condition) {
commands;
}
loop works basically as follows:
condition
whatever that might be at the time it your
program flow enters the loop.commands
are if and only if the previous checked condition was true
.while
loop.commands
have finished it goes again to check the condition
in while
.commands
.while
commands.So, to sum up, your compiler, computer or your digital friend does not check for logical flows in how you name your variables, if false
does not make sense, etc. It merely checks if condition
is true
. That's it.
Finally an infinite loop will occur if the initial condition
was true
when entering the loop and when exiting the loop always continues to be true (not does not change inside the loop because it could change and also get a true
values when exiting the loop).
Upvotes: 0
Reputation: 1575
while(more)
it's job is to run the loop for as long as more boolean
is true
and skip to next instruction when false
.while(condition)
,here condition is checked once for every iteration, and during the iteration, the compiler does not bother to check and skip the rest of the code upon more being false
. the condition is checked only before beginning an iteration.while(true){set of instructions;}
the condition is always true and therefore the block of code is always executed and we call this an Infinite Loop
.Upvotes: 1
Reputation: 311088
In your code snippet variable more
is explicitly set two times: before the loop and inside the loop if s is equal to zero
bool more = true;
while (more)
{
//...
if (s == 0)
more = false;
//..
}
Thus when more will be set to false within the body of the loop
if (s == 0)
more = false;
the loop stops its iterations because the condition in while will not true
while (more)
Take into account that the condition above is equivalent to
while (more == true)
Though there is no great sense to write such a way because variable more is already a boolean expression.
Also take into account that according to the C++ Standard
4.12 Boolean conversions
1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct-initialization (8.5), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.
You could rewrite your code snippet in other way without using variable more. For example
vector<double> salaries;
cout << "Please enter salaries, 0 to quit:" << endl;
double s;
while ( cin >> s && s != 0 )
{
salaries.push_back(s);
}
Or the condition in while could be written even like
while ( cin >> s && s )
So according to the quote of the C++ Standard if s is not equal to 0 then it is converted to bool true. As for expression cin >> s then class std::istream has explicit conversion operator that converts std::cin to boolean value if the stream is not in erroneous state.
Upvotes: 3
Reputation: 3143
Ok, so point by point:
1) The compiler knows that more
is true because on line 3 it says:
bool more = true;
This creates the bool more
and gives it the value true
.
2) more
is then set to false
if s
is equal to zero
. Although more
is true at the beginning of the loop there is nothing to say it can't be changed within the loop (this is called mutability).
3) Because more
gets set to false within the loop, the loop will stop executing. This will only happen if someone enters 0
for the input. If this doesn't happen you are correct, the loop will get run forever.
This is a fairly common while loop construct which allows an arbitrary number of values to be added to the vector salaries
. In your question you hint that positive numbers should not be allowed, it is worth noting that there is nothing in the code enforcing this. Perhaps it would be better to change the line:
if (s == 0)
to:
if (s <= 0.0)
This way the loop will stop executing if a 0
value is entered or if a negative value is entered.
Upvotes: 5
Reputation: 40593
(1): The computer (or the compiler) is not smart enough to connect more
to a literal meaning.
(2): more
can be changed inside the loop, which is what happens when you enter 0
. After changing more
to false
, the condition in while (more)
is re-evaluated. As more
is now false, the loop is exited.
(3): No, more
is not always true, see (2).
Upvotes: 5
Reputation: 17605
The variable more
is explicitly set to true
before the loop is entered. In the loop's body, if more
is set false
, nothing else is executed in the loop's body afterwards. The flow of execution goes again to the beginning of the loop, where the loop's condition is evaluated. As more
is false
, the loop's body is not executed again.
Upvotes: 2