6 Replies - 1095 Views - Last Post: 19 November 2012 - 11:24 PM Rate Topic: -----

#1 shaunihunter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-November 12

question: if-then-else to if-then?

Posted 19 November 2012 - 10:34 PM

I have the following if-then-else expression:

if(a > b )
  {

  }
  else
  {
     System.out.println("a is not greater than b");
  }



I need to switch this over to an "if-then" expression however; I can NOT change what the output says...

Can I simply write it out as:

if !(a > b )
  {
    System.out.println("a is not greater than b")
  }


This post has been edited by macosxnerd101: 19 November 2012 - 10:34 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: question: if-then-else to if-then?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,468
  • Joined: 27-December 08

Re: question: if-then-else to if-then?

Posted 19 November 2012 - 10:34 PM

You would have to surround the !(a < b ) with parentheses as well for it to be syntactically correct. Or you could invert the condition by using the <= operator.
Was This Post Helpful? 2
  • +
  • -

#3 Inkmeup626  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 02-October 12

Re: question: if-then-else to if-then?

Posted 19 November 2012 - 10:46 PM

You could switch "a" and "b"

So rather than...
 
if (a > B)/> 
{
}



You could use....
If (b > a) 
{
     System.out.println( "A is not greater than B.");
}



View PostInkmeup626, on 19 November 2012 - 10:42 PM, said:

You could switch "a" and "b"

So rather than...
 
if (a > B)/>/> 
{
}



You could use....
If (b > a) 
{
     System.out.println( "A is not greater than B.");
}



Sorry, didn't mean to put the "/>" at the end of that first example....

It should have looked like...
..........................
So rather than...
If (a > B)/>
{
}


.............................
Sorry for the confusion.
Was This Post Helpful? 1
  • +
  • -

#4 Inkmeup626  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 02-October 12

Re: question: if-then-else to if-then?

Posted 19 November 2012 - 10:56 PM

Stupid IPad...I hope u got what I meant lol
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: question: if-then-else to if-then?

Posted 19 November 2012 - 10:59 PM

View PostInkmeup626, on 20 November 2012 - 11:26 AM, said:

Stupid IPad...I hope u got what I meant lol


Well, its not a problem with your IPad at all...its a bug in the site which sometimes displays these angular brackets /> when it detects a smiley.

You will note that you didn't meant to type 'B' but when you type a 'b' followed by a ')', that automatically converts into a B and sometimes makes it a smiley like this B)/>

I guess that happens more when you try to edit the post, as I have done here :)

regards,
Raghav

This post has been edited by raghav.naganathan: 19 November 2012 - 11:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,742
  • Joined: 19-March 11

Re: question: if-then-else to if-then?

Posted 19 November 2012 - 11:14 PM

View PostInkmeup626, on 20 November 2012 - 12:46 AM, said:

You could switch "a" and "b"

So rather than...
 
if (a > B)/>/>/> 
{
}



You could use....
If (b > a) 
{
     System.out.println( "A is not greater than B.");
}



This misses one case where a is not greater than b, namely, when a==b. So to reverse an inequality of this sort you have to bear in mind that the opposite of > is <=.

This post has been edited by jon.kiparsky: 19 November 2012 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: question: if-then-else to if-then?

Posted 19 November 2012 - 11:24 PM

What if we use a ternary operator


printf("The bigger number is %d",(a>c)?a:c);


Edit: Thanks to the bug, I am not able to use 'b', so please replace 'c' by 'b' in the above code.

regards,
Raghav

This post has been edited by raghav.naganathan: 19 November 2012 - 11:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1