Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to exit the loop when the number is 5.
C Sharp (C#)
for (int i = 0; i < 10; i++) { if (i == [1]) { break; } Console.WriteLine(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number other than 5 in the condition.
Forgetting to use the break statement.
✗ Incorrect
The break statement stops the loop when i equals 5.
2fill in blank
mediumComplete the code to break out of the while loop when count reaches 3.
C Sharp (C#)
int count = 0; while (true) { Console.WriteLine(count); count++; if (count == [1]) { break; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong number in the condition.
Not incrementing the count variable.
✗ Incorrect
The loop stops when count equals 3 due to the break statement.
3fill in blank
hardFix the error in the code to break the loop when the character 'q' is found.
C Sharp (C#)
char[] letters = { 'a', 'b', 'c', 'q', 'd' };
foreach (char letter in letters) {
if (letter == [1]) {
break;
}
Console.WriteLine(letter);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes for a character.
Using a variable name instead of a character literal.
✗ Incorrect
Characters in C# use single quotes. So 'q' is correct to compare a char.
4fill in blank
hardFill both blanks to create a loop that breaks when the sum exceeds 10.
C Sharp (C#)
int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; if (sum [1] [2]) { break; } } Console.WriteLine(sum);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of >.
Using 5 instead of 10.
✗ Incorrect
The loop breaks when sum is greater than 10.
5fill in blank
hardFill all three blanks to create a loop that breaks when the user inputs 'exit'.
C Sharp (C#)
string input;
do {
Console.WriteLine("Type something (or 'exit' to quit):");
input = Console.ReadLine();
if (input [1] [2]) {
[3];
}
} while (true); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ==.
Forgetting to use break to exit the loop.
✗ Incorrect
The loop breaks when input == "exit".