• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner if loop code  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can i write inner if loop without { and } if it has more lines of code as below


I tested to see outer if which does not need { and } even though it has multiple lines in that
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need braces if your if statement has more than one line. The code you posted won't compile.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important that you don't confuse "statements" with "lines".

The syntax of the if-statement is roughly:
if (expression) statement

Your outer if statement uses num < 10 as its expression, and the inner if-statement as its statement. The inner if-statement uses num % 2 == 0 as its expression, and the println(); as its statement. The two else-clauses are both orphaned because they don't fit this pattern:
if (expression) statement else statement

This program won't compile. If you remove one of the println(); statements, the program should be valid.

Statements can always span multiple lines:
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that everything between braces is regarded as one statement, a so called block-statement:
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As above when i put { } for inner if loop no more compilation errors any more.

where as outer if loop never needed { } even it has inner if-else loop.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so below block can be there without any name for it?

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your outer if statement uses num < 10 as its expression, and the inner if-statement as its statement.


Above is clear

The inner if-statement uses num % 2 == 0 as its expression, and the println(); as its statement.


i have two println() as below so both come under one statement right?

The two else-clauses are both orphaned because they don't fit this pattern:

i am not clear on above.

If i write as below


i got output as below

num is between 0 and 10 and odd


which means inner else is not orphaned right?

We can have if statements as many as we want without corresponding else right
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:As above when i put { } for inner if loop no more compilation errors any more.

where as outer if loop never needed { } even it has inner if-else loop.

Because the inner if-else-statement is one statement. if (expression) statement else statement is considered to be one statement, even though it *contains* two more statements.

i have two println() as below so both come under one statement right?

No. They are two separate statements. An if- or else-clause may contain only one statement. If you want more than one statement, you need to wrap them in braces.

If you don't put the braces on the first inner if-statement, the compiler will think the else-clause on line 25 belongs to the if-statement starting on line 20. The else-clause on line 28 then doesn't belong to any if-statement, which is not valid.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the inner if-else-statement is one statement. if (expression) statement else statement is considered to be one statement, even though it *contains* two more statements.


this is clear.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wonder why the rules of inner if loop different from outer if loop which makes to remember all different different rules in different scenarios(including simple if loop) on top of head to master java which makes java bit complex language in my openion. please correct me if i am wrong
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. They are two separate statements. An if- or else-clause may contain only one statement. If you want more than one statement, you need to wrap them in braces.


does this rule apply for both outer if and inner if loop or only specific to inner if loop like this example?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's just one "rule":

if (condition) statement;

It's just that statement can be a block of statements surrounded by braces. I always use the braces, even if there is only one statement.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just that statement can be a block of statements surrounded by braces

you mean like below right

It's just that statement can be a many blocks of statements(since inner if is one block of statement and inner else is other block of statement) surrounded by braces

we have
outer if then
inner if
inner else
outer else
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
outer if //with or without {} compiler is happy
inner if //one block of statement in outer if loop
inner else //second block of statement in outer if loop
outer else


So as above i even if i do not put { } for outer if loop and compiler is happy.

outer if //with or without {} compiler is happy
inner if
{ //since i have { } compiler happy as compiler thinks inner if block as one statement
System.out.println("num is between 0 and 10 and even");
System.out.println("test inner if else without brakets ");
}
//one block of statement in outer if loop
inner else //second block of statement in outer if loop
outer else




outer if ////with or without {} compiler is happy
inner if
//since i have no { } compiler unhappy as compiler thinks inner if block has two statements(ie two sysouts as below)
System.out.println("num is between 0 and 10 and even");
System.out.println("test inner if else without brakets ");
}
//one block of statement in outer if loop
inner else //second block of statement in outer if loop
outer else


Is my understanding is correct?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote: . . .
we have
outer if then
inner if
inner else
outer else
Maybe. Let's put some code tags round your pseudocode… and increase the indentation.…and now we can see the intent of the pseudocode.

Nearly. Remove the “then” because the inner if is part of the outer if.Now you have it correct
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Knute, you are just over complicating everything. There is only an "if", and it executes a "statement" when the condition is true (otherwise it executes the "else" statement). And these "statement" can be a block or even another "if" statement.

All of this details of "outer if", "inner if", "blocks with braces", and "blocks without braces" (which aren't blocks) just confuses everything.

Henry
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference between an "outer" and "inner" if statement. You can have as many ifs nested as you want.

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got if loop has condition and then statement.

And these "statement" can be a block or even another "if" statement.


can 'if' be there without 'else'? which seems correct as below


can if be there without {} if it has more than one line of code? that is my confusion? That is where i feel behaviour of if is differing. please correct me if i am wrong
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The else clause is optional; you are correct.

You cannot have more than one statement in the then or else clause without braces.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot have more than one statement in the then or else clause without braces.


But compiler is happy with more than one statement( one if statement and one else statement)without braces as below
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
But compiler is happy with more than one statement( one if statement and one else statement)without braces as below


An if statement with its optional else statement is classified by the compiler as one statement.

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An if statement with its optional else statement is classified by the compiler as one statement.


this answers my question. Thank you all for guiding me to understand this concept.

I know i complicate things while coding. How to not complicate things while coding. I do get these out of box thoughts all the time may be due to my Mechanical Engg background?
please advise
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And BTW, this code ...

sai rama krishna wrote:



isn't what you think it is. Let me fix the indentation for you...



Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also be careful doing things like this:


From the indentation, you'd think "do something" and "do something else" would always run together, based on the condition. but this is what really happens:

the "do something" runs based on the condition. the "something else" and "third thing" will both always run. This is why you always want to use braces, to make it clear to both the reader and the compiler what is part of the body of the if and what isn't.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense. I will use {} to avoid confusion. I see my eclipse using Eclipse[built-in] formatter as attached. I see other option like Eclipse[built-in] also there. Which one is preferred to use to indent and format code properly in eclipse? please advise
Formatter.jpg
[Thumbnail for Formatter.jpg]
formatter
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find indentation difficult to understand if you use forms like
} else {
Try the Eclipse built-in options, then R‑click the project and you can set a special set of conventions for the project, in Eclipse.

But if you are still at the stage of having to understand if‑elses, you would do better to use a good text editor.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You will find indentation difficult to understand if you use forms like
} else {

I can't see what is wrong or hard to understand with } else {.
It may look weird if you put it out of context but I fail do see any difficulty with understanding this:
By the way, this form of if-else statement is compliant with good old Code Conventions for the Java™ Programming Language .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!