What are the main basic control flow statements in java
Java statement is the smallest unit that is a complete instruction in itself. Statement in java generally contain expression and end with a semi-colon. The most commonly used statements in any programming language are as follows:
These statements will execute one after another in a sequential manner.
Control Flow Statements :
These are executed randomly and repeatedly. These are the statements which alter the flow of execution and provide better control to the programmer on flow of execution. It is useful to write better and complex programs.
Control flow statements in java :
if else :
This statement is used to perform a task depending on the condition whether it is true or false.
It will select one option based on the switch expression.
All the switch cases will execute, if there is no break in between them.
while and do-while loop :
This loop is used to when there is a need to execute a group of statements as long as the condition is true. If the loop condition is false, the repetition will be stopped and the flow of execution comes out from the loop.
do-while will execute at least once, even though the condition is false.
for loop :
The for loop is also same as do..while or while loop, but it is more compact syntactically . Initialization,condition checking and increment will take place in a single line.
continue statement is used inside the loop to repeat the next iteration of the loop.But it will ignore all statements and control will go to the next iteration of the loop.
Break is used inside a loop to come out of it.break is used inside the switch block to come out of the switch block.
break can be used in nested blocks to go to end of a block.
Used to return result type (int,float,char….etc) , class or interface.
To return a method result, cannot declare the method return type as void.
|Related Posts :|
|How to use switch control flow statement in java|
|How to use labeled break and continue control flow statements in java|