390 likes | 528 Views
Variables and JSP Control Structures. JavaServer Pages By Xue Bai. Objectives. In this chapter, you will: Declare and use variables in JSP script Learn the data types in JSP script Learn control structures in JSP script Use logical operators in JSP script Manipulate strings in JSP script
E N D
Variables and JSP Control Structures JavaServer Pages By Xue Bai Chapter 4
Objectives In this chapter, you will: • Declare and use variables in JSP script • Learn the data types in JSP script • Learn control structures in JSP script • Use logical operators in JSP script • Manipulate strings in JSP script • Declare and use arrays in JSP script • Create and use Enumeration objects Chapter 4
Variables • A variable is a location in the computer's memory where a data value is stored, or a location that references to another location where an actual object resides • There are two types of data types in JSP: primitive data types and classes • For primitive data types, the data value is stored in the location specified by the variable name; for classes, the actual data value is stored somewhere in memory and the data value is reference by using the variable name Chapter 4
Variable Declaration dataType variableName; int x, y; char a; String s1; Chapter 4
Assignment String s1; s1= “Hi, there.”; You can declare and initialize a variable in one step: String s2 =“Hi, there.”; Chapter 4
Naming Variables • A variable name is any valid identifier • An identifier is a series of characters, consisting of letters, digits, and underscores, that does not begin with a digit. JSP script is case sensitive—uppercase and lowercase letters are different, so varname and VARNAME are different identifiers • A variable name can be any length; all of the following are valid identifiers: String s; String aLongVariableNameButStillValid; int an_integer_variable_name; • Variable names cannot contain spaces or dashes Chapter 4
Primitive Data Types • A data type describes the information that a variable stores. For example, int variables store integers, or whole numbers • A variable’s data type also determines how many bytes of memory are required to store that variable • Each data type has a range of values. Memory space is allocated to store each variable according to its data type • JSP provides eight primitive data types. Six of them are numeric; one is character, used for characters in Unicode encoding; and one is Boolean, used for true/false values Chapter 4
Name Range Storage Requirement byte short int long float double -27 to 27 -1 -215 to 215 -1 -231 to 231 - 1 -263 to 263 - 1 -3.4E38 to 3.4E38 -1.7E308 to 1.7E308 1 byte 2 bytes 4 bytes 8 bytes 4 bytes 8 bytes Numerical Data Types Chapter 4
Character Data Type • The character data type is used to represent a single character. • Unlike the string type, a character value is enclosed within single quotation marks. For example, consider the following code: • [ char letter = 'A'; • char numChar = '6'; Chapter 4
Characters and ASCII/ANSI Code Chapter 4
Boolean Data Type • The Boolean data type has two values, true and false • It is used for logical testing using the relational operators • Boolean values are integral part of control structures such as if then statements, while loops, and for loops Chapter 4
Operation Arithmetic operator Algebraic expression JSP expression Addition + x + y x + y Subtraction - x – y x - y Multiplication * x * y x * y Division / x / y x / y Modulus % x mod y x % y Arithmetic Operations Chapter 4
Operators Example Equivalent += -= *= /= %= x += 5 x -= 5 x *= 5 x /= 5 x %= 5 x = x + 5 x = x - 5 x = x * 5 x = x / 5 x = x % 5 Shortcut Operators Chapter 4
Increment/Decrement Operators x++ is equivalent to x = x + 1; ++x is equivalent to x = x + 1; x-- is equivalent to x = x - 1; --x is equivalent to x = x - 1; Chapter 4
Increment/Decrement Operators • If an increment or decrement operator is prefixed to the variable, it is referred to as the preincrement or predecrement operator, respectively • If an increment or decrement operator is suffixed to the variable, it is referred to as the postincrement or postdecrement operator, respectively Chapter 4
Increment/Decrement Operators • Preincrementing or predecrementing a variable causes the variable to increment or decrement, by 1 first, before it is used in the expression • Postincrementing or postdecrementing a variable causes the current value of the variable to be used in the expression in which it appears, then the variable value is incremented or decremented by 1 Chapter 4
Classes • Classes are data types other than primitive data types. You define variables of type of classes in the same way as to define primitive data type variables • Look at the following examples: String message = “Hi, there.”; java.util.Date today = new java.util.Date(); Chapter 4
Classes VS Primitive Data Type • A variable having a type of class is usually called an object • An object encapsulates both data members and methods, which is one of the key features for object-oriented programming languages • Therefore, unlike primitive variable, you can use methods provided by an object to perform computation • A string object, for example, provides many methods, such as, length(), indexOf(), you may use to manipulate the string Chapter 4
Operator Example Meaning == != > >= < <= x == y x != y x > y x >= y x < y x <= y x is equal to y x is not equal to y x is greater than y x is greater than or equal to y x is less than y x is less than or equal to y Comparison Operators Chapter 4
Control Structures • Control structures allow you to specify the order in which statements are executed in a program • Any computing problem can be solved by executing a series of actions in a specific order Chapter 4
Conditional Statements if (condition) { statement(s); } for example: <% if (yourGrade >= 60) { out.println("passed"); } %> Chapter 4
Conditional Statements if (condition) { statement(s); }else{ statement(s); } for example: <% if (yourGrade >= 60) { out.println("passed"); }else{ out.println("failed"); } %> Chapter 4
Loops • Repeated execution of a block of statements • A loop structure contains two parts: the statements to be executed, and the condition that determines whether the execution of the statements continues • The first part is called the loop body, and the second part contains continue-condition • The one-time execution of the loop body is referred to as an iteration of the loop • After each iteration, the condition is reevaluated. If the condition is true, the statements in the loop body are executed again. If the condition is false, the loop terminates and your program continues execution after the loop structure Chapter 4
For loop for (initialize-control-variable; condition-testing; modifying-condition){ statements; } Example: <% for(int i = 0; i<10; i++){ %> Welcome to JSP<br> <% } %> Chapter 4
While loop while (condition-testing){ statements; } Example: int i = 0; While(i < 10){ Out.println("Welcome to JSP<br>"); i--; } Chapter 4
Do loop do{ statements; } while(condition-testing); Example: <% int counter = 1; do { %> <%= counter %> <br> <% }while (++counter <=10); %> Chapter 4
Do Loop and While Loop • The do loop is similar to the while loop • The do loop tests the condition after the body of the loop is performed; therefore, the loop body is always executed at least once • In the while loop, the condition is tested at the beginning of the loop before the body of the loop is performed Chapter 4
Switch Structure switch (switch-expression){ case value1: statements1; break; case value2: statements2; break; … case valueN: statementsN; break; default: statements-for-default-case; } Chapter 4
Break and Continue Statements • The break statement is usually used in a loop structure to stop iteration, or to skip the remaining cases in a switch structure • The continue statement, when executed in a for, while, or do loop, ends the current iteration, and proceeds with the next iteration of the loop Chapter 4
Operator Meaning ! && || ^ Logical NOT Logical AND Logical OR Logical exclusive Logical Operators Chapter 4
operand !operand true false false true Truth Table for Logical NOT Operator Chapter 4
operand1 operand2 operand1 && operand2 false false true true false true false true false false false true Truth Table for Logical AND Operator Chapter 4
operand1 operand2 operand1 || operand2 false false true true false true false true false true true true Truth Table for Logical OR Operator Chapter 4
operand1 operand2 operand1 ^ operand2 false false true true false true false true false true true false Truth Table for Logical Exclusive Operator Chapter 4
String Manipulation length() charAt(index) String concatenation indexOf() and lastIndexOf() Substring() Chapter 4
Arrays • A group of contiguous memory locations • All have the same name and the same type DataType[] arrayName; int c[] = new int[10]; String[] s = new String[5]; char[] classRank = {‘A’, ‘B’,’C’, ‘D’}; Chapter 4
Enumeration • The Enumeration interface has two methods, hasMoreElements() and nextElement() • The method hasMoreElements() tests if an enumeration object contains more elements • It returns true as long as the enumeration object contains at least one more element to provide when the method: nextElement() is called; otherwise, it returns false • The method nextElement() returns the next element of an enumeration object if the enumeration object has at least one more element to provide Chapter 4