0
0
C Sharp (C#)programming~10 mins

Why loops are needed in C Sharp (C#) - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a loop.

C Sharp (C#)
for (int i = 1; i [1] 5; i++)
{
    Console.WriteLine(i);
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= will not run the loop correctly.
Using < will exclude 5 from printing.
2fill in blank
medium

Complete the code to sum numbers from 1 to 10 using a loop.

C Sharp (C#)
int sum = 0;
for (int num = 1; num [1] 10; num++)
{
    sum += num;
}
Console.WriteLine(sum);
Drag options to blanks, or click blank then click option'
A<
B>=
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will exclude 10 from the sum.
Using > or >= will cause the loop not to run as expected.
3fill in blank
hard

Fix the error in the loop condition to print all elements of the array.

C Sharp (C#)
string[] fruits = {"apple", "banana", "cherry"};
for (int i = 0; i [1] fruits.Length; i++)
{
    Console.WriteLine(fruits[i]);
}
Drag options to blanks, or click blank then click option'
A<=
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes an index out of range error.
Using > or >= will not enter the loop correctly.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

C Sharp (C#)
for (int num = [1]; num [2] 10; num += 2)
{
    Console.WriteLine(num);
}
Drag options to blanks, or click blank then click option'
A2
B<
C<=
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will print odd numbers.
Using < will exclude 10 from printing.
5fill in blank
hard

Fill all three blanks to create a loop that prints numbers from 5 down to 1.

C Sharp (C#)
for (int i = [1]; i [2] 1; i [3])
{
    Console.WriteLine(i);
}
Drag options to blanks, or click blank then click option'
A5
B>=
C--
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ will increase i and cause an infinite loop.
Using < instead of >= will skip printing 1.