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++ ) {}

    How would you access the second element from a character array named "letters".

    char letters[] = {'a','b','c','d'};

    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.

    How many times will the following loop execute?

    int i = 1;
    do {
    printf("looping");
    }
    while ( i < 1 ) ;

    once

    Please allow access to your computer’s microphone to use Voice Recording.

    Having trouble? Click here for help.

    We can’t access your microphone!

    Click the icon above to update your browser permissions above and try again

    Example:

    Reload the page to try again!

    Reload

    Press Cmd-0 to reset your zoom

    Press Ctrl-0 to reset your zoom

    It looks like your browser might be zoomed in or out. Your browser needs to be zoomed to a normal size to record audio.

    Please upgrade Flash or install Chrome
    to use Voice Recording.

    For more help, see our troubleshooting page.

    Your microphone is muted

    For help fixing this issue, see this FAQ.

    Star this term

    You can study starred terms together

    NEW! Voice Recording

    This is a Plus feature