Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

if Statement - Overview


The purpose of the if statement is to make decisions, and execute different parts of your program depending on a boolean true/false value. About 99% of the flow decisions are made with if. [The other 1% of the decisions use the switch/case statement.]

General Forms

The if statement has this form:

Do these statements before.
if (condition) {
    Do this clause if the condition is true.
Do these statements after.


Do these statements before.
if (condition) {
    Do this clause if the condition is true
} else {
    Do this clause if the condition is false
Do these statements after.

Style. It is good programming style to always write the curly braces, {}, altho they are not needed if the clause contains only a single statement. There are two reasons this is good.

  • Reliability. When code is modified, the indentation is such a strong indicator of structure that the programmer may not notice that the addition of a statement at the "correct" indentation level really isn't included in the scope of the if statement. This is a suprisingly common error.
  • Readability. It is faster to read code with the braces because the reader doesn't have to keep in mind whether they are dealing with an un-braced single statement or a braced block.

Braces have been used in most language that have descended from Algol, including C, C++, Java, C# etc because the language designers want to make it easy for programmers in earlier languages to make the transition. Unfortunately, they are extremely error prone, and languages such as Visual Basic and Python have chosen better notation.

Condition is true or false

The value of condition must be true or false (a boolean value). It is often a comparison.

. . .
int    score;     // Integer score on test.
String scoreStr;  // Temporary String form of score input.
String comment;   // Message to the user.

scoreStr = JOptionPane.showInputDialog(null, "Your score?");
score = Integer.parseInt(scoreStr);

if (score < 60) {
    comment = "This is terrible";
} else {
    comment = "Not so bad";

JOptionPane.showMessageDialog(null, comment);
. . .

The code above will display one of two messages, depending on the value of score.

Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]