# nested if has else if

Hi,

Can the nested if can have else if as below?

How many number of nested loops we can have?
Is there is maxmium limit or recommendation on maximum number. please advise
``````if ( user < 19 ) {

if ( user > 16 && user < 19 ) {
System.out.println( "You are 17 or 18");
}
else if {
System.out.println( "16 or younger");
}
else{
}

}
``````
LVL 7
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Can the nested if can have else if as below?
Yes a nested if can have an else-if. Being nested has nothing to do with it.

How many number of nested loops we can have?
You are already confused with nested loops and nested control statements (if-else).

Is there is maxmium limit or recommendation on maximum number. please advise
No there is no min/max recommendation. Purely depends on the problem you are trying to solve.
0
Commented:
If you're confused, it's too many.
0
Commented:
In reality, an infinite amount though you would want to keep it simple. Less code, the better. Yes you can have an else-if in your example.

In the event you are going to have several ages, you might consider using a case statement to help keep things understandable.
0
Commented:
What you have so far should not be nested though. You should just have if, else ifs, and else.
0
Commented:
you might consider using a case statement to help keep things understandable.

.. or even replacing with polymorphism.
0
Commented:
Yes true, but this is a simple else statement. If there is confusion about that there will be a lot more with polymorphism!
0
Commented:
If there is confusion about that there will be a lot more with polymorphism!

Not if coded correctly. I was referring to the fact that if the no of conditionals increases then you may want to consider it. But for a simple example I agree, there is no need.
0
Author Commented:
``````public class aaaa {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(caughtSpeeding(85, false));

}

public static int caughtSpeeding(int speed, boolean isBirthday) {
int result;
if (speed <= 65) {
if (speed <= 60 && isBirthday == true) {
return result = 0;
} else if ((speed > 60 && speed <= 65) && isBirthday == false) {
return result = 1;
}

else {
return result = 0;
}
}

else if (speed <= 85) {

if (speed <= 80 && isBirthday == true) {
return result = 1;
} else if ((speed > 80 && speed <= 85) && isBirthday == false) {
return result = 2;
}

else {
return result = 0;
}
}

else
return result = 2;

}

}
``````

some example as above.

so

if can have nested  if..else if..else if...else
also
else if also can can have nested  if..else if..else if...else

also
else can have nested  if..else if..else if...else

???

and as many as i want right.

0
Commented:
and as many as i want right.

There is no limit on the java programming language / JVM.

Create 200, 2000 or 20000+
0
Senior Developer/ArchitectCommented:
Technically there is a limit because the class file format allows a maximum size of 64k bytes of compiled bytecode per method.
JVM class file format

Jim
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Technically there is a limit because the class file format allows a maximum size of 64k bytes of compiled bytecode per method.

interesting. Although I think later releases of java have increased this limit.
0
Commented:
One limit that you should always bear in mind, is when you've tested for all the conditions that you want to trap.

:)
0
IT Business Systems Analyst / Software DeveloperCommented:
I think your head would explode trying to understand your nested if/else/if/if/else/if well before you reached any language imposed limitations!!
1
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.