21
Java in two semesters by Quentin Charatan & Aaron Kans

Comp102 lec 6

Embed Size (px)

Citation preview

Page 1: Comp102   lec 6

Java in two semesters by Quentin Charatan & Aaron Kans

Page 2: Comp102   lec 6

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
Page 4: Comp102   lec 6

*************************

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

10987654321

for(int i=10; i>=1; i--){

System.out.println(i);}

Page 6: Comp102   lec 6

2468101214161820

for(int i=1; i<=20;i++){

if(i%2 ==0){

System.out.println(i);}

}

Page 7: Comp102   lec 6

*************************

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
Page 9: Comp102   lec 6

Often used to implement the fixed repetitions

Page 10: Comp102   lec 6
Page 11: Comp102   lec 6

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

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
Page 14: Comp102   lec 6

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

*************************

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

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
Page 18: Comp102   lec 6

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

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

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

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