מבוא למדעי המחשב
לולאות
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;
}
© Copyright 2026 Paperzz