Question on If / Else if statements

The basic idea for the code below is to generate certain responses based on many different constraints and differing user inputs. The code is only allowed to show 2 responses at most, even though a lot of the constraints overlap leading to 3 or 4 responses. The “if” statement I included if (count >= 2); seems to work, as far as limiting my responses at 2, but I don’t really understand how it is working without a statement. I thought “if” statements needed a “condition” AND a statement? When that condition is met, does the statement execute, but just not do anything since there is no specified action? Is that an acceptable thing to do, as far as good practices go? Any input would be greatly appreciated!

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <iostream>
#include <string>
using namespace std;

int main()
{
      string input2;
      int input1 = 0;
      bool condMet = false;
      int count = 0;

     cout << "Enter Input 1: ";
     cin >> input1;
     cout << input1 << endl;

     cout << "Enter Input 2: ";
     cin >> input2;
     cout << input2 << endl;
     
      if ((input1 == 295) && (input2 == "origin"))
     {
         cout << "bang" << endl;
         condMet = true;
         count++;
     }
      if ((input1 <= 69) && (input2 < "grab"))
     {
         cout << "majority" << endl;
         condMet = true;
         count++;
     }
      if (count >= 2);
      else if ((input1 == 44) && (input2 != "disaster"))
     {
         cout << "greet" << endl;
         condMet = true;
         count++;
     }
      if (count >= 2);
      else if ((input1 % 7 == 0) || (input2 >= "labor"))
     {
         cout << "cereal" << endl;
         condMet = true;
         count++;
     }
      if (count >= 2);
      else if ((input1 > 27) || (input2 == "classroom"))
     {
         cout << "cold" << endl;
         condMet = true;
         count++;
     }
      if (count >= 2);
      else if ((input1 > 15) || (input2.at(0) == 'r'))
     {
         cout << "equal" << endl;
         condMet = true;
         count++;
     }
      if (!condMet)
     {
         cout << "leave" << endl;
     }
      return 0;
}
You don't need to limit anything with a count, or have a counter in place inside your if/else clauses. It simply has no effect. You would always only have one opportunity to input 2 answers, after which the program would exit. Reason being that because there is no do/while or while loop in place, the questions are asked only once.

Also as far as the if statements are concerned, debug your program and see what they do or rather don't do. ;-)
Last edited on
Thanks for the response! I think I am using confusing terminology when I say “2 responses”. My apologies. I meant 2 output responses generated by cout statements in the code, after user input. Also, the counters inside the if statements are working to count and then limit my output, and the code compiles and runs without error. My limited knowledge of If statements just has me confused how it is working without a statement following the condition.
I wrote an answer for you and then accidentally deleted it. :(
Briefly, because I'm out of time for tonight:

I thought “if” statements needed a “condition” AND a statement?

They do, there is one. The statement is the null statement, ;, which does nothing.

You can read all about the syntax here:
http://en.cppreference.com/w/cpp/language/if

The biggest issue with your code is that your predicate count >= 2 and some accompanying code (count++, etc.) is repeated many times. This is a bad thing, because code duplication causes serious issues:
https://en.wikipedia.org/wiki/Don't_repeat_yourself

You should try to refactor that -- it might help to think about the equivalence between
if (a) { if (b) { x; } } and if (a && b) { x; },
and by extension, the equivalence between
if (a) { ; /* do nothing */ } else if (b) { x; } and
if (!a && b) { x; }
No problem!

Though it simply does nothing, not the if statements, not the counters. I tried your code, deleted the count which reaches 1 if one of the else if statements is true and another time when another else if statement is hit.

You never set any conditions that have to be met in the first place, so you can also throw condMet = true; overboard.

Still only two possible outputs based on input, three at most.

Look at it this way. Even though you have numerous conditions set to be met and some might overlap,

else if ((input1 % 7 == 0) || (input2 >= "labor"))

for instance, these conditions by themselves turn out to be either true or false. That is why you can delete all those as condMet = true; Either the conditions cancel themselves out or don't.

Say I enter 28 and marquee, based on that, some conditions would be met, but only the first half. The result is I get cereal as only output. The other half never is true so is skipped and the next condition is checked.

Try it yourself, it should yield the same results.
Last edited on
Thanks for all the info guys. Much appreciated!
Topic archived. No new replies allowed.