Intro to Java - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Intro to Java

Description:

Deal card hands until game over. Java has 3 loop statements: while, do, for. 2 ... initialize game. while( the game isn't over){ process player input. process bots ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 9
Provided by: fernando8
Category:
Tags: game | intro | java | over

less

Transcript and Presenter's Notes

Title: Intro to Java


1
Intro to Java
  • while loops
  • pseudocode

2
A Loop
  • A simple but powerful mechanism for making lots
    of things happen!
  • Performs a statement (or block) over over
  • Usually set up to repeat an action until some
    condition is satisfied, e.g.
  • Run an application until user hits quit button
  • Read characters until end of file reached
  • Deal card hands until game over
  • Java has 3 loop statements while, do, for

3
Syntax of the while statement
while (condition) statement
  • condition is a true/false (boolean) expression
  • statement is a single or block statement
  • If condition is initially false, the statement is
    never executed
  • If the condition is true, the statement is
    executed. Repeat this step.
  • The statement should eventually make the loop
    stop (unless you want to have an infinite loop)

4
A while Loop to Print Numbers
  • // Print the numbers 1 thru 10
  • int x 1
  • while (x lt 10)
  • System.out.println(x)
  • x x 1

5
An Infinite Loop
  • // Faulty attempt to print 1 thru 10
  • int x 1
  • while (x lt 10)
  • System.out.println(x)

6
More Infinite Loops
  • // Some infinte loops are intentional
  • while (true)
  • statement(s)
  • // Others are not
  • int x 5
  • while (x lt 10)
  • statement(s) which dont change x

7
Pseudocode and Loops
Pseudocode is an informal, english-like,
code-like way Of sketching out what you want your
code to do.
  • initialize game
  • while( the game isnt over)
  • process player input
  • process bots
  • update game state
  • determine if game should end
  • if game should be saved
  • save game
  • quit

8
A while Loop for Powers of 2
  • // Computes 2 pow. Assumes pow gt 0.
  • int result 1
  • while (pow gt 0)
  • result result 2
  • pow pow - 1
  • System.out.println(result is result)
Write a Comment
User Comments (0)
About PowerShow.com