Structured Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Structured Programming

Description:

Structured Programming is writing a program in terms of only 3 basic control structures: ... We have already seen two of these. Lots of Choices. import cs1. ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 12
Provided by: rebecc2
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Structured Programming


1
Structured Programming
  • Structured Programming is writing a program in
    terms of only 3 basic control structures
  • sequence
  • selection
  • repetition
  • We have already seen two of these.

2
Lots of Choices
  • import cs1.Keyboard
  • public class Testing
  • public static void main( String args)
  • System.out.println("Enter an integer between 1
    and 5")
  • int num Keyboard.readInt()
  • if (num 1)
  • System.out.println("It's a one")
  • else if (num 2)
  • System.out.println("It's a two")
  • else if (num 3)
  • System.out.println("It's a three")
  • else if (num 4)
  • System.out.println("It's a four")
  • else if (num 5)
  • System.out.println("It's a five")
  • else
  • System.out.println("Number not in range")

3
The Switch Statement
  • switch(num)
  • case 1
  • System.out.println("It's a one")
  • break
  • case 2
  • System.out.println("It's a two")
  • break
  • case 3
  • System.out.println("It's a three")
  • break
  • case 4
  • System.out.println("It's a four")
  • break
  • case 5
  • System.out.println("It's a five")
  • break
  • default
  • System.out.println("Number not in range")

4
Repetition---the while loop
  • public static void main( String args)
  • System.out.println("Enter an integer number
    between 1 and 5")
  • int num Keyboard.readInt()
  • while(num lt 1 num gt 5)
  • System.out.println("Number not in range---try
    again")
  • num Keyboard.readInt()
  • System.out.print("\nThanks, the number ")
  • if (num 1)
  • System.out.print("one")
  • else if (num 2)
  • System.out.print("two")
  • else if (num 3)
  • System.out.print("three")
  • else if (num 4)
  • System.out.print("four")
  • else
  • System.out.print("five")
  • System.out.println(" is a great choice")

5
The while Loop
  • The while loop in Java is a pre-test loop. The
    test of the condition is done at the beginning of
    the loop.
  • The loop continues as long as the condition is
    true. As soon as the condition becomes false,
    the loop stops and the next statement is
    executed.
  • If the condition is false the first time it is
    tested, the loop is never executed at all.

6
Infinite Loops
  • The loop control variable needs to be initialized
    at the beginning of the loop, and tested each
    time the loop is executed.
  • Somewhere in the loop there must be something
    that can change the value of the loop control
    variable.
  • If there is not, we have an infinite loop which
    will continue going round and round until you
    stop it from outside.
  • If this happens in BlueJ, close the BlueJ window
    and start it up again. Then go to the editor and
    fix the problem.

7
Logical Operators
  • Boolean expressions can also use the following
    logical operators
  • ! Logical NOT
  • Logical AND
  • Logical OR
  • They all take boolean operands and produce
    boolean results
  • Logical NOT is a unary operator (it has one
    operand), but logical AND and logical OR are
    binary operators (they each have two operands)

8
Getting a Menu Choice
  • public int getMenuChoice()
  • int choice 0
  • while (choice lt 1 choice gt 4)
  • System.out.println("\n\nChoose one from the
    menu below")
  • System.out.println("1. Yes")
  • System.out.println("2. No")
  • System.out.println("3. Maybe")
  • System.out.println("4. Exit")
  • int choice Keyboard.readInt()
  • if (choice lt 1 choice gt 4)
  • System.out.println("Choose a number
    between 1 and 4")
  • return choice

9
Increment and Decrement Operators
  • The increment and decrement operators are
    arithmetic and operate on one operand
  • The increment operator () adds one to its
    operand
  • The decrement operator (--) subtracts one from
    its operand
  • The statement
  • count
  • is essentially equivalent to
  • count count 1

10
Increment and Decrement Operators
  • When used in a larger expression, the prefix and
    postfix forms have a different effect
  • In both cases the variable is incremented
    (decremented)
  • But the value used in the larger expression
    depends on the form

11
Assignment Operators
  • There are many assignment operators, including
    the following
Write a Comment
User Comments (0)
About PowerShow.com