Comp102 lec 6
-
Upload
fraz-bakhsh -
Category
Documents
-
view
275 -
download
0
Transcript of Comp102 lec 6
![Page 1: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/1.jpg)
Java in two semesters by Quentin Charatan & Aaron Kans
![Page 2: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/2.jpg)
Computers can repeat the same tasks over and over, again and again
Form of program control that allows us to instruct the computer to carry out a task by repeating a section of code Iteration Loop
Types of Loops for loop while loop do...while loop
![Page 3: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/3.jpg)
![Page 4: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/4.jpg)
*************************
System.out.println(“*****”);System.out.println(“*****”);System.out.println(“*****”);System.out.println(“*****”);System.out.println(“*****”);Loop 1…5
System.out.println(“*****”);End-Loopfor(int i=0; i<5; i++){
System.out.println(“*****”);}
![Page 5: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/5.jpg)
10987654321
for(int i=10; i>=1; i--){
System.out.println(i);}
![Page 6: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/6.jpg)
2468101214161820
for(int i=1; i<=20;i++){
if(i%2 ==0){
System.out.println(i);}
}
![Page 7: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/7.jpg)
*************************
for(int i=0; i<5; i++){
for(int j = 0; j<5; j++{
System.out.println(“*”);}
}
for(int i=0; i<5; i++){
for(int j = 0; j<5; j++{
System.out.print(“*”);}System.out.println();
}
![Page 8: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/8.jpg)
![Page 9: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/9.jpg)
Often used to implement the fixed repetitions
![Page 10: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/10.jpg)
![Page 11: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/11.jpg)
Consider the following scenarios A racing game that repeatedly moves a car around
until the car crashes A ticket issuing program that repeatedly offers
tickets for sale until he user chooses to quit the program
A password checking program that does not let a user enter into an application until he or she enters the right password
The number of repetitions is not fiexed but depends on some condition Non fixed Iterations
![Page 12: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/12.jpg)
while (/*test goes here*/){
//instruction(s) to be repeated}
simpler to construct If not used as counter loop, then it is not required
to keep a counter Normally used to validate the input
![Page 13: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/13.jpg)
![Page 14: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/14.jpg)
import java.util.Scanner;public class Assignment1 {
public static void main(String[] args) {
int marks;Scanner input = new Scanner(System.in);marks = input.nextInt();while(marks<0 || marks>100){
System.out.println("Invalid Marks - ReEnter?");marks = input.nextInt();
}if(marks>=40){
System.out.println("Congratulations... You passed");}else{
System.out.println("Sorry you failed the course");}
}}
![Page 15: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/15.jpg)
*************************
int i=0;while(i<5){
int j = 0;while(j<5){
System.out.print(“*”);j++;
}System.out.println();i++;
}
![Page 16: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/16.jpg)
Tests the condition at beginning If the condition is false at start, makes the loop to
not execute ever Loop executes ZERO or MORE times
![Page 17: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/17.jpg)
![Page 18: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/18.jpg)
Non – Fixed LoopCondition is tested at the end of the loopMakes the loop to iterate at least onceMakes the loop to iterate ONE or MORE timeswhile loop terminates once the program has done its job.
Do while is suitable if we wish to re-run the same program again based on user responseSyntax:
do{
//instruction(s) to be repeated goes here}while (/* test goes here*/);
![Page 19: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/19.jpg)
char response;do{
Scanner input = new Scanner(System.in);//program instructions go hereSystem.out.println(“Want to re-run? (y/n)”);response = input.next().charAt(0);
}while(response ==‘y’);
![Page 20: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/20.jpg)
do{
System.out.println("1 - for group A");System.out.println("2 - for group B");System.out.println("3 - Quit");response = sc.nextInt();switch(response){
case 1:System.out.println("10:00 AM");break;
case 2:System.out.println("11:00 AM");break;
case 3:System.out.println("Good Bye");break;
default:System.out.println("Invalid Input");break;
}}while(response!= 3);
![Page 21: Comp102 lec 6](https://reader034.fdocuments.in/reader034/viewer/2022050801/5549d553b4c905856d8b511b/html5/thumbnails/21.jpg)
For Loop Number of repetitions required can be determined
prior to entering the loopWhile Loop
Number of repetitions cannot be determined prior to entering the loop and
Zero repetitions is allowedDo… while loop
Number of repetitions cannot be determined before the loop
You require at least one repetition of loop