Loops and Conditions in C#
שאלות 10
מפתח התשובות מוצג למורים מחוברים.
-
-
-
-
-
Choosing between if-else if and switch
Describe a situation where an if-else if chain is more suitable than a switch statement in C#, and explain why. Give one reason based on the type of condition being tested.
Consider whether the tests compare one value for equality or check ranges and combined conditions. -
Writing a loop with a condition
Write a short C# while loop that repeatedly reads an integer from the user with Console.ReadLine and Convert.ToInt32, and keeps asking until the number entered is between 1 and 100 inclusive. Describe the key parts: the loop condition and how the value is updated.
The loop should keep running while the input is invalid, so write the condition as the negation of the valid range, and make sure the variable is updated inside the body. -
-
Parts of a for loop
A C# for loop has three parts: an initializer, a ________________, and an iterator. Each pass through the loop body is called an ________________.
exit labeldeclarationloop conditioniterationterminationloop bodyreturn valueThe middle part of the for header decides whether another pass happens, and each pass is named after what it does. -
Conditions in if statements
An if statement in C# runs its block only when its condition evaluates to __________. A condition must produce a value of type __________, so writing an int in an if test causes a compile-time error. To compare two values, a programmer uses the __________ operator rather than the assignment operator.
intstringfalseequalityzeronulltrueboolConsider the Boolean value that lets the if block run, and the type that conditions must have in C#. -
Using foreach loops
A foreach loop in C# visits each element of a collection in turn, so it needs no counter variable. It works with any type that implements the _____________ interface. The iteration variable is _____________, so you cannot assign a new value to it inside the loop, or the compiler reports an error. Use a _____________ loop instead when you need to change elements by index.
IEquatableprivatestaticforIDisposableIComparableread-onlyIEnumerableThe interface name starts with I and describes something that can be looped over; the iteration variable cannot be changed, and an index-based loop keyword is needed.
פרטים
- מקצועות
- c#
- תגיות
- נוסף על ידי AI while loop נוסף על ידי AI do-while loop נוסף על ידי AI switch statement נוסף על ידי AI short-circuit evaluation נוסף על ידי AI if-else chain
- קהל יעד
- תיכון · כיתות י׳–י״ב
- רמת קושי
- 7 מתוך 10
- שפה
- אנגלית
- מי יכול לראות
- כולם
- הושלם על ידי AI
- מה שמסומן בניצוץ נוסף על ידי AI.
- נוצר באמצעות AI
- Claude Haiku 5.5
- נוצר
תגובות