• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If----else

 
Ash sav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4) For what value of i, the following program will output "Not Greater than 10"
public class Base{
private void test() {
int i ;
if(i > 10)
System.out.println("Greater than 10");
else
if ( i < 10) <br /> System.out.println("Less than 10");<br /> else<br /> if ( i > 20)
System.out.println("Greater than 20");
else
System.out.println("Not Greater than 10");
}
static public void main(String[] a) {
new Base().test();
}
}
Select most appropriate answer.
a) 0
b) 10
c) 11
d) 100
e) None

I compiled this prog and I know the answer is 10 but I'm still confused how? The way i'm thinking is that 10>10 so it should print greate that 10 . Can somebody tell me?
Thanks in advance,
Ash
 
Junilu Lacar
Bartender
Pie
Posts: 7804
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why would you think 10 > 10? (unless, of course, you are a troll)

Originally posted by Ash sav:
The way i'm thinking is that 10>10 so it should print greate that 10 . Can somebody tell me?

 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it prints "Not greater than 10" because 10 is equal to 10:
( 10 == 10 )
( 10 >= 10 )
( 10 <= 10 )
none of these options were in the code, so the "else" statement is executed. it is a trick question, so if you look at it wrong the first time, you will not get it.
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it prints "Not Greater than 10"
beacuse the conditions
10>10
10<10<br /> 10>20
all retun false. Hence it goes to the else clause.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic