1 / 11

Control Structures

This guide provides an overview of control structures in structured programming, focusing on selection and repetition mechanisms. It explains the use of `if`, `if-else`, and `switch` statements for decision-making, as well as loops like `while`, `do-while`, and `for` for repetitive execution. Practical examples illustrate how conditions and loops operate, featuring concepts like `break` and `continue` to manage the flow. Understanding these structures is essential for effective programming in languages like Java and C.

cole-rose
Télécharger la présentation

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 • Structured Programming • Selection • if • if else • switch(break) • Repetition (break, continue) • while • do … while • for

  2. If if (condition) { statements } if (condition) { statements } else { statements }

  3. Examples - if if (score>=60) num_pass++; if (score>=60) num_pass++; else num_fail++;

  4. Examples - Nested if if (score>=60) { if (score<70) grade=‘D’; else if (score<80) grade=‘C’; else if (score<90) grade=‘B’; else grade=‘A’; }

  5. switch switch (expression) { case C1: statements break; case C2: statements break; default: statements }

  6. Examples -switch switch (score/10) { case 10: case 9: grade=‘A’; break; case 8: grade=‘B’; break; case 7: grade=‘C’; break; case 6: grade=‘D’; break; default: grade=‘F’; break; }

  7. while while (condition) { statements } int i=1; int sum=0; while (i<=10) { sum += i; // sum = sum+i; i++; }

  8. do …while do { statements } while (condition) int i=1; int sum=0; do { sum += i; // sum = sum+i; i++; } while (i<=10)

  9. for for (initialization; condition; increment) { statements } int sum=0; for (int i=1; i<=10; i++) { sum += i; // sum = sum+i; }

  10. break & continue int sum=0; for (int i=1; i<=10; i++) { if (i%5==0) continue; sum += i; // sum = sum+i; } int sum=0; for (int i=1; i<=10; i++) { if (i%5==0) break; sum += i; // sum = sum+i; }

  11. What will be printed? • for (int i=0; i<3; i++) { • for (int j=0; j<9; j++) { • switch (i) { • case 0: • break; • case 1: • continue; • } • System.out.println(10*i+j); • if (j==4) { • break; • } • else { • continue; • } • } • }

More Related