wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about "for" and "if-else" ??" Watch "about "for" and "if-else" ??" New topic
Author

about "for" and "if-else" ??

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
guys this is what i just realised about the
loop (for and while, do-while) vis-a-vis
the if-else . (when we do not give explicit
braces...)
consider the 2 examples
for(int w=0;w<5;w++)
System.out.println(w++);
System.out.println("wwwwww");

if(z<5)
System.out.println("zzzz");
++z;
in the for loop case only the statement (only
one) next to the for(...) is executed in a loop
again and again till the condition exists.
BUT in the if(...) all the statements following
are executed.
so basically when dealing with "for" one can be
complacent while giving any explicit braces.
because if u do not give one then the compiler
assumes it is only for one statement , the one
next one.....
BUT in the case of "if-else", one got to give the
braces explicitly. right ??
i hope my conclusions are correct ? right ??
Simon Whitehouse
Greenhorn

Joined: Dec 10, 2001
Posts: 12
I'm not sure that your conclusions are correct. As I understand it the same thing is happening - the next line is subject to the preceding construct and the third line is always executed once that has been done.
To clarify this you might like to ensure that the value of z is greater than 5. The following line will not be executed but the one after it will.
hth
Simon
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mark
Yes your conclusions are correct.
You need to realize that a block of code can be used anywhere a single statement can be.
In the case of a for or while loop the loop executes the statemtn or block of code immediately following the for statement.
In the case of the if-else statement all it does is control the flow of execution by allowing or not allowing the execution of the block or statement following the if statement. If the statment is true then execution is allowed to continue to the next line. If it is a block the entire block is allowed to execute then the lines following the block are executed, if it is a single statement then that one single statement is executed then all of the lines following it are executed. If the expression is false then the block or single lione is skipped and execution continues with the next line after. Consdier the following:

If someExpression is true then 'block of code' is executed and line A is also executed. However, if someExpression is false then only line A is executed.
The same applies to a single statement instead of a block:

If someExpression is true then line A is executed and line B is also executed. However, if someExpression is false then only line B is executed.
Hope that helps you out

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Always consider putting braces around statements in your for loops and/or ifs. It is much more clear for other people to understand your code and easier to search bugs...
With code blocks like

it is not really easy to see right away that stat_2 is not in the if block...
It is good coding practice to always put braces even if your block only contains one statement. Read the end of 2.1 - Indentation
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
thanks dave.
two questions:
that i do not see in the books etc. that break and continue should be the last statement in their blocks. the compiler does not accept any statement after continue or block (says "statment not reached")
second, break when used in switch is used without any labels. always... just break and no label. label may or may not be used in other loop situations. right ?
Originally posted by Dave Vick:
[B]Mark
Yes your conclusions are correct.
You need to realize that a block of code can be used anywhere a single statement can be.
In the case of a for or while loop the loop executes the statemtn or block of code immediately following the for statement.
In the case of the if-else statement all it does is control the flow of execution by allowing or not allowing the execution of the block or statement following the if statement. If the statment is true then execution is allowed to continue to the next line. If it is a block the entire block is allowed to execute then the lines following the block are executed, if it is a single statement then that one single statement is executed then all of the lines following it are executed. If the expression is false then the block or single lione is skipped and execution continues with the next line after. Consdier the following:

If someExpression is true then 'block of code' is executed and line A is also executed. However, if someExpression is false then only line A is executed.
The same applies to a single statement instead of a block:

If someExpression is true then line A is executed and line B is also executed. However, if someExpression is false then only line B is executed.
Hope that helps you out
[/B]

Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244

...
that break and continue should be the last statement in their blocks. the compiler does not accept any statement after continue or block (says "statment not reached")...

This can be a little misleading. Yes, they should be the last statement in their block, but a better way of saying it might be that they should be the last statement executed in the block you are breaking/continuing. The reason I say this is because you can have a block with in a block. For example:

here the break statement is obviously not the last statemnet in the for block but it is the last one in the inner if block. If, as you pointed out, you try to put something after the break you'll get an unreachable statemnet error. Both the unlabeled break and unlabeled continue behave in a similiar manner. break gets you out of the innermost loop and continues execution following the loop, continue stops executing the current iteration of the loop and begins with the next iteration of the innermost loop.
Labeled breaks and continue do the same thing but allow you to break or continue a loop that is not the innermost one.

second, break when used in switch is used without any labels. always... just break and no label. label may or may not be used in other loop situations. right ?

You can use a labeled break in a switch. What if you had another loop in your switch? Check this code out:

The break is used to exit the switch statement, if it weren't labeled you would just break out of the for loop and then continue executing the next case statement. This is kind of a contrived example because you can always just use two unlabeled breaks to do the same thing - which might be a little more readable, especially if you have a large switch statement and the label would be far above the labeled break.
hope that helps you out

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
that i do not see in the books etc. that break and continue should be the last statement in their blocks. the compiler does not accept any statement after continue or block (says "statment not reached")

Well, putting any code after a break or continue is useless simply because the break or continue statement changes the path of execution. Break says "get out of this block" and go on while continue says "go back to the beginning of this block." In either case, any following lines of code in the block are not going to be executed because of the change in execution flow. The Java compiler catches this case and gives you the "unreachable statement" error.
break when used in switch is used without any labels. always... just break and no label.

This is because execution simply continues "down" a switch statement until the end of the block. Placing breaks allows you to jump out of the block at any give time. Take the following example:

If i is equal to 2, the output looks like this:
2
done
However, if i is equal to 3, the output looks like this:
3
4
done
This is because no break was encountered. Breaks are used to terminate the flow through a switch statement. They're not often used with a label because a switch statement is much like an if-else chain. If this is the case do this, else if this is the case, do this, else.... In almost all cases, you want to execute some chunk of code and then simply go on. A break by itself will alter execution to jump past the end of the switch statement.
I hope this helps clear things up a little.
Corey


SCJP Tipline, etc.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: about "for" and "if-else" ??
 
Similar Threads
More basic concepts
Question about my for loop
Is there any one who can help me solve this?
Enum Question
count # not right ?