AP Computer Science Unit #2
Conditional Statements (Selection Statements, Control Statements)
1. Lets us choose which statement will be executed next. Conditional statements give us power to make basic decisions
Flow Control
1. The order of statement execution through method is linear, one statement after another. 2. Some programming statements allow us to- decide whether or not to execute a particular statement, or execute a statement over and over 3. These desk skins are based on Boolean expressions (or conditions) that evaluate to true or false 4. The order of statement execution is called the flow of control
The while Statement
while (condition) statement; If the statement is true the condition is executed. The condition is reevaluated, and if it is still true the statement is executed again. The statement is repeated until the condition becomes false. If it is originally false it will never execute. ex. int count = 1; while (count <= 5) { System.out.println(count) count++ }
Logical OR
|| Binary operators (operates on two operands). ex. true || true - true false || false - false true || false - true false || true - true
Infinite Loops
The body of a while loop must eventually make the condition false. If not it is called an infinite loop, which will execute until the user interrupts the program.
Nested Loops
The body of the loop can contain another loop. For each iteration of the outer loo, the inner loop iterates completely.
Decrement Operator
--; subtracts one ex. --count count--
Logic NOT (Logical Negation, Logical Complement)
! Unary operator (operates on one operand).
Logical AND
&& Binary operators (operates on two operands). ex. true && true - true false && false - false true && false - false false && true - false
Increment Operator
++; adds one ex. ++count count++
Assignment Operators
+=; x+=y x=x+y -= *= /= %= The right hand side of the equation is evaluated first and can be a complex equation.
Repetition Statements (Loops)
Allow us to execute a statement multiple times. Controlled by boolean expressions. Three kinds- while loop, do loop (not on AP), for loop.
Boolean Expressions
Equal to == Not equal to != Less than < Greater than > Less than or equal to <= Greater than or equal to >=
Rank of Operators
Highest !, ++, --, (cast) *, /, % +, - <, >, <=, >= ==, != && || +, +=, -=, *=, /=, %= Lowest
Short Circuited Operators
Processing of logical AND and logical OR, if the left operand is sufficient to determine the result, the right operand is not evaluated ex. false && notevaluated- false true || notevaluated - true
public static double random ()
Returns a uniformly distributed value between 0.0 and 1.0, but not including 1.0 EQT: int num = (int) (range * Math.random()) + min; where range = max - min + 1
Block Statements
Several statements grouped together delimited by braces. Can be used wherever a statement is called for in the Java syntax rules. ex. In an if-else statement, the if portion, the else portion, or both could be block statements
Nested Ifs
The statements executed as result of an if or an else statement could be another if statement. An else clause is matched to the last unmatched if. ex. if (condition1) if (condition2) statement1; else statement2; else statement3;
If-Else Statements
ex. if (condition) statement 1; else statement 2;
The for Statement
for (initialization; condition; increment) statement; The increment is executed at the end of each iteration. The initialization section can be used to declare a variable, but the variable can only be used inside the loop and can't be referenced outside the loop. ex. for (int count= 1; count <=5; count++) System.out.println(count);