Control - PowerPoint PPT Presentation

About This Presentation
Title:

Control

Description:

inc is performed at the end of each iteration. ... THESE ARE NOT JUST CUTE TRICKS THEY ARE VERY POWERFUL AND CAN BE MISUSED. HANDLE WITH CARE! ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 15
Provided by: BillL161
Category:
Tags: control | misused

less

Transcript and Presenter's Notes

Title: Control


1
Control
  • Conditionals...Loops
  • Comments...Constants

2
Conditionals
  • The simple case
  • if(ltboolean_valuegt)
  • single_statement
  • OR
  • if(ltboolean_valuegt)
  • statements

This can be dangerous!!!
3
The Classic Killer Error
  • if(ltsome_booleangt)
  • statement
  • statement

4
Conditionals
  • Adding the else part
  • if(ltboolean conditiongt)
  • statement
  • statement
  • else
  • statement
  • statement

Example if( leapYear ) febDays
29 else febDays 28
5
Conditionals
  • Adding the else part
  • if(ltboolean conditiongt)
  • statement
  • statement
  • else
  • statement
  • statement

Example if( leapYear ) febDays 29 else
febDays 28
6
Loops -- Sentinel
  • Pseudocode
  • total, i isoftype Num
  • total lt- 0
  • i lt- 1
  • loop
  • exitif(i gt 10)
  • total lt- total i
  • i lt- i 1
  • endloop
  • Java
  • int total, i
  • total 0
  • i 0
  • while(i lt 10)
  • total total i
  • i i 1

7
Loops -- Test Last
  • Pseudocode
  • total, i isoftype Num
  • total lt- 0
  • i lt- 1
  • loop
  • total lt- total i
  • i lt- i 1
  • exitif(i gt 10)
  • endloop
  • Java
  • int total, i
  • total 0
  • i 1
  • do
  • total total i
  • i i 1
  • while(i lt 10)

8
for Loops
  • for(ltinitgt ltwhile_booleangt ltincgt)
  • statement
  • ltinitgt is done before entering loop
  • ltwhile_booleangt is checked before entering and
    before each subsequent iteration and must be true
  • ltincgt is performed at the end of each iteration.
  • Note ltinitgt and ltincgt can be multiple statements
    but for now dont do it!

9
Loops -- Convenience!
  • Classic while loop
  • int total 0
  • int i 0
  • while(i lt 10)
  • total total 1
  • i i 1
  • for loop
  • int total 0
  • int i
  • for(i 0 i lt 10 i)
  • total total 1

10
Loops Summarized
  • while(ltbooleangt)
  • statement(s)
  • do
  • statement(s)
  • while(ltbooleangt)
  • for(ltinitgt ltwhile_booleangt ltincgt)
  • statement(s)

11
Shorthand Operators
  • iCounter iCounter 1 OR iCounter
  • iCounter iCounter - 1 OR iCounter--
  • iCounter iCounter 2 OR iCounter 2
  • iCounter iCounter 5 OR iCounter
    5Last two examples its op then equals
    (e.g., 2), not equals then op (e.g., isnt
    2)
  • These work with , -, , / plus others.
  • i // means increment i
  • i 2 // means add 2 to the value of i
  • WARNING THESE ARE NOT JUST CUTE TRICKS THEY ARE
    VERY POWERFUL AND CAN BE MISUSED. HANDLE WITH
    CARE!!!

12
Documentation Comments
  • Two ways to do it
  • // Double slashes comment out everything until
    the
  • // end of the line
  • / Starts a comment which may extend multiple
    lines and eventually be terminated by /

13
Constants
  • Pseudocode
  • ltCONST_IDgt is ltconstant valuegt
  • MIN_PASSING is 60
  • PI is 3.14159
  • Java
  • public final static lttypegt ltIDergt ltvaluegt
  • public final static int MIN_PASSING 60
  • public final static float PI (float) 3.14159
  • Details on why this syntax to come soon...

14
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com