Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

if statement  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I am trying to do a past paper question for school. any help would be appreciated with the logic. rewrite the following code to some other code which behaves the same but which has only one if:


my code is


 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not entirely correct.

First i'd suggest to improve indentation, so that would look like:

The question for you. What value variable a has to hold, so the if body would get executed?
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, since the == operator is associative, a == b will always give the same result as b == a. Your
if (a == b && b == a)...
therefore reduces to
if (a == b)...
 
Dor Burd
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I understand that code (which may well be wrong - I'm a beginner) is:

if (a is true){

and if (b is true) {

then x equals x plus one

}

}

So I'd put it as
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dor Burd: thanks for your post. Just a note for future reference: we try not to post complete solutions, but instead help the OP along to find the answer herself. Thanks.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dor Burd wrote:

Since the cat is let out of the bag, please tell us, what is the difference between 1 and 2?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 keystrokes
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is actually a potential difference between the two.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes. I was just looking at the two lins marked 1 and 2, without looking at the beginning two lines.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the help.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:thank you for the help.
You're welcome.

As mentioned earlier, you got logical operator && correct, but checked incorrect operands.

You just simply needed to write:
which means if a is true and b is true.
One tip: from time to time people do common mistake by writting:
don't do that, it is considered as a bad practice as it is quite easy to make mistake and get:
in this matter, you'd assign boolean value true to a and b and if statement block would get executed regardless initial a and b values.

I hope we got everything what is possible from your question
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!