-5

A problem I have asks to create a program, where a user is to enter an integer value (between 1 and 4) and depending on the input, a specific output is to be generated.

It must also provide an error to the user when the value entered is not between 1 and 4.

I managed to write it using if and else statements, however, I'm wanting to know if it possible to write this using only if statements.

If and Else statement

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 6
    It would be better to add the code snippet instead of the image. – Hesham Attia Feb 03 '17 at 19:11
  • `if (x) { a } else { b }` = `if (x) { a } if (!x) { b }` Not a very useful thing to do though. Also, you're getting downvoted because posting a screenshot instead of the actual code is pretty lazy – Zong Feb 03 '17 at 19:12
  • Yes, you can avoid `else` statements by making the same checks again. But it is inefficient. Or, you can set a flag variable in each successful statement, as a tell that the input was good. Then the error message checks that tell. – Weather Vane Feb 03 '17 at 19:13
  • Since your input is a range do a check `if (input > 4 || input < 1){ printf("Error"); return 1;}` This will end the program before having to test for specific input. – OrangesV Feb 03 '17 at 19:16
  • Sorry for doing that, I'll keep it mind for next time! (I'm new to stack overflow, so haven't quite figured out how to do that) – Siddart Fredrick Feb 03 '17 at 19:24
  • @OrangesV I was thinking of doing that, but what would happen if the user were to input 1.5? It would still consider it to be between 1 and 4 would it not? – Siddart Fredrick Feb 03 '17 at 19:25
  • If the user inputs `1.5`, your program will only see `1`. You specifically specified for `scanf` to expect an integer, you won't get a floating point number magically. – StoryTeller - Unslander Monica Feb 03 '17 at 19:33

2 Answers2

1

there is a lot of ways to skin this cat! While not using else statements is one of them, setting such a constraint seems unnecessary and makes code less readable and doesn't really do anything for else is equivalent to if ( ! resp_1 == 1 ||... ) also notice that you were redundant in your if statements having a check for if resp_1 was equal to 1 or 2 or 3 or 4 before again checking individually and printing your text.

my suggestion would be to use a switch, it is readable, and quick to implement

switch(resp_1){
  case(1): /*print*/ break;
  case(2): /*print*/ break;
  case(3): /*print*/ break;
  case(4): /*print*/ break;
  defualt: /*Print if not 1,2,3,4*/
}
Adam M.
  • 1,023
  • 1
  • 10
  • 21
  • Yep I would use switch, however, the problem specifically asks to use if statements. Thanks anyways! – Siddart Fredrick Feb 03 '17 at 19:27
  • I would argue that if the question asks for the use of if statements that you should use them, just follow my suggestion to remove the initial check of the value of resp_1 and if you don't want the else, use the idea behind else being a negation of the previous if staments – Adam M. Feb 03 '17 at 19:32
1

If you must use if statements only, then just chain them all.

if        (resp_1 == 1) {
  // Print output for 1
} else if (resp_1 == 2) {
  // Print output for 2
} else if (resp_1 == 3) {
  // Print output for 3
} else if (resp_1 == 4) {
  // Print output for 4
} else {
  // Print error
}

An if-else statement executes (exactly) one branch. And here each else branch is another if-else statement. So, only one branch will be executed; either one of the branches for valid input, or the last catch-all else branch.

And while it's possible to use a flag and write this without a single else clause, IMO it will be far less clear what you intended to happen. When writing code, always go for the option that best captures your intent. Two weeks down the line, if the code requires clarification, it'll be all that easier to explain it to yourself first.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458