0

I've been programming for about a week now and I'm just learning if statements now

I made a simple program that asks the user for a number input (1-4) and depending on what they input it will add/subtract/multiply/divide 2 numbers they enter after they decide what operation they want to perform.

I made an if statement that says if the number is not equal to 1, 2, 3, 4, then it gives an error message and then asks them to re enter their choice (1-4)

However I'm not sure how to redirect the program back to the start of the code after this this way it doesn't just end after the number is re-entered.

I appreciate any help, thanks keep in mind I'm a noob at this :D

6
Contributors
8
Replies
10
Views
9 Years
Discussion Span
Last Post by hsram
0

also say i wanted to list several numbers in one if statement how would i do that?

If (x != 1, 2, 3, 4)

Doesn't seem to be working

0

also say i wanted to list several numbers in one if statement how would i do that?

If (x != 1, 2, 3, 4)

Doesn't seem to be working

&& means "and". || means "or".


Try this:

if (x != 1 && x != 2 && x != 3 && x != 4)

or this:

if (!(x == 1 || x == 2 || x == 3 || x == 4))
1

For your redirecting question, you could use do...while loop:

do {
   // input your code here
} while (x < 1 && x > 5); // If the input isn't in the range of 1-4, it will process the code in the loop block again.
Votes + Comments
good alternate solution
0

Invisal: line 3: x can not be both at the same time. It should have use the || operator, not && :)

My bad :(

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.