warm-up: mon, apr 7 what are loops used for in programming? what are at least 2 different kinds of...

23
WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?

Upload: garry-carroll

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

WARM-UP: MON, APR 7

• What are loops used for in programming?

• What are at least 2 different kinds of loops?

CHAPTER 5: LOOPSPRE-AP COMPUTER SCIENCE

CYCLE 6

REVIEW: IF STATEMENTS

• Used to make decisions/answer questions

• Formed using a conditional statement

• Conditional operators

• IF IF-ELSE IF – ELSE-IF – ELSE

IF STRUCTURE

if (condition1) {

//statements

}

else if (condition2) {

//statements

}

else {

//statements

}

LOOPS

• Used to repeat tasks

• Benefits

• Reduce amount of code required• Reduce copy-pasting / repetition• Increase code efficiency (speed)• Makes code more readable / easier to

understand

TYPES OF LOOPS• WHILE Loops

• Tasks with unknown stopping points• “infinite” loops

• DO-WHILE Loops

• Tasks with unknown stopping points that MUST execute at least once

• FOR Loops

• Repeat ‘x’ times

TYPES OF LOOPS• WHILE Loops

• Tasks with unknown stopping points• “infinite” loops

• DO-WHILE Loops

• Tasks with unknown stopping points that MUST execute at least once

• FOR Loops

• Repeat ‘x’ times

WHILE LOOPS - STRUCTURE

while (condition)

{

//statements

//counter/environment change

}

WHILE LOOPS - STRUCTURE

while (condition)

{

//statements

//counter/environment change

}

Statements – the tasks you want repeated

Counter change – prevents infinite loops

EXAMPLE A: PRINT 1 THRU 100

int number = 1;

while (number <= 100)

{

System.out.println(number);

number++;

}

EXAMPLE B: PRINT 1 THRU AN INPUTTED NUMBER

int number = 1;

int stop = console.nextInt();

while (number <= stop)

{

System.out.println(number);

number++;

}

EXAMPLE C: PRINT 10 MULTIPLES OF AN INPUTTED NUMBER

int multiple = 1;

int number = console.nextInt();

while (multiple <= 10)

{

System.out.println(number*multiple);

multiple++;

}

WARM-UP: APR 8

Write the standard structure for a WHILE loop.

Why is it necessary to change the counter or environment inside of a WHILE loop? What will happen if you do not?

WARM-UP: APR 9

Correct the following code so that it finds the sum of 10 numbers.

int sum = 0;

while (count < 10)

num = console.nextInt();

sum = sum + num;

count++;

REVIEW: WHILE LOOPS

• Typically used for looping situations where you do not know how many times the loop will iterate

• Not always counter-controlled

while (condition) {

//statements

//counter/environment change

}

EXAMPLE: PRINT 1 THRU AN INPUTTED NUMBER

int number = 1;

int stop = console.nextInt();

while (number <= stop)

{

System.out.println(number);

number++;

}

FOR LOOPS

• Typically used in looping situations where the number of iterations is known

• Always counter controlled

FOR LOOPS - STRUCTURE

for (start; stop; change)

{

//statements

}

Start, stop, change refer to the counter

EXAMPLE A: PRINT THE NUMBERS 1 THRU 100

int counter;

for (counter=1; counter<=100; counter++)

{

System.out.println(counter);

}

EXAMPLE B: PRINT THE WORD “HELLO” 5 TIMES

int i;

for (i=1; i<=5; i++)

{

System.out.println(“Hello”);

}

EXAMPLE C: PRINT OUT ALL EVEN NUMBERS BETWEEN 0 AND 1000

for (int num=0; num<=1000; num=num+2)

{

System.out.println(num);

}

FOR VS WHILE

WHILE

int i=0;

while (i < 100)

{

System.out.print(i);

i++;

}

FOR

for (int i=0; i < 100; i++)

{

System.out.print(i);

}

WARM-UP: APR 10

• Write a FOR loop that would loop 25 times with a counter that begins at 12.

QUIZ TOMORROW!