Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter 2008-9 Semester FLOW CONTROL
Flow Control Hold 2 balls in left hand, 1 ball in right Throw ball from left hand to right Throw ball from right Throw ball from left Hold 2 balls in left hand, 1 ball in right Throw ball from left hand to right Push yourself forward, throw your legs up Balance yourself If you re losing balance Throw ball from right Throw ball from left Throw ball from right Press your arms to regain balance Throw ball from right Throw ball from left Throw ball from left If succeeded If not Stand up on your feet
if..else if (boolean expression) { Statements else if (boolean expression) { Statements else if (boolean expression) { Statements else { Statements int greater = 3+2; int smaller = 2*3; if (smaller < greater) { System.out.println( smaller is greater ); else if (smaller <= greater++) { System.out.println( smaller is greatest ); else { System.out.println( greater is smaller );
switch.. case switch(byte/short/char/int value) { case value1: statements case value2: statements default: statements Scanner scanner = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = scanner.nextint(); switch (month) { case 1: System.out.println( January ); case 2: System.out.println( February ); case 3: System.out.println( March );.. case 12: System.out.println( December ); default: System.out.println( Invalid input. );
switch..case vs. if..else Scanner input = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = input.nextint(); switch (month) { case 1: System.out.println( January ); case 2: System.out.println( February );.. case 12: System.out.println( December ); default: System.out.println( Invalid input. ); Scanner scanner = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = scanner.nextint(); if (month == 1) { System.out.println( January ); else if (month == 2) { System.out.println( February ); else.. else if (month == 12) { System.out.println( December ); else { System.out.println( Invalid input. );
switch..case : no breaks! Scanner scanner = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = scanner.nextint(); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numdays = 31; case 4: case 6: case 9: case 11: numdays = 30; case 2: if (((year % 4 == 0) &&!(year % 100 == 0)) (year % 400 == 0) ) { else { default: numdays = 29; numdays = 28; System.out.println("Invalid month."); System.out.println("Number of Days = " + numdays);
while Loop On each iteration (including first), condition is reevaluated Each iteration is done only if condition is true while glass is not empty, take a sip while (boolean expression) { Statements int x = 1024; while (x > 2) { x /= 2; Or: while ((x /= 2) > 2) {
while vs. do..while while do..while Run iteration Check condition If true: run iteration Check condition If true: run iteration do..while iteration runs at least once while iteration may not run at all
for Loop A loop, usually used for counting steps Iteration is done only if condition is true (usually if counter hasn t reached goal) Allows action on each iteration (usually increasing counter) for (initializations; boolean expression; actions) { statements
for Loop, Example Count the sum of all numbers from 1 to 4 int sum = 0; for (int i = 1; i <= 4; i++) { sum += i; System.out.println( Sum of numbers 1-4: + sum); i sum i <= 4 Undefined 0 1 0 True 1 1 2 True 3 3 True 6 4 True 10 5 False Sum of numbers 1-4 is: 10
for vs. while for loop for (int x = 0; x < 10; x++) { System.out.println( x= + x); while loop int x = 0; while (x < 10) { System.out.println( x= +x); x++;
Flow Control Duality
Nested Loops Loops within loops Garbage truck: Go through every street Go through every house in the street Collect garbage Accumulate the garbage weight to the truck weight long garbageintons = 0; for (int currentstreet = 0; currentstreet < numofstreetsintown; currentstreet++) { for (int currenthouse = 0; currenthouse < numofhousesinstreet; currenthouse++) { garbageintons += getgarbage(currentstreet, currenthouse);
Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter 2008-9 Semester FLOW CONTROL