Java Programming Assignment Help - Pennsylvania University USA | Java Programming Homework Help - Pennsylvania University USA | Java Programming Project Help - Pennsylvania University USA | Online Java Programming Help - Pennsylvania University USA

In help with java programming we suggest programming tips to use of control flow statements in java programming. In java their are two type of control flow statements under looping : while and do-while. Students ask help in java assignment help pennsylivan university USA. Following is example to better understand the concept of looping while and do while:
while
while statement executes a block of statements until a particular condition is true

Syntax
while (expression) {
//statements
}

The expression in the while statement must evaluates to a boolean value, other wise compiler will throw error. While statement first evaluates the expression, until the expression evaluates to true, it executes the code inside the while block, other wise it comes out of the while loop.

Example : Print numbers from 0 to 9 using while loop.
class WhileEx{
public static void main(String args[]){
int var1 = 0;
while(var1 < 10){
System.out.println(var1);
var1++;
}
}
}

Output
0
1
2
3
4
5
6
7
8
9

do-while
do-while is also a loop construct like while, but evaluates its expression at the bottom of the loop instead of the top.

Syntax
do {
//statement(s)
} while (expression);

Example
class DoWhileEx{
public static void main(String args[]){
int var1 = 0;
do{
System.out.println(var1);
var1++;
}while(var1<10);
}
}

Output
0
1
2
3
4
5
6
7
8
9

Some points to Remember
Students confuse in makking difference between while and do while looping so answer is :
Statements within the do block are always execute at least once even if the expression evaluates to false.

Example
class CompareLoop{
public static void main(String args[]){
boolean flag = false;

while(flag)
System.out.println("I am in while loop");
do
System.out.println("I am in do-while loop");
while(flag);
}
}

Output
I am in do-while loop

Observation
Note that do-while executed, even if the expression “flag” is false. And if you have only one statement, then no need to put the line in between blocks{ }

 

Control flow statements : looping : for
The for statement provides a compact way to iterate over a range of values.

Syntax
for(initialization; condition; update){
//statements to be executed
}

for loop execute the statements until the condition evaluates to false.

Initialization section executed only once, at the starting of loop.
Condition section evaluated every time, before executing the loop statements
update section executes immediately after every execution of loop finishes.

Example : Printing numbers from 0 to 9
class ForEx{
public static void main(String args[]){
int var1;
for(var1 = 0; var1 < 10; var1++){
System.out.println(var1);
}
}
}

Output
0
1
2
3
4
5
6
7
8
9
points to remember
You can declare a variable inside the for loop itself
for(int i=0; i<10; i++) is a valid construct

for( ; ; )
Above for loop run for infinite times.

 

Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:


John Doe

JAVA EXPERT(10+ Yrs Exp.)

Larry Doe

C EXPERT(12+ Yrs Exp.)

Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)