Chapter 4 : Looping Flashcards

1
Q

How to add items to ListBox?

A

ListBoxName.Items.Add(Item);

  • Item is the value to be added to the items property

namesListBox.Items.Add(“Chris”);
namesListBox.Items.Add(“Alicia”);
namesListBox.Items.Add(10);
namesListBox.Items.Add(20);
namesListBox.Items.Add(17.5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to clear items to ListBox?

A
  1. ListBoxName.Items.Clear();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to count items stored in ListBox?

A
  1. ListBoxName.Items.Count();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is for loop specially designed for?

A
  1. Situations requiring a counter variable to control the number of times that a loop iterates
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List out 3 actions that are required for the for loop

A
  1. Initialization
    • One-time expression that defines the initial value of the counter
  2. Test
    • A boolean expression to be tested
  3. Update
    • Increase or decrease the value of the counter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the format for for loop?

A

for ( initializationExpress; textExpression; updateExpression ){}

  • For loop is a pretest loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List out examples for using for loop

A

for (int count = 0; count <=100; count += 10) → count = count + 10
{
MessageBox.Show(count.ToString());
}

for (int count = 10; count >=0; count–)
{
MessageBox.Show(count.ToString());
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the while loop causes?

A
  1. A statement or set of statements to repeat as long as a Boolean expression is true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List out 2 parts for while loop

A
  1. A boolean expression that is tested for a true or false value
  2. A statement or set of statements that is repeated a long as the Boolean expression is true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

List out format for while loop

A

counter declaration
while ( BooleanExpression )
{
Statements;
counter increment / decrement
}

  • First line is called the while clause
    Statements inside the curly braces are the body of the loop
    When a while loop executes, the Boolean expression is tested, if true, the statements are executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the term used each time the loop executes its statements again and again?

A
  1. Iteration
  • While loop is a pretest loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a dead loop?

A
  1. A loop that will never gets executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the issue caused by a loop become a dead loop?

A
  1. Logical error in the boolean expression

// count variable is always greater than 5
int count = 5;
while (count > 5)
{
MessageBox.Show(“Hello”);
count=count+1;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List out 3 ways to increase 1of the value

A
  1. count = count + 1;
  2. count++;
  3. count += 1;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

List out 3 ways to decrease 1of the value

A
  1. count = count - 1;
  2. count–;
  3. count -= 1;
17
Q

What is Postfix Mode?

A
  1. Means to place the ++ and – operators after their operands

count++

int a, b;
a = 50;
MessageBox.Show(a++); // 50
b = a;
MessageBox.Show(a); // 51
MessageBox.Show(b); //51

18
Q

What is Prefix Mode?

A
  1. Means to place the ++ and – before their operands

–count

int a, b;
a = 50;
MessageBox.Show(++a); // 51
b = a;
MessageBox.Show(a); //51
MessageBox.Show(b); //51

19
Q

Is do-while loop a posttest loop?

A
  1. Yes
  • One or more statements are executed before a Boolean expression is tested
20
Q

What is the format for do-while loop?

A

do
{
statement(s);
} while (BooleanExpression);

21
Q
A