Shop OBEX P1 Docs P2 Docs Learn Events
Making complicated decisions — Parallax Forums

Making complicated decisions

Does anyone know how I would modify a or b to make it only print one message? I am working on the "make complicated decisions" part on parallax's website.


int main() // main function
{
int a = 17; // Initialize a variable to 25
int b = 17; // Initialize b variable to 17
print("a = %d, b = %d\n", a, b); // Print all
if((a > b) || (a == 100)) // If a > b OR A == 100
{
print("a might be larger than b, \n"); //Then print this message
print("OR a might be 100, \n"); //Then print this message
print("OR mabey both!\n"); //Then print this message
}
}


Thanks in advance

Comments

  • AribaAriba Posts: 2,690
    edited 2017-04-14 12:20
    You can't print only one message with certain a, b values. If the condition is true all of them get printed out, it's one block of print commands.

    If you want the messages dependent of the values, you need separate IF tests:
      if(a > b)
      {
        print("a is larger than b, \n");
      }
      if(a == 100)
      {
        print("a is 100, \n");
      }
      if((a > b) && (a == 100))
      {
        print("both!\n");
      }
    

    Andy
  • JonnyMacJonnyMac Posts: 9,104
    edited 2017-04-15 16:30
    Your first to if statements have an extra left parenthesis, Andy.
  • AribaAriba Posts: 2,690
    Thank's Jon
    I have it edited
  • Ariba - don't you want some else statements and reordering so that only one is printed?
  • Cluso99Cluso99 Posts: 18,069
    KeithE wrote: »
    Ariba - don't you want some else statements and reordering so that only one is printed?
    Not necessary, but the code will be slower as it has to test each if statement.
    Could also be done with a switch/case method.
  • I just mentioned it since the original post said: "make it only print one message"
Sign In or Register to comment.