In this section, we are going to discuss the control statements. Different types of control statements: the decision making statements (if-then, if-then-else and switch), looping statements (while, do-while and for) and branching statements (break, continue and return).
Java Control Statements
- Introduction to Control Statements
In this section, we are going to discuss the control
statements. Different types of control statements: the decision making
statements (if-then, if-then-else and switch), looping statements (while,
do-while and for) and branching statements (break, continue and return).
- Selection Statements
The if statement: To start with controlling statements in Java, lets have a recap over the control statements in C++. You must be familiar with the if-then statements in C++. The if-then statement is the most simpler form of control flow statement.
The "if-else" statement is
an extension of if statement that provides another option when 'if'
statement evaluates to "false" i.e. else block is
executed if "if" statement is false.
Sometimes it becomes cumbersome to write lengthy programs using if and
if-else statements. To avoid this we can use Switch statements in
Lets try to find out what a while statement does. In a simpler
language, the while statement continually executes a block of statements
while a particular condition is true.
It will enter the loop without checking the condition first and checks
the condition after the execution of the statements. That is it will
execute the statement once and then it will evaluate the result
according to the condition.
The concept of Iteration has made our life much more easier.
Repetition of similar tasks is what Iteration is and that too without
making any errors. Until now we have learnt how to use selection
statements to perform repetition.
- Transfer statements
Sometimes we use Jumping Statements in Java.
Using for, while and do-while loops is not always the right idea to use
because they are cumbersome to read.
Continue statement is just similar to the break
statement in the way that a break statement is used to pass program
control immediately after the end of a loop and the continue statement
is used to force program control back to the top of a loop.
It is a special branching statement that
transfers the control to the caller of the method. This statement is
used to return a value to the caller method and terminates execution of