Zebra0.com

csharp loopsC# Windows Forms Programming

C# Windows Forms Programming

Learn C# (C sharp) in FREE step-by-step lessons.

Quiz on Loops in C#

After doing all parts of the lesson you should be able to answer these questions.

Write down the answers first. Click the check to check your answer.

True or False: A loop always executes at least once. answer

   False, do loops always execute at least once, for and while loops execute zero or more times.

What would be shown in the listbox by the code shown below:

int num = 4;
do
{
     num = num - 1;
     listBox1.Items.Add(num);
     listBox1.Items.Add("done");
} while (num <= 0);  answer
   3
done

3 What best describes this loop: int num = 1; while(num>0) { num += 2;} answer

   endless

4 What best describes this loop: int num = 0; while(num>0) { num += 2;} answer

   executes 0 times

5 What would be put in the listbox by this code:
for(int num = 0; num < 3; num++) {
listBox1.Items.Add(num);} answer

   0, 1, 2

6 What would be shown in the listbox by this code:
for(int num=5; num>0; num--) {
lstBox.Items.Add num;
} answer

   5, 4, 3, 2, 1

7 What are the 3 types of loops? answer

   for, while, do (there is also a foreach that is not in this lesson)

8 True or False: A for loop always executes at least once. answer

   False

9 True or False: A do loop always executes at least once. answer

   True

Please review the material for any questions you miss.