Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# If----else

Ash sav
Ranch Hand
Posts: 55
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();
}
}
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?
Ash

Junilu Lacar
Sheriff
Posts: 11125
160

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
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
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.