Nested loops and patterns · לולאות מקוננות ותבניות
A loop inside a loop
- You can put one loop inside another. The inner loop runs fully for each step of the outer loop.
- This is how you work with rows and columns, or compare every pair of items.
- If the outer loop runs
Rtimes and the inner runsCtimes, the inner body runsR × Ctimes.
לולאה בתוך לולאה
- ניתן להכניס לולאה אחת בתוך לולאה אחרת. הלולאה הפנימית רצה במלואה עבור כל שלב של הלולאה החיצונית.
- כך עובדים עם שורות ועמודות, או比较 כל זוג פריטים.
- אם הלולאה החיצונית רצה
Rפעמים והפנימית רצהCפעמים, גוף הלולאה הפנימית ירצהR × Cפעמים.
Rows and columns
- Think of the outer loop as the row and the inner loop as the column.
- For a block of
*, the outer loop picks a row; the inner loop prints the stars in that row. - After the inner loop finishes a row, print a newline
\nto start the next row.
שורות ועמודות
- חשבו על הלולאה החיצונית כ-שורה והלולאה הפנימית כ-עמודה.
- עבור בלוק של
*, הלולאה החיצונית בוחרת שורה; הלולאה הפנימית מדפסת את הכוכבים בשורה זו. - לאחר שסריג פנימי גומר שורה, הדפס קו חדש
\nכדי להתחיל את השורה הבאה.
Building a line, then a block
printf("*")prints one star without moving to a new line.- Print all the stars for one row first, then one
printf("\n")to end the row. - Repeat for every row, and the rows stack into a block.
בניית שורה, ואז בלוק
printf("*")מדפס כוכב אחד ללא מעבר לשורה חדשה.- הדפס את כל הכוכבים בשורה אחת תחילה, ולאחר מכן קו חדש
printf("\n")כדי לסיים את השורה. - חזור על כך לכל שורה, והשורות מצטברות לבלוק.
#include <stdio.h>
int main(void) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
printf("*");
}
printf("\n"); // end the row
}
return 0;
}
Counting with nested loops
- Nested loops also count pairs. To count pairs
(i, j)withi < j, start the inner loop ati + 1. - A counter inside the inner loop adds up the matches.
- Watch the start and end values — off-by-one mistakes are common here.
ספירה עם לולאות מקוננות
- סריגים מקוננים גם סופרים זוגות. כדי לספור זוגות
(i, j)עםi < j, התחל את הסריג הפנימי ב-i + 1. - סופר בתוך הסריג הפנימי מוסיף את ההתאמות.
- שימו לב לערכי ההתחלה והסיום – טעאות של "פסול באחד" נפוצות כאן.
Common mistakes
- The inner loop runs fully for each single turn of the outer loop.
- Check the bounds so a pattern has no extra or missing row.
טעויות נפוצות
- הסריג הפנימי מתנהל במלואו בכל סיבוב יחיד של הסריג החיצוני.
- בדק את הגבולות כדי שהדפוס לא יהיה עם שורה עודפת או חסרה.
Now you try
- Use an inner
printfwithout\nto build a row, then aprintf("\n")to end it. - For the function task, do not write a
main— the checker provides one.
כעת תנסו בעצמכם
- השתמש בסריג פנימי
printfללא\nלבניית שורה, ולאחר מכן קו חדשprintf("\n")לסיומה. - עבור משימת הפונקציה, אל תכתוב קו חדש
main— המבחן מספק אותו.
In main, use nested loops to print a 3×3 block of * — three stars on each of three lines. · בתוך main, השתמש בלולאות מקוננות כדי להדפיס בלוק 3×3 של * — שלושה כוכבים בכל אחת משלושת השורות.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
In main, print a left-aligned triangle of * that is 4 rows tall: row 1 has one *, row 2 has two, up to row 4 with four. · ב-main, הדפס משולש ישר זווית משמאל המורכב מ-* בגובה של 4 שורות: שורה 1 מכילה ⟨⟩* אחד, שורה 2 מכילה שניים, ועד שורה 4 שמכילה ארבעה.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete int count_pairs(int n) so it returns how many pairs (i, j) satisfy 1 <= i < j <= n. Use nested loops (start the inner loop at i + 1). Do not write a main. · השלם את int count_pairs(int n) כך שהיא תחזיר כמה זוגות (i, j) מקיימים 1 <= i < j <= n. השתמש בלולאות מקווננות (התחל את הלולאה הפנימה בi + 1). אל תכתוב פקודת main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.