1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson linglu@iupui.edu Department...

Preview:

Citation preview

1

CSCI N201Programming Concepts and Database

9 – Loops

Lingma Achesonlinglu@iupui.edu

Department of Computer and Information Science, IUPUI

Task:

• Simulate a race counting laps. Output “Now on Lap X”. For simplicity, count only Lap 1 – 10.

• 10 output statements?• Same output statement, can we reuse it?• Solution – “For Loop” structure

Loops

• A loop is used for repetitive behaviour• A set of commands is placed in a code

block• A condition determines when the block

stops repeating

The For Loop

• Used when you know how many times something will happen

• Great for counting

Parts of a For Loop

• Sentry Variable (counter)• Initial Value (the value where the counter

starts)• Looping Condition (repeat the codes as long

as the counter meets the condition)• Increment Statement (how to increment the

counter)• Lap counter example:

– Sentry variable (create a variable called i)– Initial value: 1– Looping condition: i<=10– Increment Statement: i++ (i increase by 1)

Algorithm• Pseudocode:

- Start a new program

- Create a new variable i- Create a For loop (Conditional =>For loop)

- Tell the For loop to use i as the counter - Give the starting value of i - Give the ending value of i - Give the increment method

- What command to execute if the condition is true: output “Now on Lap” + i

- End For loop - End the program

Miracle• http://www.cs.iupui.edu/~aharris/MirJS.html • Loop Tracking –

– i=1Is i<=10? Yes! Execute the codes. i increase by 1.

- i = 2Is i<=10? Yes! Execute the codes. i increase by 1.

- i = 3Is i<=10? Yes! Execute the codes. i increase by 1.

…- i = 10

Is i<=10? Yes! Execute the codes. i increase by 1.- i = 11

Is i<=10? No!!! Loop finishes.

Miracle• Lab – Time to create the Lap Counter!

Increment Statement

• for(i=1; i<=10; i++){…}

• A line of code• Always involves sentry variable (counter)• Changes value of sentry• Usually i++, i--, i=i+2,…• Must make it possible for condition to

become false eventually– for(i=11; i>=10; i++){ //WRONG!

…}

More example• Counting backwards. New Year Eve Celebration – counting seconds left.

E.g. for(i=10; i>=0; i--){//output: i + “ seconds left!”}

• Generating even or odd numbers. E.g. for(i=0; i<=20; i=i+2){//output: i + “ is an even number.”}

For Loop• Lab – Create a program that output even or odd

numbers smaller than 12.

The While Loop• Another type of Loop structure• Use the word “while”• Logic – as long as the condition is true,

execute the code block (stop executing the code block when the condition becomes false)

• Requires:– Sentry Variable (counter)– Initial Value (the value where the counter starts)– Looping Condition (repeat the codes as long as

the counter meets the condition)– Increment Statement (how to increment the

counter) 12

Practice While• Using Miracle to create the Lap Counter

with the While Loop structure http://www.cs.iupui.edu/~aharris/MirJS.html

• Algorithm: Pseudocode - - Start a new program- Create a new variable i- Give the starting value of i for the while loop- Create a while loop (Conditional =>While loop)

- Give the condition (i<=10) - What command to execute if the condition is true:

output “Now on Lap” + i- Give the increment method (i++)

- End while loop - End the program

13

0 or Infinite Loops• Code block might not get executed at all,

if the condition is false in the beginning• E.g. var i=1;

while (i<1) {Output “Now on lap ” + i;i++;

}

• Must make sure the condition becomes false so the code block can stop executing, otherwise code goes into a infinite loop.

• E.g. var i=1;while (i>=1) {

Output “Now on lap ” + i;i++;

}14

Compare If, For and While

• Compare If branch, For Loop and While Loopfor(i=1; i<=10; i++){ …

}

i=1;While(i<=10) { … i++;}

i=1; If(i<=10){ … i++;

}

While Loop• Lab –

– Create a program that takes a user input, and output even or odd numbers within the user input, e.g. if user input is 10, output 0, 2, 4, 6, 8, 10.

Recommended