while for for expr 1 expr 2 expr

  • Slides: 14
Download presentation

while ↔ לולאות for לולאות for (expr 1 ; expr 2 ; expr 3)

while ↔ לולאות for לולאות for (expr 1 ; expr 2 ; expr 3) statement 3 = expr 1 while (expr 2) { statement expr 3 }

 תכנית דוגמא ) (1 המטרה : להדפיס למסך את לוח הכפל 10 9

תכנית דוגמא ) (1 המטרה : להדפיס למסך את לוח הכפל 10 9 8 7 6 5 4 3 2 1 20 18 16 14 12 10 8 6 4 2 30 27 24 21 18 15 12 9 6 3 40 36 32 28 24 20 16 12 8 4 50 45 40 35 30 25 20 15 10 5 60 54 48 42 36 30 24 18 12 6 70 63 56 49 42 35 28 21 14 7 80 72 64 56 48 40 32 24 16 8 90 81 72 63 54 45 36 27 18 9 100 90 80 70 60 50 40 30 20 10 4

(1) תכנית דוגמא /* This program prints the multiplication table */ #define LIMIT 10

(1) תכנית דוגמא /* This program prints the multiplication table */ #define LIMIT 10 int main() { int i, j; for )i=1; i <= LIMIT; i} (++ for )j=1; j <= LIMIT; j} (++ printf 4 d%")", i*j; ( } printf")n; (" } } 5 (nesting) קינון לולאות

(2) תכנית דוגמא /* This program counts the number of characters in its input

(2) תכנית דוגמא /* This program counts the number of characters in its input */ #include <stdio. h> int main() { long count=0; while (getchar( ) != EOF) { ++count; } printf("%ldn", count); return 0; } 7

(3) תכנית דוגמא : המטרה לכתוב תכנית הסופרת את מספר השורות בקלט /* This

(3) תכנית דוגמא : המטרה לכתוב תכנית הסופרת את מספר השורות בקלט /* This program counts the number of characters in its input */ #include <stdio. h> int main() { long count=0; while (getchar( ) != EOF) { ++count; } printf("%ldn", count); return 0; } 8

(3) תכנית דוגמא /* This program counts the number of lines in its input

(3) תכנית דוגמא /* This program counts the number of lines in its input */ #include <stdio. h> int main() { long count=0; int c; while ( (c = getchar( ) ) != EOF) { if (c == ‘n’){ ++count; } } printf("%ldn", count); return 0; } 9 הסוגריים הללו מבטיחים את ! סדר הקדימויות הרצוי

break הוראת ← לא מומלץ כלל )תמיד ניתן להחליף while , for , do-while

break הוראת ← לא מומלץ כלל )תמיד ניתן להחליף while , for , do-while בתוך לולאות !!! ( בניסוח אחר של תנאי הבקרה המקורי של הלולאה while(1){ scanf (“%lf”, &x); if (x < 0) break; printf(“The square root of %f is %f. n”, x, sqrt(x)); } done=0; while(!done){ scanf (“%lf”, &x); if (x < 0) done=1; else printf(“The square root of %f is %f. n”, x, sqrt(x)); } 12 q