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
 
 
Hot Web Programming Job

 

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

[an error occurred while processing this directive]

Java Notes

'if' Statement - Braces

Braces { } not required for one statement

If the true or false part of and if statement has only one statement, you do not need to use braces (also called "curly brackets").

Form

The if statement doesn't need braces if there is only one statement in a part. Here both the true and false parts have only one statement:
   if (condition) 
      one statement to do if condition is true
   else
      one statement to do if condition is false
   

Example 1 - true and false parts

Here is a paintComponent() method both with and without braces which is possible only because each clause contains only one statement.
public void paintComponent(Graphics g) {
  super.paintComponent(g);  // call parent to paint background
  if (marks < 50) {
     g.setColor(Color.red);   // bad marks in red
  }else{
     g.setColor(Color.black); // good marks in black
  }
  g.drawString("Score = " + marks, 10, 50);
}
and now without braces. Altho correct, it is not as safe a style.
public void paintComponent(Graphics g) {
  super.paintComponent(g);  // call parent to paint background
  if (marks < 50)
     g.setColor(Color.red);   // bad marks in red
  else
     g.setColor(Color.black); // good marks in black
  g.drawString("Score = " + marks, 10, 50);
}

Example 2 - only a true part

If there is only a true part of the if statement, it only needs braces if there is more than one statment.
public void paintComponent(Graphics g) {
  super.paintComponent(g); 
  if (marks < 50)
     g.setColor(Color.red);   // bad marks in red
  g.drawString("Score = " + marks, 10, 50);
}
If the if condition is false, this will not set the color, so the default color will be used (black).

Should you always use braces?

If there is one statment, many programs use braces to make the code more robust. This is a safer practice because any later addition of a statement to one of the clauses will require braces. If you don't have the braces with multiple statements, the compiler may not give any error message, but your code will not do what was expected.

What is a statement?

A statement is a part of a Java program. We have already seen some simple statements:
  • Assignment statement (eg, x = 1;).
  • Method call statement (eg, g.setColor(Color.red);).
  • if statement.
There are about ten kinds of statements. Many of them use braces for grouping statements in the same way that the if statement uses braces.

References

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

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

 

 
 
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.