Adel Notes Week 4 (Chapter 4) Flashcards

1
Q

loops

A

to iterate and have something repeated. Each loop has three parts: initialization, condition, and update.

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

While-loop

A

runs until a condition returns false

	while(condition){ …//body of the loop }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do-while loop

A

runs the body of the loop once and then starts checking the condition(post-test); body gets executed at least once.

do{
…//body of the loop
}while(condition);

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

For-loop

A

<> an entry-controlled loop that is used to execute a block of code repeatedly for the specified range of values. Basically, for loop allows you to repeat a set of instructions for a specific number of iterations.

for(initialization; condition; update){
//body
}
for(int i = 100; i>=0; i–){

…//body of the loop
}

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

Notes

A

● Missing update can result in infinite loops. Make sure your loop condition returns false at some point to prevent the crashing of your program.

● Don’t add semi-colon after the condition parenthesis. This can result in infinite loop that will crash the program.
Ex:
while(7<10);

● Use for loop when you know how many times the body of the loop should repeat.

● Use do-while or while loop for data-validation. Get the data from the user. If invalid ask again until valid.

● Variable we make inside a loop are local and get destroyed when the loop ends.
Ex:
for(int i=0; i<=5;i++){
System.out.println(i);
}
System.out.println(i); // error

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

Are variables inside a loop or condition statement like if-statements, not locals? If so, can they be accessed?

A

variables defined inside a loop or condition statement like if statement are local and cannot be accessed outside the loop or conditional statement. It would result in an error.

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

Break

A

break keyword closes the loop.

    for(int i=1; i<=10;i++){
        if (i == 6)break;
else System.out.print(i); //12345
    }
    System.out.println(i);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Continue

A

It moves on to the next iteration.

for(int i=1; i<=10;i++){
if (i == 6)continue;
else System.out.print(i); //1234578910
}

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

Running totals

A

running total is a variable that accumulates overtime.

ex: total = total + sale;

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

Counter

A

to keep track of a number of times something has happened. Usually starts at 0 and Increments as needed.
Ex:
double totalSales = 0.0;
int numSales = 0;
int userInput ;
while(moreSales){
//get user input

if(userInput == sentinel) {
moreSales = False; //to signal when to stop.
break;
}
numSales++; //increment counter
totalSales += userInput;
}

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

Sentinel value

A

a value to stop the loop. used to notify the program to stop acquiring input.

For example, while entering positive numbers, a sentinel value might be -1, or while entering names, a sentinel value might be done, end, or quit.

We usually use a sentinel value when we don’t know the exact number of iterations in a loop.

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

Data Validation/Input Validation

A

make sure the data user inputs is what is expected and won’t crash the program. One common way is to use do-while loop or while loop and repeat while the input is invalid.
ex:
int numSales;

//the following makes sure the user doesn’t input negative values.
do{
cin&raquo_space; numSales;
}while(numSales < 0)

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