Download presentation
Presentation is loading. Please wait.
Published byDuane Stevenson Modified over 9 years ago
1
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 4. Controlling Execution junyoung@sparcs SPARCS JAVA Study
2
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems true and false == / != /review/ Java doesn’t allow you to use a number as a boolean.. //! if(a){ … } if(a != 0){ … }
3
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems If-else test(10, 5) → 1 test(5, 10) → -1 test(5, 5) → 0 static void test(int testval, int target) { if(testval > target)//() 안의 값이 true 일 때 result = +1; else if(testval < target) result = -1; else result = 0; }
4
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration Looping control –while / do-while / for while public class WhileTest { static boolean condition(){ boolean result = Math.random() < 0.99; System.out.print(result + “, “); return result; } public static void main(String[] args) { while(condition())// ( condition() == true ) System.out.println(“Inside ‘while’”); System.out.println(“Exited ‘while’”); }
5
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration do – while a = 2; a = 0; do{ a++; }while(a == 1); a = 0; while(a == 1){ a++; }
6
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration for for ( initialization; Boolean-expression; step ) value: 97 character: a value: 98 character: b value: 99 character: c … http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html#isLowerCase(char) public static void main (String[] args) { for(char c = 0; c < 128; c++){ if(Character.isLowerCase(c)) System.out.println(“value: “ + (int)c + “ character: “ + c); }
7
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration for – The comma operator for ( initialization; Boolean-expression; step ) for(int i = 1, j = i + 10 ; i < 5 ; i++, j = i * 2 ){ System.out.println(“i = “ + i + “ j = “ + j); } → i=1j=11 i=2 j=4 i=3 j=6 i=4 j=8
8
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Foreach syntax Java SE5 의 새로운 기능 pubic class ForEachFloat { public static void main (String[] args) { Random rand = new Random(); float f[] = new float[10]; for(int i = 0; i < 10; i++) f[i] = rand.nextFloat(); for(float x : f) System.out.println(x); } for(char c: “An African Swallow”.toCharArray()) System.out.print(c + “ ”); → A n A f r i c a n S w a l l o w
9
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems return 1.method will return any value 2.current method will exit static int test(int testval, int target) { if(testval > target)//() 안의 값이 true 일 때 return +1; else if(testval < target) return -1; else return 0; } slide 3 (if-else) test(10, 5) → 1 test(5, 10) → -1 test(5, 5) → 0
10
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems break and continue Can control the flow of the loop inside the body of iteration statement. i = 0; while(true) {// infinite loop // for(;;) i++; if(i > 10) break;// out of loop System.out.print(“b ”); if(i > 3) continue;// top of loop System.out.print(“a ”); } => b a b a b a b b b b b
11
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems The infamous “goto” Can control the flow of the loop inside the body of iteration statement. outer: outer-iteration { inner-iteration { break; // … continue; // … continue outer; // break out inner-iteration // … // and back to outer-iteration break outer; // break out inner & outer-iteration }
12
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems switch Multiway selection –Selector : only int / char switch(selector){ case ‘a’: case ‘e’: System.out.print(“e”); case ‘u’: System.out.print(“u”); break; default : System.out.print(“else”); } selector 가 ‘a’ / ‘e’ 였다면 :eu selector 가 ‘u’ 였다면 : u selector 가 그 외의 값이면 : else
13
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Homework Exercise 4 / 7 / 9 ch 5. Initialization & Cleanup 읽어오기.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.