New

## Long-Term Learning

Learn efficiently and remember over time.

## Start Long-Term Learning

Get personalized study reminders at intervals optimized for better retention.
Track your progress on this set by creating a folder
Or add to an existing folder

## Add this set to a folder

• ### What is a loop?

It is a construct that allows you to repeatedly execute a block of code.

### Explain this for loop. { for (int i=0; i<12; i++) {sum += evens[i]; printf("sum %d \n", sum); } return 0; }

The for loop has three statements within the set of parentheses.
a. First we are initializing an integer variable "i" to "0"
b. Second a Boolean expression which "i", to the number of items in the array (3).
c. Thirdly we are auto incrementing "i" if the value of the index is <12.

(i) Then within the code block {} we are incrementally summing the values that are in the array "even".

(ii) w/in the block we are indexing the variables using the variable "i".

### What are the While and do while loops?

Simply allow for executing a block of code without providing any construct for keeping a counter.

### What are the drawbacks of the while and do while loops?

It is easy to make the common error of the "infinite death loop".

### What is inside the parenthesis? for (int i=0; i<12; i++)

The conditional expression.

### What is the purpose of a do while conditional expression?

The code within the block will always be evaluated once before the condition is reached.
This is why it is used.

#include <stdio.h>

int main()
{
char letters[] = {'a','b','c'};
int w = 0;
do
{
printf("letter %d is %c \n", w, letters[w]);
} while (w<4);

return 0;
}

### What is the cause of the bug that is known as the infinite loop?

The variable is not auto incremented, so it repeatedly traverses the same index value in the array.

#include <stdio.h>

int main()
{
char letters[] = {'a','b','c'};
int w = 0;
do
{
printf("letter %d is %c \n", w, letters[w]);
++w; }
while (w<4);

return 0;
}

### Is the do while loop frequently used in practice?

No it is not. The for loop and while loop are used for when you want to traverse arrays or do any other type of looping.

### Which of the following is not a looping construct?

do-until loop

### Which is a valid for loop that will execute exactly 5 times?

for ( int = 0 ; i < 5; i++ ) {}

letters[1];

### How many times will the following loop execute? int i = 0; while ( i < 1 ) { printf("looping"); }

Infinite because it neglects the auto incrementation of the variable within the conditional expression.

once

Example: