Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book


And that's it for branching, looping, and creating methods today. This discussion was all about adding more power to your Java arsenal.

We started by taking a look at branching statements that enable you to make decisions in code. You can use if statements in JSP for this purpose: if the condition given in an if statement is true, the code in the body of the if statement is executed, and not otherwise. We also saw that if statements could have else clauses, and that the body of an else clause is executed if the if statement's condition is false. And we saw how to create if-else ladders that could handle multiple true/false conditions.

The switch statement takes up where if-else ladders leave off, because such ladder statements can become very awkward to program when they get large. The switch statement lets you check a data item against one or more case statements, and if there's a match, the code in the body of the matching case statement is executed. We also saw that switch statements could have a default statement with code to execute if no case matched.

You also took a look at the available loop statements today—the for loop, the while loop, and the do-while loop. loop statements let you loop over your code, executing that code multiple times on your data.

As we've seen, the for loop is a very general loop that is most often used with a loop index that you can increment (or decrement) each time through the loop. The while loop keeps executing the code in its body while the condition you give it remains true, and the do-while loop is the same as the while loop, except that the loop's condition is checked at the end of the loop, not at the beginning as in the while loop.

Finally, we took a look at creating methods in JSP. Methods let you organize your code into discrete units, which is great when your code gets long. Ideally, each method should handle one discrete task. You can pass data to methods, process that data, and return other data from methods.

You also took a look at using the built-in methods jspInit and jspDestroy today, which can be used for initializing and cleaning up a Web page. We also saw that when you pass an object to a method, that method is passed by reference, which means that you have direct access to the object.

Tomorrow you're going to start working with a very popular JSP topic—reading the data the user sends you (as typed into text fields in a Web page, for example) in your JSP code. To do that, we're going to start creating some real-world JSP pages.

  • + Share This
  • 🔖 Save To Your Account