cis3931 – intro to java lecture note set 3 19-may-05
TRANSCRIPT
CIS3931 – Intro to JAVA
Lecture Note Set 3
19-May-05
Compound Assignment Operators
• Way of shortening code.
• Assume, c = 3.
• c = c + 7;
c += 7; //same as above
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;
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 --.
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.
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?
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?
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.
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.
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”)
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.
Precedence Chart
++ -- (Postfix) High precedence
++ -- + - (Prefix)
* / %
+ -
< <= > >=
== !=
?:
= += -= /= */ %= Low precedence
Counter-Controlled Repetition
Counter-controlled repetition requires:
• A control variable
• Initial Value
• The increment (or decrement)
• Loop-continuation condition
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
The for statement
for( int count = 1; count <=10; count++)
{
System.out.println(count);
} //same output as the previous while loop
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.
For loops
• Is this legal?
for( ; ; )
• If so, what does it do?
• What does it not do?
General For format
for ( initialization ; loopContinuationCondition ; increment) //should be 1 line
{
statement/algorithm
}
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.
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 */
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
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.
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.
Switch statement
• Can only compare integer values
• Useful if there are several different conditions.
• See the SampleSwitch.java code
switch
switch (integer value)case 1:case 2: do something here break;default: do something here break;
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.
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 ); }
Continue
• Continue statements can be avoided by using structured programming.
• Therefore, they will not be covered in this course.
Assignment #2
• Tax withholding calculator
• Due Thursday May 26th
• Description available online at course website http://www.cs.fsu.edu/~cis3931
Questions…