230 likes | 386 Views
This guide delves into the fundamentals of loops within programming, essential for automating repetitive tasks. We’ll explore different types of loops including WHILE, DO-WHILE, and FOR loops, each with its unique structure and use cases. You'll learn when to use each type, how to prevent infinite loops, and the significance of manipulating counters in WHILE loops. Through practical examples, such as printing numbers or sums based on user input, we'll provide a solid foundation for understanding and implementing loops effectively in your code.
E N D
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 • 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 • inti; • for (i=1; i<=5; i++) • { • System.out.println(“Hello”); • }
Example C: Print out all even numbers between 0 and 1000 • for (intnum=0; num<=1000; num=num+2) • { • System.out.println(num); • }
FOR vs WHILE WHILE FOR for (inti=0; i < 100; i++) { System.out.print(i); } • inti=0; • while (i < 100) • { • System.out.print(i); • i++; • }
Warm-Up: Apr 10 • Write a FOR loop that would loop 25 times with a counter that begins at 12. • QUIZ TOMORROW!