Iteration and Simple Menus - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Iteration and Simple Menus

Description:

Output passing things to the outside world ... for( int i = 0; i 10; i ) will repeat 10 times by incrementing i ... increment level ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 20
Provided by: gawainSoc
Category:

less

Transcript and Presenter's Notes

Title: Iteration and Simple Menus


1
Iteration and Simple Menus
  • Deterministic / Non-deterministic loops and
    simple menus

2
Where are we?
  • The 6 Coding Concepts
  • Input getting things into the program
  • Output passing things to the outside world
  • Assignment pass a value from one part of the
    program to another
  • Sequence - do each thing in the order specified
  • Decision-making -check something then do the
    appropriate action depending on that condition
  • Repetition - do the set of commands several times

3
Deterministic loops (for loop)?
  • Use if you know how many times the loop should
    execute (deterministic loop)?
  • int num Console.readInt("How many lines?")
  • for (int i 0 i lt num i)?
  • System.out.println("I must stay awake in
    class")

4
Breakdown of the for Loop
  • for (int i 0 i lt num i)?
  • What does this mean?
  • The for loop consists of 3 items
  • The initialiser (int i 0)?
  • The terminating condition (i lt num)?
  • The incrementor (i)?
  • These items are separated by a
  • Like the if statement, you can omit the that
    follow it if there is only statement to be
    repeated

5
Nested for loops
  • You have seen how if-else statements can be
    nested inside each other
  • if( condition )?
  • if( condition )?
  • // do something
  • else
  • // do something different
  • else

6
Nested for loops
  • Well you can do the same with for loops
  • for( int i 0 i lt 10 i )?
  • System.out.println(Outer loop)
  • for( int j 0 j lt 5 j )?
  • System.out.println(Inner loop)

7
Loop examples
  • for( int i 0 i lt 10 i ) will repeat 10
    times by incrementing i
  • for( int i 0 i lt 10 i ) will repeat 11
    times
  • for( int i 0 i lt 10 i2 ) will repeat 5
    times
  • for( int i 10 i gt 0 i-- ) will repeat 10
    times by decrementing i
  • for() will repeat infinitely!

8
Alternative syntax For-Each
  • There is another way of specifying the Java for
    loop when using arrays and collections.
  • int myArray
  • for(int i myArray)?
  • sum i
  • instead of
  • for(int i i lt myArray.length i)?
  • sum myArrayi

9
Non-deterministic loops (while)?
  • use if you want the loop to execute until a
    condition is false (non-deterministic loop)?
  • must allow tested variable to change within the
    loop, otherwise youll have an infinite loop!
  • int num Console.readInt("How many lines?")
  • while (num gt 0)?
  • System.out.println("I love Java!!!")
  • num--

10
Non-deterministic loops (do-while)?
  • There is a similar while loop called the do-while
    loop
  • int num Console.readInt("How many lines?")
  • do
  • System.out.println("I love Java!!!")
  • num--
  • while (num gt 0)

Note the semi-colon!
11
Non-deterministic loops (do-while)?
  • So whats the difference between while and
    do-while?
  • The contents of a while loop is not executed if
    the test condition fails at the start.
  • The contents of a do-while loop is guaranteed to
    execute at least once.
  • The test condition is only performed after the
    first iteration through the loop.

12
Exiting a loop
  • The normal way to exit a loop is for the
    condition that is tested to become false.
  • This is true of all three types of loops in Java
    for, while, and do-while.
  • However, there might be times when you want a
    loop to end immediately, even if the condition
    being tested is still true.
  • You can do this with a break statement, as shown
    in the following code

13
The break statement
  • int index 0
  • while (index lt 1000)?
  • index index 5
  • if (index 400) break
  • System.out.println("The index is " index)
  • The break statement is especially handy if you
    need to search a list and then exit the search
    loop code if you found what you are looking for

14
Menus
  • Can use a combination of a while loop and a
    switch statement to implement a menu
  • NB may be better to have 0 Exit - why?
  • What would you like to do?
  • 1 Register student
  • 2 Register student on program
  • 3 Exit
  • ?

15
Simple menu display
  • public int showMenu()?
  • int choice
  • System.out.println("\nWhat would you like to
    do?")
  • System.out.println("\t1\tRegister student")
  • System.out.println("\t2\tChange student's
    program")
  • System.out.println("\t3\tExit")
  • choice Console.readInt("? ")
  • return choice

16
Simple menu control
  • int choice showMenu()
  • while (choice ! 3)?
  • switch (choice)?
  • case 1
  • registerStudent()
  • break
  • case 2
  • changeProgram()
  • break
  • default
  • System.out.print(Invalid, please try
    again")
  • choice showMenu()

17
Implementing amenu-based interface
  • Would have to write appropriate methods
  • showMenu()?
  • registerStudent()?
  • changeProgram()?
  • Look at StudentInterface.java listing

18
Summary
  • We have looked at
  • for loops
  • Nested for loops
  • while loops
  • do-while loops
  • break
  • We have put them together into a menu-driven
    interface for the Student class (see tutorial).

19
Further work
  • Practical
  • Have a look at the Student class tutorial sheet
  • Have a look at the Control Flow tutorial sheet
  • Add more functionality
  • print student details
  • increment level
  • Read http//www.faqs.org/docs/javap/c3/s1.html to
    go over all the main blocks of Java code youve
    seen so far.
Write a Comment
User Comments (0)
About PowerShow.com