Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.

Similar presentations


Presentation on theme: "CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05."— Presentation transcript:

1 CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05

2 Variables - Reminders Variables are Case-Sensitive
It must be a legal identifier - May only begin with a letter, the underscore or a dollar sign. - Consist of letters, digits, underscores, and the dollar sign. - Must not start with a number!

3 Variables - Reminders It must not be one of Java’s reserved keywords. (See Appendix C for a complete list) It must not be a boolean literal – true or false It must not be the reserved word null It can be any length

4 Built-in Data Types char – used for storing single characters
boolean – has two possible values, true or false Integer types: - byte – 8 bits - short – 16 bits - int – 32 bits - long – 64 bits

5 Data-Types continued Floating point types (for storing decimals)
- float – 32 bits - double – 64 bits

6 Declaring Variables int x; //don’t forget the ; char grade = ‘A’;
You can also declare more than one variable in a line. Ex… float x=3.4, y=9.7, z; When declaring on one line, separate by a comma.

7 Control Structures Provide a method by which you can test variables and conditions in your program Provide a method of flow control and execution if statements if/else statements while loops

8 Logical Operators Two types – Relational and Conditional
Relational : Compares two values and determines the relationship between them Examples : > , < , >= , ==

9 Relationship operators
Use Description > op1 > op2 Returns true if op1 is greater than op2 >= op1 >= op2 Returns true if op1 is greater than or equal to op2 < op1 < op2 Returns true if op1 is less than op2 <= op1 <= op2 Returns true if op1 is less than or equal to op2 == op1 == op2 Returns true if op1 and op2 are equal != op1 != op2 Returns true if op1 and op2 are not equal

10 Conditional Operators
Used with relational operators Combines multiple relational operators with a condition such as (and) or (or)

11 Conditional Operators
Use Description && Op1 && op2 Returns true if both op1 and op2 are true || Op1 || op2 Returns true if either op1 or op2 is true ! !op Returns true if op is false

12 If statements Execute if the statement returns true int x = 5;
if (x == 5) { System.out.print(“x = 5”); } { } are optional if only 1 line under if

13 If/Else The else executes only when the if statement fails.
if (x == 5) System.out.println(“x=5”); else System.out.println(“x != 5”);

14 Dangling-else if (x > 5) if (y > 5)
System.out.println(“x and y are > 5”); else System.out.println(“x is <= 5”); What is the output if… x=10 and y=10? If x=10 and y=2?

15 Dangling Else p2 Compiler actually sees… if (x > 5) if (y > 5)
System.out.println(“x and y are > 5”); else System.out.println(“x is <= 5”); To solve this problem, use { }

16 While Loops while executes until false. int product = 3;
while (product <= 100 ) product = 3 * product; product is initially 3, then it goes to 9, 27, 81, 243. Once it gets to 243, it stops because 243 is not <= 100.

17 More while loops { } must be used after the while statement if there is more than 1 line. While loops are extremely useful when you want to obtain correct input from the user. Make user keep entering the information until it is correct.

18 Questions….


Download ppt "CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05."

Similar presentations


Ads by Google