What are the flow control statements in java? , JAVA Programming

Assignment Help:

The flow control statements give you to conditionally execute statements, to repeatedly operate a block of statements, or to just modify the sequential flow of control.

Looping

while, do-while, for

 

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

 

while(i < 5){...}

 

The body of the do-while loop is run 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.

 

do { ... } while(i < 5);

 

The for loop syntax is:

 

for(expr1; expr2; expr3)

{

// body

}

 

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

 

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

Decision making

if-else, switch-case

 

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

 

if (x == 5) {...} else {..}

 

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

 

int i = 1;

switch(i)

{

case 0:

System.out.println("Zero");break; //if break; is omitted case 1: also executed

case 1:

System.out.println("One");break; //if break; is omitted default: also executed

default: System.out.println("Default");break;

}

Branching

break, continue, label:, return

 

The break statement is needed to exit from a loop or switch statement, while the continue statement is needed to skip just the current iteration and continue with the next. The return is used to return from a function based on a condition. The label statements may lead to unreadable and unmaintainable spaghetti code hence should be avoided.

Exception handling

try-catch-finally, throw

 

Exceptions may be used to define ordinary flow control. This is a misuse of the idea of exceptions, which are meant only for exceptional conditions and hence should be avoided.

 


Related Discussions:- What are the flow control statements in java?

Differentiation between a vector and an array, Differentiation between a V...

Differentiation between a Vector and an array . Explain in Brief about the pros and cons of both?

Code java and javascript in liferay, We need a serious programmer who will ...

We need a serious programmer who will code Java and Javascript in Liferay - open to bidding Project Description: Big Data project Prototype in Liferay. Big Data + User onl

Explain preemptive scheduling and time slicing, In preemptive scheduling, t...

In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. In time slicing, a task i

Explain the purpose of the recovery console, Question: a) Give a detai...

Question: a) Give a detailed description of your understanding concerning "Managing User Sessions and Open files". b) What is "Overriding Inheritance" and how can it be do

What is the use of wrapper class, What is the use of Wrapper class You...

What is the use of Wrapper class You can create an object of Wrapper class using a String or a primitive data type Integer num = new Integer (4); or Integer num = n

Create a gui in java, Fill in the necessary parts to create a GUI that use...

Fill in the necessary parts to create a GUI that uses the CardDeck class.  In your GUI you should have a text field for each hand, two buttons and a text area.   Your program s

Inheritance and interfaces to construct type hierarchies, Objectives 1. To...

Objectives 1. To help you become comfortable with using inheritance and interfaces to construct type hierarchies. 2. To familiarize you with programming from specifications. A

Explain java is fully object oriented languages or not, No, java is not ful...

No, java is not fully object oriented language due to it does not support "multiple inheritance" and "pointers" which are used in C++. But, by using Interfaces we can execute multi

What is arithmetic promotion assignment and casting, What is Arithmetic pro...

What is Arithmetic promotion assignment and casting? In an assignment statement, i.e. if there's an equals sign, Java compares the type of the left hand side to the at last typ

Difference between while statement and a do while statement, What is the di...

What is the difference between a while statement and a do while statement? A while statement checks at the starting of a loop to see whether the next loop iteration should occu

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd