SlideShare a Scribd company logo
Java in two semesters by Quentin Charatan & Aaron Kans
 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
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-Loop
        for(int i=0; i<5; i++)
        {
          System.out.println(“*****”);
        }
10
9    for(int i=10; i>=1; i--)
8    {
7      System.out.println(i);
6    }
5
4
3
2
1
2
4
     for(int i=1; i<=20;i++)
6
     {
8
       if(i%2 ==0)
10
       {
12
           System.out.println(i);
14
       }
16
     }
18
20
for(int i=0; i<5; i++)
*****   {
*****     for(int j = 0; j<5; j++
*****     {
*****        System.out.print(“*”);
             System.out.println(“*”);
*****     }
        } System.out.println();
        }
Comp102   lec 6
 Often   used to implement the fixed repetitions
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
 Thenumber of repetitions is not fiexed but
 depends on some condition
   Non fixed Iterations
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
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");
                  }
         }
}
int i=0;
        while(i<5)
*****
        {
*****
          int j = 0;
*****
          while(j<5)
*****
          {
*****
              System.out.print(“*”);
              j++;
          }
          System.out.println();
          i++;
        }
 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
Comp102   lec 6
 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*/);
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’);
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);
 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

More Related Content

PPT
Java Programming: Loops
PPTX
Oop object oriented programing topics
PPT
Java Programmin: Selections
PPTX
Chapter 5.3
PPT
Menu Driven programs in Java
PDF
Integration Project Inspection 3
DOCX
PPTX
5 Cool Things About PLSQL
Java Programming: Loops
Oop object oriented programing topics
Java Programmin: Selections
Chapter 5.3
Menu Driven programs in Java
Integration Project Inspection 3
5 Cool Things About PLSQL

What's hot (20)

PDF
From Elixir to Akka (and back) - ElixirConf Mx 2017
PDF
Python Unit Test
PPTX
Computer Science Assignment Help
DOCX
lec4.docx
PPTX
Operating System Engineering Quiz
PDF
PLSQL Note
PPTX
Computer Science Homework Help
PPTX
Programming Assignment Help
PPTX
unittest in 5 minutes
TXT
Javascript
PPTX
Python Programming Essentials - M25 - os and sys modules
PPTX
Operating System Assignment Help
PPT
Java Threads
PPTX
tick cross game
PPTX
Breakpoints
PDF
Python unittest
PDF
The Ring programming language version 1.3 book - Part 17 of 88
PDF
Valgrind tutorial
PPT
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعة
PPTX
Operating System Engineering
From Elixir to Akka (and back) - ElixirConf Mx 2017
Python Unit Test
Computer Science Assignment Help
lec4.docx
Operating System Engineering Quiz
PLSQL Note
Computer Science Homework Help
Programming Assignment Help
unittest in 5 minutes
Javascript
Python Programming Essentials - M25 - os and sys modules
Operating System Assignment Help
Java Threads
tick cross game
Breakpoints
Python unittest
The Ring programming language version 1.3 book - Part 17 of 88
Valgrind tutorial
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعة
Operating System Engineering
Ad

Viewers also liked (20)

PDF
Czwrtk social media krakow 25072013_kpd
PPTX
Hybrid electric vehicles
PDF
Sintesis data spektroskopi
DOCX
Profile of sriharsa paul v1.0
PPTX
Proposal
DOCX
Profile of sriharsa paul v1.3
PPTX
UV ESL Center - Small Group Class - FILM STUDIES
PPTX
Proposal
PPTX
Trường Anh ngữ UV ESL
PPTX
Geografi
PPTX
Trường Anh ngữ UV ESL
PPT
Comp102 lec 4
PPT
Comp102 lec 5.1
PPT
Comp102 lec 8
PPT
Comp102 lec 10
PPT
Comp102 lec 9
PPT
Comp102 lec 5.0
PPTX
Đón học viên tại sân bay - Trường UV ESL
PDF
ちょっと怖くなくったRails
PDF
steam - its generation and use - 41st edition
Czwrtk social media krakow 25072013_kpd
Hybrid electric vehicles
Sintesis data spektroskopi
Profile of sriharsa paul v1.0
Proposal
Profile of sriharsa paul v1.3
UV ESL Center - Small Group Class - FILM STUDIES
Proposal
Trường Anh ngữ UV ESL
Geografi
Trường Anh ngữ UV ESL
Comp102 lec 4
Comp102 lec 5.1
Comp102 lec 8
Comp102 lec 10
Comp102 lec 9
Comp102 lec 5.0
Đón học viên tại sân bay - Trường UV ESL
ちょっと怖くなくったRails
steam - its generation and use - 41st edition
Ad

Similar to Comp102 lec 6 (20)

PPTX
130707833146508191
PPTX
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
PDF
how to write loops in java explained vividly
PDF
Cis 1403 lab5_loops
PPSX
Control structures
PPTX
Java Chapter 05 - Conditions & Loops: part 5
PDF
L8
PPTX
Java covers syntax, data types, object-oriented concepts, control flow, excep...
PPT
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
PDF
PDF
Lo43
PDF
DSA 103 Object Oriented Programming :: Week 3
PPT
Cso gaddis java_chapter4
PDF
Chapter 00 revision
PPTX
Java chapter 3
PPT
Computer Programming, Loops using Java
PPT
Java căn bản - Chapter6
PPT
Chap05
PDF
while- loop understanding with -ease.pdf
DOCX
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
130707833146508191
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
how to write loops in java explained vividly
Cis 1403 lab5_loops
Control structures
Java Chapter 05 - Conditions & Loops: part 5
L8
Java covers syntax, data types, object-oriented concepts, control flow, excep...
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
Lo43
DSA 103 Object Oriented Programming :: Week 3
Cso gaddis java_chapter4
Chapter 00 revision
Java chapter 3
Computer Programming, Loops using Java
Java căn bản - Chapter6
Chap05
while- loop understanding with -ease.pdf
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx

Comp102 lec 6

  • 1. Java in two semesters by Quentin Charatan & Aaron Kans
  • 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
  • 4. 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(“*****”); }
  • 5. 10 9 for(int i=10; i>=1; i--) 8 { 7 System.out.println(i); 6 } 5 4 3 2 1
  • 6. 2 4 for(int i=1; i<=20;i++) 6 { 8 if(i%2 ==0) 10 { 12 System.out.println(i); 14 } 16 } 18 20
  • 7. for(int i=0; i<5; i++) ***** { ***** for(int j = 0; j<5; j++ ***** { ***** System.out.print(“*”); System.out.println(“*”); ***** } } System.out.println(); }
  • 9.  Often used to implement the fixed repetitions
  • 11.  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
  • 12. 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
  • 14. 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"); } } }
  • 15. int i=0; while(i<5) ***** { ***** int j = 0; ***** while(j<5) ***** { ***** System.out.print(“*”); j++; } System.out.println(); i++; }
  • 16.  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
  • 18.  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*/);
  • 19. 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’);
  • 20. 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);
  • 21.  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