Tutorial 5: Simple flow control

All programs require some for of flow control. Flow control is typically in the forms of either decision making (e.g. Do I need to run code block A or code block B) or loops (e.g. I have to run this block of code 10 times). In keeping with previous blog posts, all sample code is encompassed by jsp scriptlet tags so you can copy and past directly into your JSP pages for experimenting.

1. IF statements

If statements work by evaluating a given expression, then if true, running a certain code block, and if false skipping the code block and continuing.

1.a In this IF statement, we are going to check if 1 is equal to 1. If true, the code will output the word YES.

<%
if (1==1){
	out.println("YES");
}	
%>

YES

1.b In this IF statement, we are now going to check if 1 is equal to 2, which will return false and no text will be output.

<%
if (1==2){
	out.println("YES");
}
%>

1.c.In this IF statement, we are now going to check if 1 is equal to 2, however we will now return something if the expression evaluates to false and output the work NO.

<%
if (1==2){
	out.println("YES");
}
else
{
	out.println("NO");	
}
%>

NO

2 Nested IF statements

In this statement, we will check 1=1 and if true then check if 2=2. Only then will we output yes.

<%
if (1==1){
	if (2==2){
		out.println("YES");
	}
}	
%>

YES

3. IF statements checking both clauses are true

<%
if ((1==1)&&(2==2)){
		out.println("YES");
}
%>

YES

4. IF statements checking if either clauses is true

<%
if ((1==1)||(1==2)){
		out.println("YES");
}
%>

YES

5. WHILE loops

While loops work by running a particular code block while a certain clause is true. In this example, the counter variable is incremented by 1 and output with each run of the loop until the counter is less than or equal to 5 clause equates to false.

*The loop invariant is what causes the loop to exit. In this case, the loop will always exit as the counter will always count up to 6 and cause the clause to return false. If you don’t increment the variable, you will create an infinite loop that will eventually cause your application to run out of memory and crash.

<%
int counter = 0;
while (counter<=5){
	out.println(counter +"<br>");
	counter++;
}
%>

0
1
2
3
4
5

6. FOR loops

For loops work just like while loops, but have some nice clean syntax that is easier to code and helps prevent issues like infinite loops.

For loops are in the format of

for(initialisation; termination; increment){ statements };

When using this version of the for statement, keep in mind that:

  • The initialization expression initializes the loop; it’s executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
<%
for (int i = 0; i<=5; i++){
	out.println(i +"<br>");
}
%>

0
1
2
3
4
5

Leave a Reply

Your email address will not be published. Required fields are marked *