Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: 'if' Statement - Braces

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

Java: '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.
 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy