לולאות Department of Computer Science-BGU 19 יולי 19.

Slides:



Advertisements
מצגות קשורות
הרצאה 02 סוגי משתנים קרן כליף.
Advertisements

קורס ניהול ידע מפגש 3 - פורטלים 2know.kmrom.com.
Computer Programming Fall 2012 תרגול 6 פונקציות
תגובות חמצון -חיזור ודוגמאות מחיי היום יום
מבוא לתכנות ב-JAVA מעבדה 1
Trends in Periodic Table Properties
על הסתיו ועוד, בגן ה"תמרים" בעכו שם הגננות: מירב קדוש אלה נאמן
The Partnership tree in ECP- Austin עץ השותפות באוסטין
טקס פתיחת "גינת השותפות" בעכו
נערך ע"י אריק הוד, הגימנסיה העברית הרצליה
תרגול 5: לולאות ומערכים.
Isotopes and Atomic Mass
OOO Execution of Memory Operations
Marina Kogan Sadetsky –
אלקטרושליליות ופולריות הקשר
2013 האמור במצגת זו כולל גם מידע צופה פני עתיד, כהגדרתו בחוק ניירות ערך, של תחזיות, מטרות, הערכות, אומדנים ומידע אחר המתייחסים לאירועים ו/או לעניינים.
משואה כימית של שריפת פחם (תגובה עם חמצן)
הרצאה 3: משפטים, תנאים ולולאות
מודל דחיית זוגות אלקטרונים של קליפת הערכיות
תרגול Introduction to C - Fall Amir Menczel.
פעולות אריתמטיות קרן כליף.
Computer Programming תרגול 1 Summer 2016
The local ceremony for kindergarten children in Akko was on Akko’s hill and was dedicated to the environment. The children climbed the hill while singing.
Present Perfect Simple
Passover in “Tomer” kindergarten
תירגול 11: מיונים וחיפוש בינארי
תרכובות עם יונים פוליאטומים בחיי היום יום
יסודות מבני נתונים תרגול 13: עץ פורש מינימלי - Minimum Spanning Tree (MST)
חוק שימור החומר The Law of Conservation of Mass indicates that in an
תירגול 3: לולאות, קלט, וטיפוסים
תכנות בשפת C תרגול 8 רקורסיה
The Dolphin Class International Forest of Support
חג פורים שמח ומבדח מגן פרפר....תפן שם הגננת: מאיה פלס
מבני נתונים תרגול 3 ליאור שפירא.
Akiba Academy of Dallas Presents
מבוא למדעי המחשב הרצאה 9: רקורסיה
תרגול 6: פונקציות, סוגי משתנים, top-down design
תירגול 2: מבוא לתיכנות ב-C
האם אתם יודעים כיצד כותבים מגילה?
Direction of induced current
שם הגננת: עדנה סרי The teacher: Edna Seri
Computer Architecture and System Programming Laboratory
בתרכובות יוניות יש קשרים יונים
תיעוד מפעילויות ראש השנה בגן "פרפר נחמד" –עכו-תש"ע
Branch Prediction בעריכת אורן קצנגולד Updated by Franck Sala.
Branch Prediction Oren Katzengold, Franck Sala, Elad Shtiegmann.
Elements and Symbols יסודות וסמלים מהם יסודות? gold carbon aluminum
Marina Kogan Sadetsky –
מת"מ מעבדה 7-8 רשימות.
Emech at 1 + W = Emech at 2 mgh1 = mgh2
© המרכז להוראת המדעים האוניברסיטה העברית בירושלים
What is Chemistry? מהי כימיה? Chemistry is the study of matter and its
מבוא לתכנות ב- JAVA מעבדה 4
מבוא לתכנות בWINDOWS ד"ר אבי רוזנפלד.
קומבינטוריקה למדעי-המחשב EULER GRAPHS גרפים אויילרים
מבוא למדעי המחשב סמסטר ב' – 2008 מרצה: יעל סיגל מתרגל: ענבל בודובסקי.
Introduction to Programming in C
הכנת שמן זית וארכיאולוגיה בגן "ארגמן" עכו Preparing olive oil and Archeology at “Argaman” kindergarten in Akko שם הגננת: נטלי גוריבודסקי שם הסייעת :
גן "יסמין "עכו שם הגננת: מרגלית פוגל שם הסייעת: זיוה אוזן
“Augustus and his smile” “"חיוכו של נסיך
Tutorial #10 MIPS commands
שם הגננת:אתי ברכפלד קיבוץ סער
עקרונות תכנות מונחה עצמים תרגול 9:C++ - תרגילים
יינון המים In the ionization of water,
Engineering Programming A
בגן" הדס" בעכו משלבת פרלה שטרית הגננת ,בין חנוכה לבין השמירה על איכות הסביבה. פרלה בקשה מהילדים להכין בביתם חנוכייה מחומרים ממוחזרים או מחומרים שאינם.
תמליל מצגת:

לולאות Department of Computer Science-BGU 19 יולי 19

מבנה הלולאה ? ? Department of Computer Science-BGU 19 יולי 19

לולאה (loop) for, while, do-while loops. 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. Department of Computer Science-BGU 19 יולי 19

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. Department of Computer Science-BGU 19 יולי 19

דוגמא- factorial #include <stdio.h> void main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; while (i<=n) { fact = fact*i; i++; } printf("the factorial is %d\n", fact); This is a counter Every iteration i is incremented by 1. (Equivalent to i=i+1) Department of Computer Science-BGU 19 יולי 19

Exercise Input – Output – Note – Two integers – A and B How many times A contains B This is the result of the integer division A/B Note – Do not use the division operator! Department of Computer Science-BGU 19 יולי 19

Solution #include <stdio.h> void main() { int a, b, res; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); res = 0; while ( (res+1) * b <= a) res = res + 1; printf("%d / %d = %d", a, b, res); } Department of Computer Science-BGU 19 יולי 19

Additional Exercise Input – Output – Two integers – A and B Only if A and B are Digits Otherwise return to input. Department of Computer Science-BGU 19 יולי 19

Solution #include <stdio.h> 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 ) } // executing statements using a and b Department of Computer Science-BGU 19 יולי 19

Compact Solution #include <stdio.h> 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 || b > 9 || b < 0 ) flag = 1; } // executing statements using a and b Department of Computer Science-BGU 19 יולי 19

לולאה 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) Department of Computer Science-BGU 19 יולי 19

לקלוט נתון לפי הדרישה #include <stdio.h> void main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i<=0) printf("That's not a positive number! Try again.\n"); } while (i<=0); /* The program continues.... */ } Department of Computer Science-BGU 19 יולי 19

לולאה for - מבנה /* Program before the for loop */ for( ; ; ) { loop body; } /* Continue the program after for loop */ initiali ze condition increment False True Department of Computer Science-BGU 19 יולי 19

דוגמא חלק של תכנית המחשבת את הממוצע של 10 מספרים sum=0; for(i=0; i< 10 ; i++){ printf("Enter number \n"); scanf("%d", &num); sum += num; } average = sum / 10; printf("The average is : %d\n", average); Department of Computer Science-BGU 19 יולי 19

לולאות מקוננות : תרגיל פשוט לולאות מקוננות : תרגיל פשוט כתוב תכנית שמדפיסה את טבלת הכפל עד 4 * 5 פתרון:#include <stdio.h> void main(){ int i,j; printf("Printing the multiplication table: \n\r"); for (i = 1 ; i <= 4 ; i++) {        printf("\n"); // putchar(‘\n’)        for (j = 1 ; j <= 5 ; j++)           printf ("%2d ", i*j);        } } Department of Computer Science-BGU 19 יולי 19

לולאות מקוננות : תרגיל נוסף כתוב תכנית שקולטת 5 סדרות של 10 מספרים כל אחת. התכנית מחשבת ומדפיסה את הממוצע של כל אחת הסדרות . התכנית מחשבת ומדפיסה את הממוצע הכללי. Department of Computer Science-BGU 19 יולי 19

לולאות מקוננות sum=0; for(i=0; i< 5 ; i++){ aux_sum = 0; for(j=0 ; j < 10; j++){ printf("Enter number \n"); scanf("%d", &num); sum += num; aux_sum += num; } printf("The average of serie %d is :%d\n", i, aux_sum /10); printf("The total average is : %d\n", sum / 50 ); Department of Computer Science-BGU 19 יולי 19

for vs . while for is equivalent to while… Any for loop can be converted to while loop and vice versa. If we want to perform something for a predefined number of times, better use for. If we just wait for something to happen (not after a certain number or iterations), better use while. Department of Computer Science-BGU 19 יולי 19

break When break is encountered, the loop is exited regardless of whether the condition is still true. The program then continues to run from the first line after the while loop. If called within a nested loop, break breaks out of the inner loop only. Department of Computer Science-BGU 19 יולי 19

שימושים ב-break void main () { . loop1(expression1) { loop2(expression2) { break; } // end loop2 // continue with loop1 } // end loop1 } void main () { . loop(expression) { break; } // continue with the program Department of Computer Science-BGU 19 יולי 19

לקלוט נתון לפי הדרישה עם break #include <stdio.h> void main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i >= 0) break; // after break no needed else printf("That's not a positive number! Try again.\n"); } while (1); /* The program continues.... */ } Department of Computer Science-BGU 19 יולי 19

continue When continue is encountered, the rest of the loop is ignored. The program then continues to run from the beginning of the loop. Rarely used. Can usually be replaced by an appropriate if-else statement. Department of Computer Science-BGU 19 יולי 19

שימוש ב- continue void main () { . loop(expression) { continue; } // continue with the program Department of Computer Science-BGU 19 יולי 19

תרגיל Write a program that accepts a number from the user, and checks whether it is prime. Department of Computer Science-BGU 19 יולי 19

פתרון #include <stdio.h> void main() { int i, num; printf("enter a number\n"); scanf("%d", &num); for(i = 2 ; i < num; i++) if (num % i == 0) /* Then we know it's not a prime */ break; if (i < num) printf("%d is not a prime number!\n", num); else printf("%d is indeed a prime number!\n", num); } Department of Computer Science-BGU 19 יולי 19

תרגיל נוסף Extend the former program so it accepts a number from the user, and prints out all of the prime numbers up to that number (Hint – gotta use nested loops here...) Department of Computer Science-BGU 19 יולי 19

פתרון #include <stdio.h> void main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for(i = 2; i <= last; i++) { for(j = 2 ; j < i; j++) if (i % j == 0) break; if (j == i) printf("the number %d is prime\n", i); } // end for } Department of Computer Science-BGU 19 יולי 19

תרגיל נוסף Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** **** *** ** * Department of Computer Science-BGU 19 יולי 19

הנה הפתרון #include<stdio.h> void main() { int i, j, size; printf(“Please enter a size:\n”); scanf(“%d”,&size); for (i = 1; i <= size; i++) { for(j = i; j <= size; j++) printf("*"); printf("\n"); } Department of Computer Science-BGU 19 יולי 19

שינוי בתרגיל Change the former prime-listing program, so that is displays only the largest prime number which is smaller than or equal to the user’s input. Department of Computer Science-BGU 19 יולי 19

פתרון 1 #include <stdio.h> void main() { int i, j, last; int found = 0; /* This indicates whether we found the largest prime */ printf("enter a number\n"); scanf("%d", &last); i = last; while (!found) { /* Loop until we find our guy */ for(j = 2 ; j < i; j++) if (i % j == 0) break; if (j == i) /* If this is true then i is prime */ found = 1; else i--; } printf("The largest prime not larger than %d is %d.\n", last, i); Department of Computer Science-BGU 19 יולי 19

פתרון 2 – עם break #include <stdio.h> 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); Department of Computer Science-BGU 19 יולי 19