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

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Java Syntax Primitive data types Operators Control statements.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Fourth Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
 Three permissible styles of comments  line comments  e.g. //comments on a line by themselves  block comments  e.g. /*comments on one line or can.
Basic Elements of C++ Chapter 2.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 7 JavaScript: Control Statements, Part 1
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 3: Operators, Expressions and Type Conversion
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
JavaServer Pages: Introduction to JSP Instructor: Enoch E. Damson
elementary programming
Chap 7. Advanced Control Statements in Java
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Presentation transcript:

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

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

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

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

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

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

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

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

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';

Chapter 410 Characters and ASCII/ANSI Code

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

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

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

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;

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

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

Chapter 417 Increment/Decrement Operators: An Example

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();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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’};

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