Lost Password?

  #1 (permalink)  
Old 12-09-2006, 05:47 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,633
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
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:
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:
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:
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:
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:
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;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
Custom Control McMillan0520 C# Programming 0 08-08-2007 10:12 AM
website control panel monsurvey1 Introductions 5 01-26-2007 05:22 AM
Standards without control? martinig General Programming 3 01-18-2007 09:57 AM
Custom control properties Ronin C# Programming 1 09-15-2006 03:17 PM


All times are GMT -5. The time now is 03:17 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads