Chapter 4 : Looping Flashcards
How to add items to ListBox?
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 to clear items to ListBox?
- ListBoxName.Items.Clear();
How to count items stored in ListBox?
- ListBoxName.Items.Count();
What is for loop specially designed for?
- Situations requiring a counter variable to control the number of times that a loop iterates
List out 3 actions that are required for the for loop
- Initialization
- One-time expression that defines the initial value of the counter
- Test
- A boolean expression to be tested
- Update
- Increase or decrease the value of the counter
What is the format for for loop?
for ( initializationExpress; textExpression; updateExpression ){}
- For loop is a pretest loop
List out examples for using for loop
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());
}
What does the while loop causes?
- A statement or set of statements to repeat as long as a Boolean expression is true
List out 2 parts for while loop
- A boolean expression that is tested for a true or false value
- A statement or set of statements that is repeated a long as the Boolean expression is true
List out format for while loop
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
What is the term used each time the loop executes its statements again and again?
- Iteration
- While loop is a pretest loop
What is a dead loop?
- A loop that will never gets executed
What is the issue caused by a loop become a dead loop?
- 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;
}
List out 3 ways to increase 1of the value
- count = count + 1;
- count++;
- count += 1;
List out 3 ways to decrease 1of the value
- count = count - 1;
- count–;
- count -= 1;
What is Postfix Mode?
- 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
What is Prefix Mode?
- 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
Is do-while loop a posttest loop?
- Yes
- One or more statements are executed before a Boolean expression is tested
What is the format for do-while loop?
do
{
statement(s);
} while (BooleanExpression);