![Page 1: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/1.jpg)
CIS3931 – Intro to JAVA
Lecture Note Set 3
19-May-05
![Page 2: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/2.jpg)
Compound Assignment Operators
• Way of shortening code.
• Assume, c = 3.
• c = c + 7;
c += 7; //same as above
![Page 3: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/3.jpg)
Compound Operators
Operator Use Equivalent 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;
![Page 4: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/4.jpg)
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 --.
![Page 5: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/5.jpg)
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.
![Page 6: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/6.jpg)
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?
![Page 7: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/7.jpg)
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?
![Page 8: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/8.jpg)
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.
![Page 9: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/9.jpg)
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.
![Page 10: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/10.jpg)
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”)
![Page 11: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/11.jpg)
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.
![Page 12: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/12.jpg)
Precedence Chart
++ -- (Postfix) High precedence
++ -- + - (Prefix)
* / %
+ -
< <= > >=
== !=
?:
= += -= /= */ %= Low precedence
![Page 13: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/13.jpg)
Counter-Controlled Repetition
Counter-controlled repetition requires:
• A control variable
• Initial Value
• The increment (or decrement)
• Loop-continuation condition
![Page 14: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/14.jpg)
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
![Page 15: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/15.jpg)
The for statement
for( int count = 1; count <=10; count++)
{
System.out.println(count);
} //same output as the previous while loop
![Page 16: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/16.jpg)
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.
![Page 17: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/17.jpg)
For loops
• Is this legal?
for( ; ; )
• If so, what does it do?
• What does it not do?
![Page 18: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/18.jpg)
General For format
for ( initialization ; loopContinuationCondition ; increment) //should be 1 line
{
statement/algorithm
}
![Page 19: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/19.jpg)
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.
![Page 20: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/20.jpg)
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 */
![Page 21: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/21.jpg)
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
![Page 22: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/22.jpg)
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.
![Page 23: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/23.jpg)
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.
![Page 24: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/24.jpg)
Switch statement
• Can only compare integer values
• Useful if there are several different conditions.
• See the SampleSwitch.java code
![Page 25: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/25.jpg)
switch
switch (integer value)case 1:case 2: do something here break;default: do something here break;
![Page 26: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/26.jpg)
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.
![Page 27: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/27.jpg)
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 ); }
![Page 28: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/28.jpg)
Continue
• Continue statements can be avoided by using structured programming.
• Therefore, they will not be covered in this course.
![Page 29: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/29.jpg)
Assignment #2
• Tax withholding calculator
• Due Thursday May 26th
• Description available online at course website http://www.cs.fsu.edu/~cis3931
![Page 30: CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05](https://reader036.vdocuments.net/reader036/viewer/2022062407/56649de95503460f94ae3616/html5/thumbnails/30.jpg)
Questions…