warm-up: mon, apr 7
DESCRIPTION
Warm-up: Mon, apr 7. What are loops used for in programming? What are at least 2 different kinds of loops? . Chapter 5: Loops. Pre-AP Computer Science Cycle 6. Review: IF Statements. Used to make decisions/answer questions Formed using a conditional statement Conditional operators - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/1.jpg)
WARM-UP: MON, APR 7• What are loops used for in
programming?
• What are at least 2 different kinds of loops?
![Page 2: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/2.jpg)
CHAPTER 5: LOOPSPRE-AP COMPUTER SCIENCECYCLE 6
![Page 3: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/3.jpg)
REVIEW: IF STATEMENTS• Used to make decisions/answer
questions
• Formed using a conditional statement• Conditional operators
• IF IF-ELSE IF – ELSE-IF – ELSE
![Page 4: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/4.jpg)
IF STRUCTUREif (condition1) { //statements}else if (condition2) { //statements}else { //statements}
![Page 5: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/5.jpg)
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
![Page 6: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/6.jpg)
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
![Page 7: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/7.jpg)
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
![Page 8: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/8.jpg)
WHILE LOOPS - STRUCTUREwhile (condition) { //statements //counter/environment change}
![Page 9: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/9.jpg)
WHILE LOOPS - STRUCTUREwhile (condition) { //statements //counter/environment change}
Statements – the tasks you want repeatedCounter change – prevents infinite loops
![Page 10: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/10.jpg)
EXAMPLE A: PRINT 1 THRU 100 int number = 1;while (number <= 100){
System.out.println(number);number++;
}
![Page 11: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/11.jpg)
EXAMPLE B: PRINT 1 THRU AN INPUTTED NUMBERint number = 1;int stop = console.nextInt();while (number <= stop){
System.out.println(number);number++;
}
![Page 12: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/12.jpg)
EXAMPLE C: PRINT 10 MULTIPLES OF AN INPUTTED NUMBERint multiple = 1;int number = console.nextInt();while (multiple <= 10){
System.out.println(number*multiple);multiple++;
}
![Page 13: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/13.jpg)
WARM-UP: APR 8Write 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?
![Page 14: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/14.jpg)
WARM-UP: APR 9Correct 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++;
![Page 15: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/15.jpg)
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
}
![Page 16: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/16.jpg)
EXAMPLE: PRINT 1 THRU AN INPUTTED NUMBERint number = 1;int stop = console.nextInt();while (number <= stop){
System.out.println(number);number++;
}
![Page 17: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/17.jpg)
FOR LOOPS• Typically used in looping situations
where the number of iterations is known
• Always counter controlled
![Page 18: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/18.jpg)
FOR LOOPS - STRUCTUREfor (start; stop; change){
//statements}
Start, stop, change refer to the counter
![Page 19: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/19.jpg)
EXAMPLE A: PRINT THE NUMBERS 1 THRU 100
int counter;for (counter=1; counter<=100; counter++){
System.out.println(counter);}
![Page 20: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/20.jpg)
EXAMPLE B: PRINT THE WORD “HELLO” 5 TIMES
int i;for (i=1; i<=5; i++){
System.out.println(“Hello”);}
![Page 21: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/21.jpg)
EXAMPLE C: PRINT OUT ALL EVEN NUMBERS BETWEEN 0 AND 1000
for (int num=0; num<=1000; num=num+2){
System.out.println(num);}
![Page 22: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/22.jpg)
FOR VS WHILEWHILE
int i=0;while (i < 100){ System.out.print(i); i++;}
FORfor (int i=0; i < 100; i++){ System.out.print(i);}
![Page 23: Warm-up: Mon, apr 7](https://reader036.vdocuments.net/reader036/viewer/2022062315/568165cd550346895dd8dbf3/html5/thumbnails/23.jpg)
WARM-UP: APR 10 • Write a FOR loop that would loop 25
times with a counter that begins at 12.
QUIZ TOMORROW!