Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.

Similar presentations


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

1 CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05

2 Compound Assignment Operators Way of shortening code. Assume, c = 3. c = c + 7; c += 7; //same as above

3 Compound Operators OperatorUseEquivalent to +=c += 7;c = c + 7; -=d -= 4;d = d – 4; *=e *= 5;e = e * 5; /=f /= 3;f = f / 3; %=g %= 9;g = g % 9;

4 More Shorthand You can also use ++ and -- if you wish to only change the integer by 1. For example, d++ is the same as writing d=d+1. It is also the same as writing d += 1 The same holds true for the --.

5 Prefix vs. Postfix Prefix is ++b while postfix is b++. Prefix and postfix operators are used mainly in loops. Whether you use prefix or postfix will determine the outcome of your program.

6 Prefix Increment Example: ++a It would increment by 1, then use the new value of a in the expression in which a resides. int a = 5; b = ++a; What is b? What is a?

7 Postfix Increment Example: a++ It uses the current value of a in the expression in which it resides, then increments by 1. int a = 5; b = a++; What is b? What is a?

8 Decrement The same holds true for the postfix and prefix decrement operators. Depending on whether or not it is pre or post depends on when it gets computed.

9 Ternary Operator Takes in 3 operands. The first leftmost operand is a boolean expression (it can only return true or false) The second (between ? and :) is what is done if it is true. The third is what is done if it returns false. Shorthand for if/else statements.

10 Ternary Operator 2 An example: System.out.println( grade>=60 ? “Passed” : “Failed” ); If the grade is >= 60, it prints out Passed. Else it prints out Failed. ( grade >= 60 ? “Passed” : “Failed”)

11 Precedence Some operators execute before others. This is because they have a higher precedence. Useful if you have more than one mathematical operation taking place on one line.

12 Precedence Chart ++ -- (Postfix)High precedence ++ -- + - (Prefix) * / % + - >= == != ?: = += -= /= */ %=Low precedence

13 Counter-Controlled Repetition Counter-controlled repetition requires: A control variable Initial Value The increment (or decrement) Loop-continuation condition

14 Counting a while loop int counter = 1; while (counter <=10 ) { System.out.println(counter); counter++; } //prints out integers 1 through 10 on a // separate line

15 The for statement for( int count = 1; count <=10; count++) { System.out.println(count); } //same output as the previous while loop

16 For Brackets Once again, the brackets are not required if there is just one line of code below the for statement. If there is more than one line, they are required.

17 For loops Is this legal? for( ; ; ) If so, what does it do? What does it not do?

18 General For format for ( initialization ; loopContinuationCondition ; increment) //should be 1 line { statement/algorithm }

19 Common errors When a for statement’s control variable is declared in the initialization section of the for loop, editing that same variable in the for body will produce an error. In other words, don’t alter the variable inside the body of the for loop. To sidestep this problem, use temp variables and set them equal if need be.

20 Finding Sum int total = 0; for( int i=2; i<=20 ; i+2 ) total += i; System.out.println(“Sum is “ + total ); /* Above finds the sum of all even numbers 2 through 20 and prints it out */

21 Do…while statement int counter = 1; do { System.out.println(counter); counter++; } while ( counter <= 10 ); //notice the ; // Same as while statement a few slides ago

22 Braces on do…while Although you do not need braces on a do while statement if it is only 1 line, it is common to add them to help readability and to separate between the while and the do.

23 Do…While Every do while statement can be written as a for statement or a while statement. Most people opt for the for loop instead of writing a do…while loop.

24 Switch statement Can only compare integer values Useful if there are several different conditions. See the SampleSwitch.java code

25 switch switch (integer value) case 1: case 2: do something here break; default: do something here break;

26 Switch Defaults Goes to default if it doesn’t match anything else. You should always have a default after the last case. The last case in a switch statement doesn’t need a default, although most programmers add it for clarity.

27 Break A break statement will “break” out of the loop it was in. for( int i=1; I <=10; i++) { if (i == 5) //if i == 5, break; // break out of for loop System.out.println( i ); }

28 Continue Continue statements can be avoided by using structured programming. Therefore, they will not be covered in this course.

29 Assignment #2 Tax withholding calculator Due Thursday May 26 th Description available online at course website http://www.cs.fsu.edu/~cis3931

30 Questions…


Download ppt "CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05."

Similar presentations


Ads by Google