DEV Community

Hamid Mohamadi
Hamid Mohamadi

Posted on

Lesson 8 : Java Control Statements (part 2) :

We discussed about "selection or conditional statements" in the last post . Now , let's continue :
The second category in java statements is :

2- Iteration or Looping statements :

( for , while , do-while )

In iteration statements, the same code fragment execute several times until a specific condition is satisfied . Iteration statement executes the same set of instructions until a termination condition is met .
for :
A for loop executes a statement ( that is usually a block ) as long as the boolean condition evaluates to true . A for loop is a combination of the three elements initialization statement ( or it can be any valid java statement , boolean expression and increment or decrement statement .

Alt text of image
Alt text of image
You can try examples here.

while :
A while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true.

Alt text of image
Alt text of image
You can try examples here.

do-while loop :
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Alt text of image
Alt text of image
You can try examples here.

3- Jump Statements:

In Java jump statements are mainly used to transfer control to another part of our program depending on the conditions. These statements are very useful from the programmer's view because these statements allow alteration of the flow of execution of the program. These statements can be used to jump directly to other statements, skip a specific statement and so on. In Java we have the following three jump statements:

1- break (simple and labeled)
2- continue
3- return

The break statement :
If we want to go out of a loop then we use a break statement. If we use a break statement in a loop then execution will continue with the immediate next statement outside the loop. After a break, all the remaining statements in the loop are skipped. The break statement can be used in a while loop, for loop, do-while loop, and in a switch case.
Syntax :
if(condition)
{
break;
}

The continue statement :
The continue keyword is used mainly with loops. When we do not want to execute some statements then we use a continue statement to skip those statements to execute. If the continue statement is confronted in the program then it will start the next iteration. It does not terminate the loop, it just skips some part of the loop. The execution again starts from the top of the loop. In some ways it is similar to the break statement.
Syntax :
if(condition)
{
continue;
}

The return statement :
The return statement is the last jump statement. The return statement is used to end the execution of a specific method and then return a value. When we use a return statement in our program then it sends the program control to the method caller. The data type of the returned value should always be equal to the data type of the method's declared return value.
Syntax :
if(condition)
{
return;
}

Oldest comments (0)