Adel Notes Week 4 (Chapter 4) Flashcards
loops
to iterate and have something repeated. Each loop has three parts: initialization, condition, and update.
While-loop
runs until a condition returns false
while(condition){ …//body of the loop }
Do-while loop
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);
For-loop
<> 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
}
Notes
● 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
Are variables inside a loop or condition statement like if-statements, not locals? If so, can they be accessed?
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.
Break
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);
Continue
It moves on to the next iteration.
for(int i=1; i<=10;i++){
if (i == 6)continue;
else System.out.print(i); //1234578910
}
Running totals
running total is a variable that accumulates overtime.
ex: total = total + sale;
Counter
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;
}
Sentinel value
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.
Data Validation/Input Validation
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»_space; numSales;
}while(numSales < 0)