Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.

Similar presentations

Presentation on theme: "Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai."— Presentation transcript:

1 Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai

2 Chapter 42 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

3 Chapter 43 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

4 Chapter 44 Variable Declaration dataType variableName; int x, y; char a; String s1;

5 Chapter 45 Assignment String s1; s1= “Hi, there.”; You can declare and initialize a variable in one step: String s2 =“Hi, there.”;

6 Chapter 46 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

7 Chapter 47 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

8 Chapter 48 Numerical Data Types NameRangeStorage Requirement byte short int long float double -2 7 to 2 7 -1 -2 15 to 2 15 -1 -2 31 to 2 31 - 1 -2 63 to 2 63 - 1 -3.4E38 to 3.4E38 -1.7E308 to 1.7E308 1 byte 2 bytes 4 bytes 8 bytes 4 bytes 8 bytes

9 Chapter 49 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';

10 Chapter 410 Characters and ASCII/ANSI Code

11 Chapter 411 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

12 Chapter 412 Arithmetic Operations OperationArithmetic operator Algebraic expression JSP expression Addition+x + y Subtraction-x – yx - y Multiplication*x * y Division/x / y Modulus%x mod yx % y

13 Chapter 413 Shortcut Operators OperatorsExampleEquivalent += -= *= /= %= 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

14 Chapter 414 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;

15 Chapter 415 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

16 Chapter 416 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

17 Chapter 417 Increment/Decrement Operators: An Example

18 Chapter 418 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();

19 Chapter 419 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

20 Chapter 420 Comparison Operators OperatorExampleMeaning == != > >= < <= 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

21 Chapter 421 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

22 Chapter 422 Conditional Statements if (condition) { statement(s); } for example: = 60) { out.println("passed"); } %>

23 Chapter 423 Conditional Statements if (condition) { statement(s); }else{ statement(s); } for example: = 60) { out.println("passed"); }else{ out.println("failed"); } %>

24 Chapter 424 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

25 Chapter 425 For loop for (initialize-control-variable; condition-testing; modifying-condition){ statements; } Example: Welcome to JSP

26 Chapter 426 While loop while (condition-testing){ statements; } Example: int i = 0; While(i < 10){ Out.println("Welcome to JSP "); i--; }

27 Chapter 427 Do loop do{ statements; } while(condition-testing); Example: <% int counter = 1; do { %>

28 Chapter 428 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

29 Chapter 429 Switch Structure switch (switch-expression){ case value1: statements1; break; case value2:statements2; break; … case valueN: statementsN; break; default:statements-for-default-case; }

30 Chapter 430 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

31 Chapter 431 Logical Operators OperatorMeaning ! && || ^ Logical NOT Logical AND Logical OR Logical exclusive

32 Chapter 432 Truth Table for Logical NOT Operator operand!operand true false true

33 Chapter 433 Truth Table for Logical AND Operator operand1operand2operand1 && operand2 false true false true false true false true

34 Chapter 434 Truth Table for Logical OR Operator operand1operand2operand1 || operand2 false true false true false true false true

35 Chapter 435 Truth Table for Logical Exclusive Operator operand1operand2operand1 ^ operand2 false true false true false true false true false

36 Chapter 436 String Manipulation length() charAt(index) String concatenation indexOf() and lastIndexOf() Substring()

37 Chapter 437 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’};

38 Chapter 438 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

Download ppt "Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai."

Similar presentations

Ads by Google