understanding the LCV in a Loop

How could I write a LCV to make my loop eventually false?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>

using namespace std;

int main()
{
	int firstnum, secondnum;
	int sum;

	cout << "Enter two integers: ";
	cin >> firstnum >> secondnum;
	cout << endl;

	while (firstnum > secondnum)

		cout << "The first number must"
		<< "be less than the second number.";

	secondnum = secondnum * firstnum;

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
    int firstnum, secondnum;
    int sum; // Are you going to use it?

    cout << "Enter two integers: ";
    cin >> firstnum >> secondnum;
    cout << endl;

    // If the first number is higher than
    // the second number, then it is true.
    // Otherwise, it is false.
    // Use enclosing braces {} to validate
    // the user input. Then, ask the user
    // to input the values again. 
    while (firstnum > secondnum)
    {
	   cout << "The first number must"
		  << "be less than the second number.";

	   cout << "Enter two integers: ";
	   cin >> firstnum >> secondnum;
	   cout << endl;
    }

    // Do you want to display the result? If yes,
    // then use cout.
    secondnum = secondnum * firstnum;

    return 0;
}
Last edited on
ok, I think I get it now, what you are doing is using the brackets to check if the statement is true. Right? I was under the impression that it had to be a true/false statement or something of the sort.
No. The syntax of while has two forms. There is either one statement after the condition (like you have), or a block of multiple statements within "brackets".

See http://www.cplusplus.com/doc/tutorial/control/

Your
1
2
while (firstnum > secondnum)
  cout << "The first number must" << "be less than the second number.";

is exactly the same as:
1
2
3
4
while (firstnum > secondnum)
{
  cout << "The first number must" << "be less than the second number.";
}

Nothing in that loop body can change the condition.


chicofeo's line 27 is in the loop's body and can change the condition.
alright, I'm on board now. The line 27 gives the user the chance to enter a correct input and therefore the gives the program the option to be true or false.
Last edited on
@kromari. Sorry for the confusion. keskiverto explained it better of what I meant. But, yeah, line 27 does what you say.
Topic archived. No new replies allowed.