דילוג לתוכן
מבחן 10 שאלות

Loops in C#: For and While

שאלות 10

מפתח התשובות מוצג למורים מחוברים.

  1. שאלה פתוחהc#for looploop iterationsloop step

    Counting iterations of a for loop

    How many times will the loop body execute in the following C# code? for (int i = 1; i <= 10; i += 3) { Console.WriteLine(i); }


    List each value of i before the loop body runs, checking the condition i <= 10 each time, and count the values.
  2. שאלה פתוחהc#while loopaccumulatorloop tracing

    Final value of a while loop accumulator

    Assume the following C# code runs. int n = 1; int total = 0; while (n < 50) { total += n; n *= 2; } What is the final value of total?


    Trace n and total step by step, noting that n doubles each time and the loop stops as soon as n is no longer less than 50.
  3. שאלה פתוחהc#loop selectionloop structurefor vs while

    Choosing between for and while

    Explain when a programmer should choose a for loop instead of a while loop in C#, and when a while loop is the better choice. Refer to how each loop handles its initialization, condition and iteration step.


    Think about where the counter is set up and updated in each loop, and whether you know the number of repetitions before the loop starts.
  4. שאלה פתוחהc#infinite loopwhile looploop debugging

    Spotting an infinite loop

    The following C# code is intended to print the numbers 10 down to 1, but it does not behave as intended. int x = 10; while (x > 0) { Console.WriteLine(x); x++; } Explain why this loop never ends and how it should be corrected.


    Consider the direction in which x changes on each iteration compared with the direction needed to make the condition x > 0 eventually false.
  5. שאלה פתוחהc#nested loopsfor looploop counting

    Counting with nested for loops

    Assume count is an int that starts at 0. What is the final value of count after the following C# code runs? for (int i = 0; i < 3; i++) { for (int j = 0; j < i; j++) { count++; } }


    The inner loop runs a different number of times for each value of i. Work out how many times it runs when i is 0, 1 and 2.
  6. שאלה פתוחהשפות תכנותc#for loopeven numbersConsole.WriteLine

    Writing a for loop that prints even numbers

    Write a C# for loop that prints the even numbers from 2 to 20 inclusive, one number per line, using Console.WriteLine. Describe the initialization, condition and iteration step you used.


    Start at the first even number and choose an increment that skips the odd numbers. Make sure the condition includes 20.
  7. השלמהשפות תכנותc#continue statementloop controlflow control

    Continue keyword

    The __________ keyword skips the rest of the current loop iteration.

    breakcontinuereturngoto

    Think of a keyword that moves straight to the next pass of the loop instead of leaving it.
  8. השלמהשפות תכנותc#do-while looploop conditionwhile loop

    Condition timing in while and do-while

    A while loop evaluates its condition ________ each pass, and a do-while loop evaluates it ________ each pass.

    sometimesafterseldombeforerarely

    Consider when the while loop checks its condition compared with running the body, and how a do-while differs.
  9. השלמהשפות תכנותc#for looploop structureiterator

    Parts of a for loop

    In a for loop, the ________________ part runs only once, before the first iteration. The ________________ is checked before every iteration, and the loop stops when it becomes false. The ________________ part, such as i++, runs at the end of each iteration.

    terminatorconditioninitializationmodifiercounterdelimiteriterator

    The for loop header has three sections separated by semicolons; the first section sets up the loop variable.
  10. השלמהשפות תכנותc#while loopbreak statementinfinite loop

    Infinite while loops

    A while (true) loop is an __________ loop by default. The __________ statement is the usual way to leave it early.

    infinitepausenestedbreakcontinuefinite

    A condition that is always true never becomes false, so the loop never ends on its own.

פרטים

Questions on writing for and while loops in C#, tracing their output, and avoiding common errors.
מקצועות
c#
קהל יעד
תיכון · כיתות י׳–י״ב
רמת קושי
6 מתוך 10
שפה
אנגלית
מי יכול לראות
כולם
הושלם על ידי AI
מה שמסומן בניצוץ נוסף על ידי AI.
נוצר באמצעות AI
Claude Haiku 5.5
נוצר

תגובות

  1. אין תגובות עדיין.
© כל הזכויות שמורות