Lesson 8 : Java Control Statements (part 2) :
Published: 2019-07-01 18:33:34

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](https://1.bp.blogspot.com/-ZTtyMXOx5Ag/XRTMM_NsSFI/AAAAAAAAIUw/YiaC51Z3F0cy-4CC-R6p7lq3lGXBikJjwCLcBGAs/s1600/1.png)
![Alt text of image](https://1.bp.blogspot.com/-Onj-H-ZGHWM/XRTO007wGZI/AAAAAAAAIVA/TRb6hDhfEOU_Q8wbmLQg070LOZY_1fZ6gCPcBGAYYCw/s1600/java_for_loop.jpg)
You can try examples [here](https://www.w3schools.com/java/java_for_loop.asp).

__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](https://1.bp.blogspot.com/-8FXlRny3OCs/XRpDFnyjR9I/AAAAAAAAIXg/Xmcr4SqMiqY8YeFYJrbLUO4WsgffLz7RQCLcBGAs/s1600/1.png)
![Alt text of image](https://1.bp.blogspot.com/-FQYn8ry61WU/XRpDkGSdXBI/AAAAAAAAIXo/SUch8vBPtd0me6UqJbqZJ7L9VpMpp7dDACLcBGAs/s1600/2.png)
You can try examples [here](https://www.w3schools.com/java/java_for_loop.asp).

__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](https://1.bp.blogspot.com/-BRy46vIooIg/XRpENwH3CrI/AAAAAAAAIX0/DcRfjiN2dMkO2Rzj91hYNyeW0WaBzuragCLcBGAs/s1600/4.png)
![Alt text of image](https://1.bp.blogspot.com/-kdK1-6YdY-A/XRpF2xzW7eI/AAAAAAAAIYI/EN8oS-ZyTZYd_ro-gtXme76whrh4zIsSACLcBGAs/s1600/3.png)
You can try examples [here](https://www.w3schools.com/java/java_for_loop.asp).

#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;
}