AP CS Unit 3: Control Structures Notes The if and if-else Statements. These statements are called control statements because they control whether a particular block of code is executed or not. Some texts refer to them as conditionals because they represent a condition that may be or. x > 8 x = x + 2 n 5 n = n - 1 n = 3 * n Boolean Expressions A Boolean expression is an expression that is either or. For example: x >= 8 The above expression compares x to 8. If x is greater than or equal to 8, then the expression is. Otherwise the expression is. Java uses 6 relational operators in its Boolean expressions. _ Page 1
Logical Operators. More complicated Boolean expressions can be composed through the use of logical operators such as AND and OR. The following table shows how certain expressions can be written. English Mathematically Java A number is greater than 0 and less than or equal to 5. A number is less than 12 or greater than 65 Example. Complete the code to the right based on the flowchart below. k++ k = k + 2 3 < k < 7 k = k - 3 k 5 display k k-- k = 2 * k public class Example { public static void main(string[] args) { Scanner x = new Scanner( System.in ); System.out.print("Enter an integer "); int k = x.nextint(); Page 2
The Math Class. The Math Class contains a collection of standard math functions. Here are some examples on how to use them. To take the square root of a number: double a = Math.sqrt( 16 ); a = Math.sqrt( a ); To raise a number to nth power: double b = Math.pow( 2, 3 ); double c = Math.pow( b, 2.0 ); int d = (int) Math.pow( 3, 4 ); // a equals // a equals // equivalent to, b equals // c equals // d equals Math.pow always returns a double. To take the absolute value: int e = Math.abs( -17 ); double f = Math.abs( -45.8 ); To generate a random decimal between 0 (inclusive) and 1 (exclusive): double g = Math.random(); double h = 5 * Math.random(); // // To generate a random integer from min (inclusive) and max (inclusive), use the following formula: int num = (int) ( range * Math.random() ) + min; // where range = max min + 1 For example, to generate a random integer between -3 and 5: int num = (int) ( 9 * Math.random() ) - 3; // num will be -3 or -2 or 4 or 5 1. a is a random decimal within these limits a < double a = 10 * Math.random() - 3; 2. b is a random integer within these limits b int b = (int)( 5 * Math.random() ) + 3; 3. c is a random integer within these limits c int c = (int)( 12 * Math.random() ); 4. d is a random integer within these limits The abs method is overloaded. This means that are more than one method with the same name but different parameters and return types. d int d = (int)( 3 * Math.random() ) - 7; Page 3
if-else if Statements. The basic if - else structure can be expanded to handle multiple conditionals through the use of else-if statements. For example, if you want to determine if a grade is an A, B, C, D, or E, a simple if - else statement won't work. 0 age<13 age<20 age<120 print "???" print "child" print "teen" print "adult" done get cost get id cost > 100 id = 13 print cost reduce cost by 10% reduce cost by 5% public class Example { public static void main(string[] args) { Scanner sc = new Scanner( System.in ); System.out.print("Enter cost "); double cost = sc.nextdouble(); System.out.print("Enter id "); int id = sc.nextint(); IMPORTANT. Page 4
While Loops. A while loop is a control structure that allows you to write code that is executed repeatedly as long as some condition is. int n = 2; while( n <= 5 ) { System.out.println(n + "cats"); n++; Every pass through the body of a loop is called an. double money = 1000.0; int numyears = 0; Scanner get = new Scanner( System.in ); System.out.print( "Enter your goal: " ); double goal = get.nextdouble(); System.out.println( numyears + " years "); System.out.println( money + " dollars "); You have $1,000 and a 10% interest rate. Calculate how many years must pass until your money has reached (or surpassed) some goal money = 1,000; numyrs = 0; get $ goal from user money < goal calculate interest increase money numyrs++ display numyears and money Page 5
The Break Statement. The break statement causes a loop to end immediately. For example: int x = 0; while ( x >=0) { x = (int)( 100 * Math.random() ); if (x % 10 == 0 ) break; System.out.println( x ); // x // what may be displayed? Block Scope. In our earlier programs, a variable existed from the line where we declared it until the end of the program (remembering that all of our programs consist of just the main method). If a variable is declared within a pair of curly braces (for example, after an if or while statement), it only exists within those curly braces. This is called block scope. public class A{ public static void main( String[] args ){ Scanner in = new Scanner( System.in ); System.out.println( "How many waffles?"); int n = in.nextint(); if ( n > 3 ){ int extra = (int) (5*Math.random()+1 ); n = n + extra; System.out.println( "You get " + n + "!" ); Program A public class B{ public static void main( String[] args ){ Scanner in = new Scanner( System.in ); System.out.println( "How many waffles?"); int n = in.nextint(); if ( n > 3 ){ int extra = (int) (5*Math.random()+1 ); n = n + extra; System.out.println( "You get " + n + "!" ); Program B Which of the above programs compile? Which one does not compile? Would anything change if the curly brackets associated with the if statement in Program A were deleted? Page 6
For Loops. A for-loop is typically used when you need to repeat a process a specific number of times. The first statement in a for-loop contains three statements separated by semicolons: 1. 2. 3. for ( int n = 1; n < 4; n++) { // the body of the loop Example 1. What does this loop display? for ( int k = 5; k <= 8; k++ ){ System.out.print( k + " " ); Example 2. What does this loop display? for ( int n = 25; n >= 10; n = n - 7 ){ System.out.print( n + " " ); Example 3. What does this loop display? for ( int k = 1; k < 10; k++ ){ if ( k % 5 == 0 ) break; System.out.print( k + " " ); Page 7