Title: Introduction to Programming with Java, for Beginners
1Introduction to Programmingwith Java, for
Beginners
2Sequential Control Flow
- Consists of just a list of commands to be done in
order - Welcome to Dr. Java
- int x 2
- int squareOfX x x
- System.out.println(x) //Or just type
squareOfX - 2
3What are control Structures ?
- You cant do very much if your program consists
of sequential control flow - The program cannot choose whether or not to
perform a command - The program cannot perform the same command more
than once - Such programs are extremely limited!
- Control structures allow a program to base its
behavior on the values of variables
4Recap Boolean
- Boolean is one of the eight primitive types
- Booleans are used to make yes or no decisions
- All control structures use Booleans
- There are exactly two Boolean values, true
(yes) and false (no) - Boolean, true, and false are all lowercase
- Boolean variables are declared like any other
kind of variable - Boolean hungry
- boolean passingGrade
- boolean taskCompleted false
5Numeric Boolean Comparisons
- The following comparisons each give a Boolean
result - (25 24) (12 13) //results to false
-
- (25 24) (12 13) //results to true
-
6Conditionals (if statements)
- An if statement is a flow control statement
- It is also called a conditional, or a branch
- Well see several flavors
- An if all by itself
- An if with an else part
- An if with an else if part
7if statement
if (condition) statement(s)
- If the condition is true, then the statement(s)
(i.e. instructions) will be executed. Otherwise,
it/they wont.
8if-else statement
9Cascading if-else
10Nested if-statements
11The infamous dangling else
- An else is paired with the last else-less if,
regardless of spacing, unless dictate
otherwise.
12A 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
13Syntax of the while statement
while (condition) statement(s)
- condition is a true/false (boolean) expression
- If condition is initially false, the statement is
never executed - If condition is true, statement is executed and
condition is re-evaluated - The statement should eventually make the loop stop
14A while Loop to Print Numbers
- // Print the numbers 1 thru 10
- int x 1
- while (x
- System.out.println(x)
- x x 1
-
-
15An Infinite Loop
- // Faulty attempt to print 1 thru 10
- int x 1
- while (x
- System.out.println(x)
-
-
16More Infinite Loops
- // Some infinte loops are intentional
- while (true)
- statement(s)
-
- // Others are not
- int x 5
- while (x
- statement(s) which dont change x
-
17Pseudocode 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
- update game state
- determine if game should end
-
- if game should be saved
- save game
- quit
18Compute Square of first 10 numbers
- //In Square.java
- int number 1
- int squareOfNumber 0
-
- while (number
- squareOfNumber number number
- System.out.println(number " "
squareOfNumber) - number number 1
-
19For Loop
- for (init end-test re-init) statement
-
- Executes loop body as long as end-test evaluates
to TRUE - Initialization and re-initialization code
included in loop statement - Note Test is evaluated before executing loop body
20While vs. For
Note For loops are used generally for bounded
iteration
21Summary of Loops
22Indentation
- if(x
- else___xx-1
- while(x0)___System.out.println(x)___xx
-1
- Recommended indentation is from 2 to 4 spaces,
but must be consistent throughout the program - Most important style rule If you are modifying
or adding to an existing program, keep to the
original style (whatever it may be)