What happens when you enter a value of 3? How can it ever leave that loop?

Cheers!

Z

So how should the while statement be constructed in order to make tcode = 3 to input the last of the transaction and end the program?

October 14th, 2012, 08:06 PM

Zaphod_b

Re: Program not ending correctly

For what values of tcode do you want to leave the loop? Write them down. Write all of them down.

For what values of tcode do you want to stay in the loop? Don't try to write all of them down. They are all of the values except the three values that you wrote, right? Can you think of a way to express these values with a mathematical expression involving ">" and "<" and logical operators?

Now write a logic expression that is true for all values for which you want to stay in the loop. That's what goes into the parentheses at the end of do{}while();

Cheers!

Z

October 14th, 2012, 08:25 PM

alex067

Re: Program not ending correctly

Quote:

Originally Posted by Zaphod_b

For what values of tcode do you want to leave the loop? Write them down. Write all of them down.

For what values of tcode do you want to stay in the loop? Don't try to write all of them down. They are all of the values except the three values that you wrote, right? Can you think of a way to express these values with a mathematical expression involving ">" and "<" and logical operators?

Now write a logic expression that is true for all values for which you want to stay in the loop. That's what goes into the parentheses at the end of do{}while();

Cheers!

Z

Values of tcode that would end the loop would just be 3

Values for tcode to stay in the loop would be 1,2

So I would do, }while (tcode > 0 || tcode < 3) ? So for every tcode greater than 0 (to make the tcode choice of 1 and 2 valid) and for every tcode less than 3 (to make tcode choice of 1 and 2 valid)

Isnt the argument inside the while loop have to be false in order to return the value? For example, }while (statement), if the statement = false, then the loop would continue, and if the statement = true, the loop would end?

October 14th, 2012, 09:26 PM

Zaphod_b

Re: Program not ending correctly

Quote:

Originally Posted by alex067

Values of tcode that would end the loop would just be 3

That's just wrong.

...

If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)

If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)

Quote:

Originally Posted by alex067

Isnt the argument inside the while loop have to be false in order to return the value?

I think you are getting it. I would put it like this, which is what I think you mean:
If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.

Cheers!

Z

October 14th, 2012, 10:03 PM

alex067

Re: Program not ending correctly

Quote:

Originally Posted by Zaphod_b

That's just wrong.

...

If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)

If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)

I think you are getting it. I would put it like this, which is what I think you mean:
If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.

Cheers!

Z

So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:

}while (tcode > 0 || tcode < 3);

this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?

If this is true, wouldn't any value greater than 3 terminate the program?

October 14th, 2012, 10:04 PM

alex067

Re: Program not ending correctly

Quote:

Originally Posted by Zaphod_b

That's just wrong.

...

If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)

If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)

I think you are getting it. I would put it like this, which is what I think you mean:
If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.

Cheers!

Z

So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:

}while (tcode > 0 || tcode < 3);

this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?

If this is true, wouldn't any value greater than 3 terminate the program?

October 14th, 2012, 10:49 PM

jps

Re: Program not ending correctly

Quote:

Originally Posted by alex067

}while (tcode > 0 || tcode < 3);

this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?

Did you try it? Is that really what the statement means? Try running the statement with -1, 0, 1, 2, 3, 4 and see what happens.

October 15th, 2012, 07:54 AM

Zaphod_b

Re: Program not ending correctly

Quote:

Originally Posted by alex067

So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:

}while (tcode > 0 || tcode < 3);

this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?

That code implies the following:

If tcode is greater than 0, continue the loop.
If tcode is less than 3, continue the loop.
For any other range of values, terminate the loop.

That is really, really not what you want, right?

What you want is:

If tcode is less than zero continue the loop.
If tcode is greater than 3 continue the loop.

As jps says: If you get an idea that you think might work, try it. I can't see any point in asking whether might work. Try it!

Try it with each and every one of the values for which you want to terminate the loop: 1, 2, 3
Try it with various values other than these to make sure it stays in the loop and gives the user a chance to try again. Try some smaller numbers. Try some larger numbers.