Posted by: ahmedashfaque | March 10, 2015

Why Java is so popular? Part 9


So far in part 1, part 2, part 3, part 4, part 5, part 6, part 7 and part 8 we have covered many topics on Java. In this post we will see loop control.

Loops are used to continuously check a condition and do some processing in each looping through the code while the condition is true. Once the condition returns false then the control flow of the program code exits the loop and terminates.

Loops are very useful in many business transactions. For example if a customer needs to be given some discount for each purchase of higher value than a cut off value then the customer can be given some cumulative discount. Similar business logic can be executed using loops.

Let us see one example:

public class myLoop{

public static void main(String args[]) {
int x = 20;

while( x < 25 ) {
System.out.print(“value of x : ” + x );
x++;
System.out.print(“\n”);
}
}
}

The system will print out following values:

value of x : 20

value of x : 21

value of x : 22

value of x : 23

value of x : 24

Explanation: When The program is run, the execution will move into the loop for the first time and will print “value of x : 20” because the current value of x is 20. During processing, the value of x is increased by 1 and so when next time it prints, it will be “value of x : 21”.

 

Advertisements

Responses

  1. […] far in part 1, part 2, part 3, part 4, part 5, part 6, part 7, part 8 and part 9 we have covered many topics on Java. In this post we will see nested if […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: