IT Community - Software Programming, Web Development and Technical Support

Java:Tutorial - Flow Control

This is a discussion on Java:Tutorial - Flow Control within the Java Programming forums, part of the Software Development category; There are three main types of flow controls. The if...else statements, the switch statements, and the do…while statements ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-24-2007, 12:53 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
Default Java:Tutorial - Flow Control

There are three main types of flow controls. The if...else statements, the switch statements, and the do…while statements which I have already discussed in the loops tutorial. Probably the most common is the if…else statement which you learn about in junior high school.

The basic idea behind the if statement is
Code:
Code:

if(true){
//evaluate code
}
Which is absolutely necessary if you want your program to make any decisions. Between the parentheses a Boolean statement is evaluated, if it is true it will evaluate the code, in this example if it is false it will totally ignore it. What is a Boolean statement you ask? A Boolean statement can be written using any logical operator. For example:
Code:
Code:

int x = 0;
if(x == 0){
//evaluate code
}
Will evaluate the code. Java also has the cabapility to evaluate code if the if statement is false. To do that you will use the else clause.

Code:
Code:

int x = 1;
if(x == 0){
//evaluate code
}else {
//evaluate code
}
In this case, when the if statement is evaluated, it returns false and then the else code is executed. To allow even more flexibility, Java has an else if statement also.
Code:
Code:

int day = 2;
if(day == 1){
System.out.println(“Monday”);
}else if(x == 2 {
System.out.println(“Tuesday”);
} else {
//evaluate code
}
Although this is extremely useful, depending on what you need to do, there is a more efficient way, the case/switch mechanism. For the example above, to write five more else if statements would work to determine the day of the week but it could be more efficient to create something like this:

Code:
Code:

int day = 2;
switch (day) {
case 1:  System.out.println("Monday"); break;
case 2:  System.out.println("Tuesday"); break;
case 3:  System.out.println("Wednesday"); break;
case 4:  System.out.println("Thursday"); break;
case 5:  System.out.println("Friday"); break;
case 6:  System.out.println("Saturday"); break;
case 7:  System.out.println("Sunday"); break;
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Tutorial - A better looking GUI pranky Java Programming 2 03-08-2007 12:34 PM
Java:Tutorial - The Variable pranky Java Programming 0 02-24-2007 12:59 AM
Java:Tutorial - Arrays pranky Java Programming 0 02-24-2007 12:54 AM
Java:Tutorial - Getting Started pranky Java Programming 0 02-24-2007 12:48 AM
Java:Tutorial - Tic-Tac-Toe pranky Java Programming 6 02-23-2007 09:48 AM


All times are GMT -7. The time now is 11:27 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0