SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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
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();
        }
 Often   used to implement the fixed repetitions
 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
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
 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

Weitere ähnliche Inhalte

Was ist angesagt?

From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017Agustin Ramos
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit TestDavid Xie
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutesRay Toal
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesP3 InfoTech Solutions Pvt. Ltd.
 
The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88Mahmoud Samir Fayed
 
Valgrind tutorial
Valgrind tutorialValgrind tutorial
Valgrind tutorialSatabdi Das
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة السابعةشرح مقرر البرمجة 2   لغة جافا - الوحدة السابعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعةجامعة القدس المفتوحة
 

Was ist angesagt? (20)

From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
Operating System Engineering Quiz
Operating System Engineering QuizOperating System Engineering Quiz
Operating System Engineering Quiz
 
PLSQL Note
PLSQL NotePLSQL Note
PLSQL Note
 
Computer Science Homework Help
Computer Science Homework HelpComputer Science Homework Help
Computer Science Homework Help
 
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutes
 
Javascript
JavascriptJavascript
Javascript
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
tick cross game
tick cross gametick cross game
tick cross game
 
Breakpoints
BreakpointsBreakpoints
Breakpoints
 
Python unittest
Python unittestPython unittest
Python unittest
 
The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88
 
Valgrind tutorial
Valgrind tutorialValgrind tutorial
Valgrind tutorial
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة السابعةشرح مقرر البرمجة 2   لغة جافا - الوحدة السابعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعة
 
Operating System Engineering
Operating System EngineeringOperating System Engineering
Operating System Engineering
 

Andere mochten auch

Sintesis data spektroskopi
Sintesis data spektroskopiSintesis data spektroskopi
Sintesis data spektroskopiDini Fatharani
 
Profile of sriharsa paul v1.0
Profile of sriharsa paul v1.0Profile of sriharsa paul v1.0
Profile of sriharsa paul v1.0Sriharsha Paul
 
Profile of sriharsa paul v1.3
Profile of sriharsa paul v1.3Profile of sriharsa paul v1.3
Profile of sriharsa paul v1.3Sriharsha Paul
 
UV ESL Center - Small Group Class - FILM STUDIES
UV ESL Center - Small Group Class - FILM STUDIESUV ESL Center - Small Group Class - FILM STUDIES
UV ESL Center - Small Group Class - FILM STUDIESUV ESL Center
 
Trường Anh ngữ UV ESL
Trường Anh ngữ UV ESL Trường Anh ngữ UV ESL
Trường Anh ngữ UV ESL UV ESL Center
 
Trường Anh ngữ UV ESL
Trường Anh ngữ UV ESL Trường Anh ngữ UV ESL
Trường Anh ngữ UV ESL UV ESL Center
 
Đón học viên tại sân bay - Trường UV ESL
Đón học viên tại sân bay - Trường UV ESLĐón học viên tại sân bay - Trường UV ESL
Đón học viên tại sân bay - Trường UV ESLUV ESL Center
 
ちょっと怖くなくったRails
ちょっと怖くなくったRailsちょっと怖くなくったRails
ちょっと怖くなくったRailsYutaka Kinjyo
 
steam - its generation and use - 41st edition
steam - its generation and use - 41st editionsteam - its generation and use - 41st edition
steam - its generation and use - 41st editionCuong Dao
 

Andere mochten auch (20)

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

Ähnlich wie Comp102 lec 6

Repetition Structure
Repetition StructureRepetition Structure
Repetition StructurePRN USM
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingPathomchon Sriwilairit
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.pptMahyuddin8
 
Repetition Structure.pptx
Repetition Structure.pptxRepetition Structure.pptx
Repetition Structure.pptxrhiene05
 
Control statements
Control statementsControl statements
Control statementsraksharao
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerAiman Hud
 
for loop in java
for loop in java for loop in java
for loop in java Majid Ali
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثةجامعة القدس المفتوحة
 
Import java
Import javaImport java
Import javaheni2121
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJava_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJayveeCultivo
 

Ähnlich wie Comp102 lec 6 (20)

Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Java programs
Java programsJava programs
Java programs
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
 
Ch5(loops)
Ch5(loops)Ch5(loops)
Ch5(loops)
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
Repetition Structure.pptx
Repetition Structure.pptxRepetition Structure.pptx
Repetition Structure.pptx
 
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Control statements
Control statementsControl statements
Control statements
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
Data structures
Data structuresData structures
Data structures
 
for loop in java
for loop in java for loop in java
for loop in java
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
 
Import java
Import javaImport java
Import java
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJava_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdf
 
Practice
PracticePractice
Practice
 
system FUNCTION ..pptx
system FUNCTION ..pptxsystem FUNCTION ..pptx
system FUNCTION ..pptx
 

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
  • 3.
  • 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(); }
  • 8.
  • 9.  Often used to implement the fixed repetitions
  • 10.
  • 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
  • 13.
  • 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
  • 17.
  • 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