AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1
More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks 2
More operators public class MoreOperators { public static void main(string[] args) { int a = 19; int b = 4; boolean c = true; System.out.println(!c); System.out.println(a!=b); System.out.println(a%b); false true 3 http://apcs.cold.rocks 3
if if(condition) { //BLOCK 1 The condition should be boolean If the condition is true, the block (BLOCK 1) is executed If the condition is false, nothing happens http://apcs.cold.rocks 4
if public class ControlFlow { public static void main(string[] args) { if(true) { System.out.println( red ); if(false) { red System.out.println( blue ); http://apcs.cold.rocks 5
if else if(condition) { //BLOCK 1 else { //BLOCK 2 If the condition is false, else block (BLOCK 2) is executed else should come right after if block http://apcs.cold.rocks 6
if else public class ControlFlow { public static void main(string[] args) { int a=3; int b=5; if(a>b) { else { System.out.print(a); System.out.print( is greater than ); System.out.println(b); 3 is not greater than 5 System.out.print(a); System.out.print( is not greater than ); System.out.println(b); http://apcs.cold.rocks 7
else if if(condition1) { //BLOCK 1 else if(condition2) { //BLOCK 2 else { //BLOCK 3 If CONDITION1 is true, BLOCK1 is executed If CONDITION1 is false, we check CONDITION2 If CONDITION2 is true, BLOCK2. If not, BLOCK3. http://apcs.cold.rocks 8
if else if else if else if else if(condition1) { //BLOCK 1 else if(condition2) { //BLOCK 2 also possible. else if(condition3) { //BLOCK 3 else if(condition4) { //BLOCK 4 else if(condition5) { //BLOCK 5 else if(condition6) { //BLOCK 6 else if(condition7) { //BLOCK 7 else if(condition8) { //BLOCK 8 else { //BLOCK 9 http://apcs.cold.rocks 9
if else if else if else if else public class ControlFlow { public static void main(string[] args) { int score=85; if(score>=90) { System.out.println( You ve got an A ); else if(score>=80) { System.out.println( You ve got a B ); else if(score>=70) { System.out.println( You ve got a C ); else if(score>=60) { System.out.println( You ve got a D ); else { System.out.println( You are F ); You ve got a B http://apcs.cold.rocks 10
Your turn Print what you are supposed to be doing, from the variable time For example, time = 19; > You should be in AP Computer Science class now http://apcs.cold.rocks 11
Input <OPTIONAL> import java.util.scanner; public class ControlFlow { public static void main(string[] args) { Scanner sc = new Scanner(System.in); System.out.println( What s your name? ); String s = sc.nextline(); What s your name? Teemo Good evening, Teemo System.out.print( Good evening, ); System.out.println(s); http://apcs.cold.rocks 12
if else if else if else with input <OPTIONAL> import java.util.scanner; public class ControlFlow { public static void main(string[] args) { Scanner sc = new Scanner(System.in); System.out.println( What s your score? ); int score = sc.nextint(); if(score>=90) { System.out.println( You ve got an A ); else if(score>=80) { System.out.println( You ve got a B ); else if(score>=70) { System.out.println( You ve got a C ); else if(score>=60) { System.out.println( You ve got a D ); else { System.out.println( You are F ); What s your score? 30 You are F http://apcs.cold.rocks 13
while while(condition) { //BLOCK 1 The condition should be boolean Looks just like if BLOCK 1 is repeated if the condition is true Nothing happens if the condition is false Stops when the condition becomes false, or break is executed http://apcs.cold.rocks 14
while public class ControlFlow { public static void main(string[] args) { while(true) { System.out.println( ); http://apcs.cold.rocks 15
while public class ControlFlow { public static void main(string[] args) { int a=1; while(true) { System.out.print( ); System.out.println(a); a=a+1; if(a>5) break; 1 2 3 4 5 http://apcs.cold.rocks 16
while public class ControlFlow { public static void main(string[] args) { int a=1; while(a<=5) { System.out.print( ); System.out.println(a); a=a+1; 1 2 3 4 5 http://apcs.cold.rocks 17
for for(initialization;condition;afterthought) { //BLOCK 1 INITIALIZATION while(condition) { //BLOCK 1 AFTERTHOUGHT Two are equivalent http://apcs.cold.rocks 18
for for(initialization;condition;afterthought) { //BLOCK 1 Works like while, but INITIALIZATION and AFTERTHOUGHT are added. http://apcs.cold.rocks 19
for public class ControlFlow { public static void main(string[] args) { for(int a=1; a<=5; a=a+1) { System.out.print( ); System.out.println(a); 1 2 3 4 5 http://apcs.cold.rocks 20
Abbreviation? a = a + b; a += b; Are equivalent a += 1; a++; Are (almost) equivalent http://apcs.cold.rocks 21
for for(int i=0; i<n; i++) { //THIS BLOCK WILL BE REPEATED n TIMES There are many ways to repeat something n times But this one is the formula http://apcs.cold.rocks 22
Your turn Print all odd numbers between 0 to 100 http://apcs.cold.rocks 23
Nested loops for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { System.out.println( ); How many s? http://apcs.cold.rocks 24
for public class ControlFlow { public static void main(string[] args) { for(int i=0; i<3; i++) { for(int j=0; j<4; j++) { System.out.print( ( ); System.out.print(i); System.out.print(, ); System.out.print(j); System.out.println( ) ); (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) http://apcs.cold.rocks 25
for public class ControlFlow { public static void main(string[] args) { for(int i=0; i<5; i++) { for(int j=0; j<=i; j++) { System.out.print( * ); System.out.println(); * ** *** **** ***** http://apcs.cold.rocks 26
Your turn Print : ***** **** *** ** * Want to challenge? : * ** *** **** ***** http://apcs.cold.rocks 27