Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Comp102 lec 6

408 views

Published on

  • Be the first to comment

  • Be the first to like this

Comp102 lec 6

  1. 1. Java in two semesters by Quentin Charatan & Aaron Kans
  2. 2.  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
  3. 3. System.out.println(“*****”);***** System.out.println(“*****”);***** System.out.println(“*****”);***** System.out.println(“*****”);***** System.out.println(“*****”);***** Loop 1…5 System.out.println(“*****”); End-Loop for(int i=0; i<5; i++) { System.out.println(“*****”); }
  4. 4. 109 for(int i=10; i>=1; i--)8 {7 System.out.println(i);6 }54321
  5. 5. 24 for(int i=1; i<=20;i++)6 {8 if(i%2 ==0)10 {12 System.out.println(i);14 }16 }1820
  6. 6. for(int i=0; i<5; i++)***** {***** for(int j = 0; j<5; j++***** {***** System.out.print(“*”); System.out.println(“*”);***** } } System.out.println(); }
  7. 7.  Often used to implement the fixed repetitions
  8. 8.  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 Thenumber of repetitions is not fiexed but depends on some condition  Non fixed Iterations
  9. 9. 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
  10. 10. 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"); } }}
  11. 11. int i=0; while(i<5)***** {***** int j = 0;***** while(j<5)***** {***** System.out.print(“*”); j++; } System.out.println(); i++; }
  12. 12.  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
  13. 13.  Non – Fixed Loop Condition is tested at the end of the loop Makes the loop to iterate at least once Makes the loop to iterate ONE or MORE times while 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 response Syntax:do{ //instruction(s) to be repeated goes here}while (/* test goes here*/);
  14. 14. char response;do{ Scanner input = new Scanner(System.in); //program instructions go here System.out.println(“Want to re-run? (y/n)”); response = input.next().charAt(0);}while(response ==‘y’);
  15. 15. 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);
  16. 16.  For Loop  Number of repetitions required can be determined prior to entering the loop While Loop  Number of repetitions cannot be determined prior to entering the loop and  Zero repetitions is allowed Do… while loop  Number of repetitions cannot be determined before the loop  You require at least one repetition of loop

×