agenda perform quiz #1 (20 minutes) loops –introduction / purpose –while loops structure /...

15
Agenda • Perform Quiz #1 (20 minutes) • Loops – Introduction / Purpose – while loops • Structure / Examples involving a while loop – do/while loops • Structure / Examples involving a do/while loop – Homework Question

Upload: hollie-simmons

Post on 19-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

Agenda

• Perform Quiz #1 (20 minutes)

• Loops– Introduction / Purpose– while loops

• Structure / Examples involving a while loop

– do/while loops• Structure / Examples involving a do/while loop

– Homework Question

Page 2: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

Loops

– Loops are used for repetition when running a program.

– To make sure the loop ends, we need a condition to control the loop; otherwise, the loop will continue forever!

Perform action

Page 3: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

Types of loops

• while and do/while Loops– The “while” and “do/while” loops are often

called indeterminate loops because they are best used when the number of repetitions are not known in advance.

• for Loops– The “for” loop is often called a determinate

loop since it is best used when the number of repetitions is already known.

Page 4: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

In Class Exercise #1

Which are the best types of loops for the following situations: determinate or indeterminate?– ask user to enter a group of marks until they are

finished.– print a multiplication table (1 - 12)– indicate that user has inputted an invalid

selection, and needs to try again (error checking)

Page 5: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

while loop

Testcondition

Commandsif TRUE

true

false

• The “while” loop test the condition first (referred to as a pre-test).

• Therefore, if the pre-test condition is FALSE, the loop is NOT executed.

Page 6: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

while loop - Example 1

main () { int i = 0; while ( i < 3 ) { printf (“i is: %d\n”, i); i = i + 1; } }

OUTPUT: i is: 0 i is: 1 i is: 2

Declare and assignvariable i (counter)

Pretest condition involvingvariable i

Perform action in while loopif test is TRUE, advancethe value of variable i (or “counter”) by 1 and return to pretest

Tip: the short-form ofi = i + 1; is i++;i = I - 1; is i--;

Page 7: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

while loop - Example 2

main () { int i ; printf (“\nEnter a positive integer: “); scanf (“%d”, &i); while ( i < 0 ) { printf (“Oops! Enter a positive integer: ”); scanf (“%d”, &i); } printf (“\n”); }

Declare and assignvariable i (used to store selection,NOT used as a counter!)

Prompt & scan value of i

Notice NO semicolon “;” when performingpretest and notice braces (code block)

Perform action if condition is TRUE(error-checking procedure). User isinformed of mistake and must re-enteruntil a positive integer is finally entered

Page 8: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

while loop - Example 2

main () { int i ; printf (“\nEnter a positive integer: “); scanf (“%d”, &i); while ( i < 0 ) { printf (“Oops! Enter a positive integer!: ”); scanf (“%d”, &i); } printf (“\n”); }

It is OK to give very simplevariable names like i, j, x or ywhen using them for loopingpurposes

Page 9: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

In Class Exercise #2

• Can you think of how both programs above could be modified to create “infinite loops?”

Page 10: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

while loop - Example 1

main () { int i = 0; while ( i != 3 ) { printf (“i is: %d\n”, i); i = i + 2; } }

OUTPUT: i is: 0 i is: 2 i is: 4 6 8 10 12, etc. etc. etc ---> forever!

Test condition will only be FALSE (thus stop the loop) if i is not equal to 3, but counter can skip 3 and go beyond 3! So you have an infinate loop!

Page 11: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

while loop - Example 2

main () { int i ; printf (“\nEnter a positive integer: “); scanf (“%d”, &i); while ( i < 0 ) { printf (“Oops! Enter a positive integer: ”); } printf (“\n”); }

WARNING: If you do NOTinclude an opportunity to allowuser to re-enter again, you willencounter an “infinite loop”!!

Page 12: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

do/while loop

Testcondition

Commands

true

false

• The do statement performs an action first, and then is followed by the while statement to test condition (referred to as a “post test”)

• Therefore, action is performed at least once, even if condition is FALSE

Page 13: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

do/while loop - Example 1

main ()

{

int i = 10;

do

{

printf (“i is: %d\n”, i);

i--;

} while (i > 0);

}

Declare and assignvariable i (counter)

Perform action first:Prompt & scan value of i

After action is performed once,then perform post test)

Notice while condition immediatelyafter closing brace for do statement,and semi-colon “;” after while statement!

Page 14: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

do/while loop - Example 2

main ()

{

int num;

do

{

printf (“\nGuess the secret number: ”, num);

scanf (“%d”, &num);

} while (num != 5);

printf (“That’s right!\n”);

printf (“The secret number is 5.\n\n”);

}

When post test is false(i.e. num is equal to 5, programcontinues, and prints themessage “That’s right!The secret number is 5.”

Declare variable num

Prompt user for number, and then test and repeat loop while num is not equal to 5

Page 15: Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure

Homework Question

• Create a program to prompt the user for a group of integers. If the user enters a negative integer, then the program should display the average of the entered integers.– What loop should you use?– Plan the program– Create & compile the source code