Document

‫מבוא למדעי המחשב‬
‫לולאות‬
‫‪1‬‬
‫לולאות‬
‫הרעיון ‪ -‬ביצוע של הוראה אחת‪ ,‬או סדרה של הוראות‪ ,‬מספר‬
‫כלשהוא של פעמים‪:‬‬
‫‪‬‬
‫מספר הפעמים ידוע מראש (למשל – סיכום חמישה מספרים‬
‫הנתונים בקלט)‪.‬‬
‫או‬
‫‪‬‬
‫מספר לא ידוע מראש של פעמים‪ .‬התנאי לסיום הביצוע נקבע‬
‫באופן דינאמי בזמן ריצת התכנית‪.‬‬
‫‪2‬‬
‫לולאות ‪ -‬מוטיבציה‬
‫המטרה‪ :‬ניסוח אלגוריתם לחישוב הערך של !‪.n‬‬
‫‪n!=1*2*3*…*(n-1)*n‬‬
‫אלגוריתם לחישוב !‪:n‬‬
‫אתחל את ‪ result‬ל‪( 1 -‬האיבר הניטראלי לכפל)‬
‫אתחל את ‪ i‬ל‪( 1 -‬מונה)‬
‫כל עוד ‪ i‬קטן או שווה ל‪n -‬‬
‫תנאי הלולאה נבדק‬
‫כפול את ‪ result‬ב‪i -‬‬
‫לפני ביצוע ההוראה‬
‫הגדל את ‪ i‬ב‪1 -‬‬
‫שאחריו‬
‫‪3‬‬
‫לולאות – תיאור גרפי‬
‫לא‬
‫‪‬‬
‫‪‬‬
‫כן‬
‫לולאה יכולה להתבצע אפס פעמים (אם התנאי אינו מתקיים כבר‬
‫בחישוב הראשון)‪.‬‬
‫אין שום מניעה לכתוב תכנית שבה תבוצע לולאה אינסופית!‬
‫‪4‬‬
‫לולאות‬
‫‪while‬‬
‫תחביר‪:‬‬
‫)‪while (expression‬‬
‫‪statement‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪ – while‬מילה שמורה‬
‫הסוגריים הם חלק מן התחביר‬
‫‪ – expression‬ביטוי‬
‫‪ – statement‬הוראה אחת (שיכולה להיות גם הוראה מורכבת)‪.‬‬
‫‪5‬‬
‫לולאות‬
‫‪while‬‬
‫משמעות‪:‬‬
‫)‪while (expression‬‬
‫‪statement‬‬
‫‪.1‬‬
‫‪.2‬‬
‫‪ expression‬משוערך‪.‬‬
‫אמת ← ההוראה ‪ statement‬מתבצעת והביצוע חוזר אל‬
‫‪.expression‬‬
‫שקר ← הבקרה מועברת אל ההוראה הבאה לאחר ה‪.while -‬‬
‫‪6‬‬
‫ דוגמא‬- while ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n,i;
double result=1;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
i=1;
while (i<=n) {
result *= i;
i++;
}
printf("%d!=%.0f\n", n, result);
}
return 0;
}
‫ אבל‬,n ‫ הוא מספר שלם לכל‬n!
‫ערכו גדל במהירות (מקום‬
)‫אחסון מספיק‬
:‫בקרת קלט‬
?‫• האם המספר שלם‬
?‫• האם המספר חיובי‬
‫ דוגמא‬- while ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n,i;
double result=1;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
i=1;
while (i<=n) {
result *= i;
i++;
}
printf("%d!=%.0f\n", n, result);
}
return 0;
}
while ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n,i;
double result=1;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
i=1;
while (i<=n) {
result *= i;
i++;
}
printf("%d!=%.0f\n", n, result);
}
return 0;
}
‫אתחול משתנה המשמש‬
‫כמונה‬
.1
‫תנאי הבודק את ערכו של‬
‫המשתנה‬
.2
‫גוף הלולאה‬
.3
‫עדכון המשתנה‬
.4
‫לולאות‬
‫‪for‬‬
‫תחביר‪:‬‬
‫)‪for (expr1 ; expr2 ; expr3‬‬
‫‪statement‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪ – for‬מילה שמורה‬
‫הסוגריים הם חלק מן התחביר‬
‫כל ‪ – expr‬ביטוי‬
‫‪ – statement‬הוראה אחת (שיכולה להיות גם הוראה מורכבת)‪.‬‬
‫‪10‬‬
‫לולאות‬
‫(*) קידום – ביטוי השמה‬
‫‪for‬‬
‫(*) בדיקת תנאי – ביטוי‬
‫יחס (מחזיר ערך לוגי)‬
‫(*) אתחול – ביטוי השמה‬
‫)‪for (expr1 ; expr2 ; expr3‬‬
‫‪statement‬‬
‫שקול ל‪:‬‬
‫‪expr1‬‬
‫)‪while (expr2‬‬
‫{‬
‫‪statement‬‬
‫‪expr3‬‬
‫}‬
‫(*) בדר"כ‬
‫‪11‬‬
‫לולאות‬
‫(*) קידום – ביטוי השמה‬
‫‪for‬‬
‫(*) בדיקת תנאי – ביטוי‬
‫יחס (מחזיר ערך לוגי)‬
‫(*) אתחול – ביטוי השמה‬
‫)‪for (expr1 ; expr2 ; expr3‬‬
‫‪statement‬‬
‫‪‬‬
‫‪‬‬
‫מבחינה תחבירית‪ ,‬כל אחד משלושת הביטויים יכול להיות מושמט‪ ,‬אך סימני ה‪; -‬‬
‫הכרחיים‪.‬‬
‫מבחינה סמנטית‪ ,‬הלולאה תתבצע כל עוד ‪ expr2‬משוערך כאמת (כלומר‪.)0≠ ,‬‬
‫אם ‪ expr2‬חסר‪ ,‬הוא מתפרש כאמת תמיד‪.‬‬
‫‪12‬‬
‫ דוגמא‬- for ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n,i;
double result=1;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
for (i=1 ; i<=n ; i++) {
result *= i;
}
printf(“%d!=%.0f\n", n, result);
}
return 0;
}
‫ דוגמא‬- for ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n,i;
double result;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
for (i=1 , result=1 ; i<=n ; i++) {
result *= i;
}
printf(“%d!=%.0f\n", n, result);
}
return 0;
}
‫לולאות ‪ - for‬דוגמא‬
‫{ )‪for (i=1 , result=1 ; i<=n ; i++‬‬
‫;‪result *= i‬‬
‫}‬
‫ידוע‪:‬‬
‫‪n! = 1*2*…*(n-1)*n = n*(n-1)*…*2*1‬‬
‫ההגדרה ‪ n! = n*(n-1)*…*2*1‬מאפשרת לארגן את הלולאה בסדר הפוך‪ ,‬מ‪n -‬‬
‫עד ‪.1‬‬
‫יתרון‪ :‬משתנה הקלט (‪ )n‬יכול לשמש גם כמונה ← אין יותר צורך במשתנה ‪!!! i‬‬
‫{ )‪for (result=1 ; n>0 ; n--‬‬
‫;‪result *= n‬‬
‫}‬
‫‪15‬‬
‫ דוגמא‬- for ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n;
double result;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
printf("%d!=", n);
for (result=1 ; n>0 ; n--) {
result *= n;
}
printf(%.0f\n", result);
}
return 0;
}
‫ דוגמא‬- for ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n;
double result=1;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
printf(“%d!=“, n);
for ( ; n>0 ; n--) {
result *= n;
}
printf("%.0f\n", result);
}
return 0;
}
‫ דוגמא‬- for ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n;
double result=1;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
printf(“%d!=“, n);
for ( ; n>0 ; ) {
result *= n--;
}
printf("%.0f\n", result);
}
return 0;
}
while (n>0)
result*=n--;
‫ דוגמא‬- for ‫לולאות‬
/* This program computes n!, the factorial of n */
#include <stdio.h>
int main()
{
int n;
double result=1;
printf("Enter a natural number: ");
if (scanf("%d", &n) != 1) {
printf("Input error.\n");
return 1;
}
else if (n<0) {
printf("Factorial is undefined for negative integers.\n");
return -1;
}
else {
printf(“%d!=“, n);
while (n>0)
result*=n--;
printf("%.0f\n", n, result);
}
return 0;
}