Download presentation
Presentation is loading. Please wait.
1
LOOPS BY: LAUREN & ROMEO
2
There are 3 types of loops:
A loop allows you to execute a group of statements as many times as needed There are 3 types of loops: 1 2 3 Do-While While For
3
Do-while A loop that executes statements before checking the Boolean expression in the while statement. Always loops at least once EXAMPLE: Number is declared as 0 int number=0; do{ number+=1; }while(number<=10) 1 is added to the variable: number every time loop initiates CONDITION: loop initiates when the number is below or equal to 10.
4
while A while statement is a loop that checks the Boolean expression before executing the statements within the loop. Does not always loop once EXAMPLE: Number is declared as 0 int number=0; while(number<10) { number+=1; } 1 is added to the variable: number every time loop initiates CONDITION: loop initiates only when the number is below 10.
5
for A for statement is a counter loop that counts the number of times the statements in the loop are iterated EXAMPLE: Int i is declared as 0 Loop initiates when i is below or equal to 10 For (int i=0;i<=10;i++){ System.out.println(“Hello”); } 1 is added to i every time loop iterates Loop displays the phrase “hello” every time
6
Nested Loop EXAMPLE: A nested loop is a loop within a loop
for loop will iterate 10 times int num = 0; for (int i=0;i<=10;i++){ do{ num+=1; System.out.println(“Hello”); }while(num==i); } 1 is added to the variable: num every time loop iterates CONDITION: do-while loop will stop iterating when num is equal to i
7
Infinite Loops EXAMPLE: int number = 0; do { number += 1;
An infinite loop is a loop that contains an error to make it iterate infinite times because the condition is never met EXAMPLE: Number is declared as 0 int number = 0; do { number += 1; } while (number >0); 1 is added to the variable: number every time the loop iterates CONDITION: loop initiates when the number is greater than 0.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.