# לולאות 02 יולי 15 02 יולי 15 02 יולי 1502 יולי 15 02 יולי 15 02 יולי 1502 יולי 15 02 יולי 15 02 יולי 15 1 Department of Computer Science-BGU

• View
219

0

Embed Size (px)

Transcript

• Slide 1
• 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 1 Department of Computer Science-BGU
• Slide 2
• ? ? 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 2 Department of Computer Science-BGU
• Slide 3
• (loop) Used to repeat the same instruction(s) over and over again. C provides some flexible ways of deciding how many times to loop, or when to exit a loop. for, while, do-while loops. 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 3 Department of Computer Science-BGU
• Slide 4
• While while (condition) { statement(s); } The statements are executed as long as condition is true When the condition is no longer true, the loop is exited. 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 4 Department of Computer Science-BGU
• Slide 5
• - factorial #include void main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; while (i 9 ||" title="Solution #include void main() { int a, b, flag = 1; while ( flag){ flag = 0; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); if(a > 9 ||">
• Solution #include void main() { int a, b, flag = 1; while ( flag){ flag = 0; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); if(a > 9 || a < 0) flag = 1; else if (b > 9 || b < 0 ) flag = 1; } // executing statements using a and b } 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 9 Department of Computer Science-BGU
• Slide 10 9 || a 9 || b < 0 ) flag = 1; } // executing statements using a and b } 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 10 Department of Computer Science-BGU">
• Compact Solution #include void main() { int a, b, flag = 1; while ( flag){ flag = 0; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); if(a > 9 || a 9 || b < 0 ) flag = 1; } // executing statements using a and b } 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 10 Department of Computer Science-BGU
• Slide 11
• do - while do { statement(s) } while (expression); Similar to while loops Except the condition is evaluated after the loop body The loop body is always executed at least once, even if the expression is never true (equals zero) 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 11 Department of Computer Science-BGU
• Slide 12
• #include void main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i 1 ; i--) { for(j = 2 ; j < i; j++) if (i % j == 0) break; // break the inner for loop only !! if (j == i) // i is prime. We found our guy break; } printf("The largest prime not larger than %d is %d.\n", last, i); } 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 30 Department of Computer Science-BGU"> 1 ; i--) { for(j = 2 ; j < i;" title=" 2 break #include void main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for(i=last ; i>1 ; i--) { for(j = 2 ; j < i;">
• 2 break #include void main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for(i=last ; i>1 ; i--) { for(j = 2 ; j < i; j++) if (i % j == 0) break; // break the inner for loop only !! if (j == i) // i is prime. We found our guy break; } printf("The largest prime not larger than %d is %d.\n", last, i); } 02 15 02 15 02 1502 15 02 15 02 1502 15 02 15 02 15 30 Department of Computer Science-BGU