JavaBeat
calling cards | international calling cards | phone card
Search JavaBeat

Certification Kits
SCJP 1.5 Exam Questions
SCJP 1.6 Exam Questions
SCWCD 5.0 Exam Questions
SCBCD 5.0 Exam Questions
SCJP 1.5 Links
SCJP 1.5 Home
SCJP Books
SCWCD Books
SCBCD Books
New to SCJP?
SCJP 1.4 or SCJP 1.5?
Objectives
Articles
Mock Exams
Tutorials
Forums
SCJP Exam Voucher
Certification Links
SCJP 1.4
SCJP 1.5
SCJP 1.6
SCWCD 1.4
SCWCD 5.0
SCBCD 5.0
SCEA
SCEA 5.0
JavaBeat
Home
Articles
Tips
Code
QnA
Forums
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
1. Arrays 2. Variables Declarations 3. Constructors 4. Flow Control
5. Assertions 6. Java Keywords 7. Interfaces 8. Package
9. Finalization 10. Gargage Collection-1 11. Exception 12. Gargage Collection-2
13. Polymorphism 14. Encapsulation 15. Passing variables 16. Equals Method
17. Operators 18. Literals 19. Wrapper 20. String and StringBuffer
21. Math class 22. Threads 23. Thread Methods 24. Thread States
25. Thread Synchronization 26. Collections
Flow control statements page 1 of 5


The flow control statements allow you to conditionally execute statements, to repeatedly execute a block of statements, or to just change the sequential flow of control.

if/else statement
The if/else statement is used for decision-making -- that is, it decides which course of action needs to be taken. The syntax is:


if(boolean expression)
{
// do this if the expression is true
}
else
{
// do this if the expression is false
}

The else part in the if/else statement is optional. The curly braces are optional if the body is limited to a single statement. (Note that we cannot use numeric values to represent true and false as we do in C/C++.) For instance:


if (x == 5) {} // compiles, executes body if x is equal to 5
if (x = 0) {} // does not compile, because expression is non-boolean
if (x = true) {} // compiles, but the body is always executed

In the case of nested if/else statements, each else clause belongs to the closest preceding if statement that does not have an else.

switch statement
The switch statement is also used for decision-making, based on an integer expression. The argument passed to the switch and case statements should be int, short, char, or byte. The argument passed to the case statement should be a literal or a final variable. If no case matches, the default statement (which is optional) is executed.

When a break statement is encountered, the control moves out of the switch statement. If no break statement is given, all the case statements are executed until a break is encountered or the switch statement ends. For instance:

	
int x = 1;
switch(x)
{
case 1: System.out.println("one");
case 2: System.out.println("two");
}
// both one and two are printed

Notice the position of the default statement. Though the default statement is usually placed at the end of all the case options, it is not mandatory as you can see in the example below:


int i = 1;
switch(i)
{
default:
System.out.println("Default");
break;
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("One");
break;
}

Note that the control comes to the default statement only if none of the cases match. However, it is a good practice to have the default statement at the end itself.

Loop statements
The three types of Java looping constructs are while, do-while, and for.

while loop
The syntax of the while loop is:


while(boolean expression) {}

The body of the while loop is executed only if the expression is true, so it may not be executed even once:


while(false){} // body never executed

while(1) {} // code will not compile, not a boolean

do-while loop
The syntax of the do-while loop is:


do { } while(boolean expression);

The body of the do-while loop is executed at least once because the test expression is evaluated only after executing the loop body. Also, don't forget the ending semicolon after the while expression.

for loop
The syntax of the for loop is:


for(expr1; expr2; expr3)
{
// body
}

expr1 is for initialization, expr2 is the conditional test, and expr3 is the iteration expression. Any of these three sections can be omitted and the syntax will still be legal:


for( ; ; ) {} // an endless loop

There can be more than one initialization expression and more than one iteration expression, but only one test expression.

break and continue
The break statement is used to exit from a loop or switch statement, while the continue statement is used to skip just the current iteration and continue with the next.

In the case of nested loops, the break statement exits from the innermost loop only. If a break keyword (see Java keywords and identifiers for more information on keywords) is combined with a label, a break statement will exit out of the labeled loop:

	
outer: for (int i = 0; i < 10; i++)
{
while(y > 0)
{
break outer;
}
}
// breaks from the for loop if y > 0
1. Arrays 2. Variables Declarations 3. Constructors 4. Flow Control
5. Assertions 6. Java Keywords 7. Interfaces 8. Package
9. Finalization 10. Gargage Collection-1 11. Exception 12. Gargage Collection-2
13. Polymorphism 14. Encapsulation 15. Passing variables 16. Equals Method
17. Operators 18. Literals 19. Wrapper 20. String and StringBuffer
21. Math class 22. Threads 23. Thread Methods 24. Thread States
25. Thread Synchronization 26. Collections
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details

Favorites
AffiliatedAds.com
Buy movies
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | about us | planetoss
Copyright (2004 - 2008), JavaBeat