1 / 18

Control Structures

Control Structures. Sequential Execution. x = keyBoard.nextInt();. statement. statement. y = 2*x - 3;. statement. System.out.println(x);. …. …. System.out.println(y);. statement. Selecting Execution Route. ?. true. statement. false. if(?){ statement; }.

curts
Download Presentation

Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures

  2. Sequential Execution x = keyBoard.nextInt(); statement statement y = 2*x - 3; statement System.out.println(x); … … System.out.println(y); statement

  3. Selecting Execution Route ? true statement false if(?){ statement;}

  4. Selecting Execution Route ? true statement _1 false statement_2 if(?){ statement_1;}else{ statement_2;}

  5. for Loops Initializecounter • Referred to as the “counting” loop • Parts of a for-loop • Initialize and declare counter section • Test counter section • Update counter section • Loop body section Test counter false true statement Updatecounter

  6. for Loops for(initialize; test; update){ body} for(initialize_variable; test_variable; change_variable) { //body of loop goes here }

  7. while loop

  8. //display all divisors of a given positive integer //N = positive integer //d is a divisor of N input N d = 1 while d less than or equal to N{ if(d divides evenly into N){ print d } increment d by one }

  9. input N d = 1 while d less than or equal to N{ if(d divides evenly into N){ print d } increment d by one } Bench Test: N d d <= N? d divdes N? output ============================================================== 10 1 yes yes 1 ------------------------------------------------------------- 2 yes yes 2 ------------------------------------------------------------- 3 yes no - ------------------------------------------------------------- 4 yes no - ------------------------------------------------------------- 5 yes yes 5 ------------------------------------------------------------- 6 yes no - ------------------------------------------------------------- 7 yes no - ------------------------------------------------------------- 8 yes no - ------------------------------------------------------------- 9 yes no - ------------------------------------------------------------- 10 yes yes 10 ------------------------------------------------------------- 11 no

  10. Remember toupdatej Contrasting Loops j = 1;while(j<=5){ System.out.println(“Hello”); j = j + 1;} for(int j=1; j<=5; j=j+1){ System.out.println(“Hello”);}

  11. Arithmetic Sequences • a = first term • d = common difference • n = number of terms • Sequence:a, a+d, a+2d, …, a+(n-1)d

  12. Code outCnt = 0; for(int k=0; k<n; k++){ if(outCnt != 5) System.out.printf("%5d", a + k*d); else{ System.out.printf("%n%5d", a + k*d); outCnt = 0; } outCnt++; }

  13. Alternate Code outCnt = 0; for(int k=a; k<a+n*d; k=k+d){ if(outCnt != 5) System.out.printf("%5d", k); else{ System.out.printf("%n%5d", k); outCnt = 0; } outCnt++; }

  14. do-while Loops • Bottom-test loop • Always executesthe body at least once • Loops until the Test? returns false statement Test? true false

  15. do-while Loops do{ statement;}while(?); Bottom-test loop while(?){ statement;} Top-test loop

  16. Example

  17. Premature Loop Exit • break • Used to exit an enclosing loop statement • Used to exit a switch statement x = keyBoard.nextInt();while(x<=100){ System.out.println(x); if(x % 4 == 0) break; x = x + 7;}

  18. switch Statement • Used in place of a series of if-else if-else ifs • When the test produces an integer value if(month==1){ …}else if(month==2){ …}… switch(month){ case 1: … break; case 2: … case 3: … break; … default:}

More Related