Java & J2EE The JSP Files (part 2): Attack Of The Killer Fortune Cookies |
With a little bit of luck, our introductory article on JSP left you so excited that you spent the last few days eagerly practicing variable names and letting your friends know how much smarter you are than them. And this week, we're going to help you cement your reputation still further, by giving you a crash course in JSP's conditional statements and loops. Make sure you're strapped in tight - this is gonna be one hell of a ride!{mospagebreak title=Adding It All Up} You'll remember how, in the first part of this tutorial, we used the + operator to add numbers and strings together. And just as you have the + operator for addition, JSP comes with a bunch of other arithmetic operators designed to simplify the task of performing mathematical operations. The following example demonstrates the important arithmetic operators available in JSP: And here's the output: As with all other programming languages, division and multiplication take precedence over addition and subtraction, although parentheses can be used to give a particular operation greater precedence. For example, returns 48. In addition to these operators, JSP comes with the very useful auto-increment [++] and auto-decrement [--] operators, which you'll see a lot of in the next article. The auto-increment operator increments the value of the variable to which it is applied by 1, while the auto-decrement operator does the opposite. Here's an example:
JSP also comes with a bunch of comparison operators, whose sole raison d'etre is to evaluate expressions and determine if they are true or false. The following table should make this clearer. Assume x=4 and y=10
blog comments powered by Disqus |