120 likes | 238 Views
Control Structures. Structured Programming Selection if if else switch(break) Repetition (break, continue) while do … while for. If. if (condition) { statements } if (condition) { statements } else { statements }. Examples - if. if (score>=60) num_pass++; if (score>=60)
E N D
Control Structures • Structured Programming • Selection • if • if else • switch(break) • Repetition (break, continue) • while • do … while • for
If if (condition) { statements } if (condition) { statements } else { statements }
Examples - if if (score>=60) num_pass++; if (score>=60) num_pass++; else num_fail++;
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’; }
switch switch (expression) { case C1: statements break; case C2: statements break; default: statements }
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; }
while while (condition) { statements } int i=1; int sum=0; while (i<=10) { sum += i; // sum = sum+i; i++; }
do …while do { statements } while (condition) int i=1; int sum=0; do { sum += i; // sum = sum+i; i++; } while (i<=10)
for for (initialization; condition; increment) { statements } int sum=0; for (int i=1; i<=10; i++) { sum += i; // sum = sum+i; }
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; }
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; • } • } • }