Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Conditional branching

In Java, an if test is basically the same as the boolean test in a while loop – except instead of saying, “while there’s still beer...”, you’ll say, “if there’s still beer...”

image with no caption

The code above executes the line that prints “x must be 3” only if the condition (x is equal to 3) is true. Regardless of whether it’s true, though, the line that prints, “This runs no matter what” will run. So depending on the value of x, either one statement or two will print out.

But we can add an else to the condition, so that we can say something like, “If there’s still beer, keep coding, else (otherwise) get more beer, and then continue on...”

image with no caption

System.out.print vs. System.out.println

If you’ve been paying attention (of course you have) then you’ve noticed us switching between print and println.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial