While Loops, For Loops and Switch Statements - PowerPoint PPT Presentation

About This Presentation
Title:

While Loops, For Loops and Switch Statements

Description:

There are actually several ways to rewrite this more concisely. 11/9/09 ... keep displaying the status, e.g. number of stairs climbed, calories burned, etc. ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 32
Provided by: Cer8
Category:

less

Transcript and Presenter's Notes

Title: While Loops, For Loops and Switch Statements


1
While Loops,For Loops andSwitch Statements
  • Ethan Cerami
  • New York University

2
This Week
  • Tuesday
  • Pre-Post Increment Operators
  • While Loops
  • Amazon.com, Version 3.0
  • Thursday
  • For Loops
  • Switch Statements
  • Logical Operators

3
I. Assignment Statements
  • Given the following
  • x 2
  • x x 1
  • printf (x d, x)
  • There are actually several ways to rewrite this
    more concisely.

4
Short Cut Operator
  • One option is to use the operator
  • x 2
  • x 1
  • printf (x d, x)
  • There are similar operators for , -, /
  • x x 5 is equivalent to x 5
  • x x 5 is equivalent to x - 4
  • x x / 5 is equivalent to x / 5

5
Increment Operator
  • A second option is to use an increment operator
  • x Post-Increment Operator
  • x Pre-Increment Operator
  • Both operators will increment x by 1, but they do
    have subtle differences.

6
Pre v. Post Increment
  • PostIncrement Operator (x)
  • use the current value of x in the expression.
    Then, increment by 1.
  • PreIncrement Operator (x)
  • Increment x by 1. Then, use the new value of x in
    the expression.

7
How about a real example?
  • / Preincrementing v. PostIncrementing /
  • include ltstdio.hgt
  • main ()
  • int c5
  • printf ("d\n", c)
  • printf ("d\n", c)
  • printf ("d\n\n", c)
  • c5
  • printf ("d\n", c)
  • printf ("d\n", c)
  • printf ("d\n", c)
  • getchar()

Post Increment
Output 5 5 6 5 6 6
Pre Increment
8
II. While Loops
  • While Loop Keep repeating an action while some
    condition remains true.
  • Examples
  • Every Stairmaster Machine contains a while loop.
  • while the person is still climbing, keep
    displaying the status, e.g. number of stairs
    climbed, calories burned, etc.
  • Keep prompting for book orders until the user is
    done.

9
Parts of a While Loop
  • Every while loop will always contain three main
    elements
  • Priming initialize your variables.
  • Testing test against some known condition.
  • Updating update the variable that is tested.

10
Simple While Loop
Index 1 Index 2 Index 3 Index 4 Index
5 Index 6 Index 7 Index 8 Index 9 Index
10
  • include ltstdio.hgt
  •  
  • main ()
  • int index 1
  •   while (index lt 10)
  • printf ("Index d\n", index)
  • index

1. Priming
2. Test Condition
3. Update In this case, you can use either the
pre or post increment operator.
11
While Loop Flowchart
1. Priming Set index1
2. Test index lt 10
TRUE
3. Update Index
FALSE
12
Infinite Loop
  • Infinite Loop A loop that never ends.
  • Generally, you want to avoid these!
  • There are special cases, however, when you do
    want to create infinite loops on purpose.
  • Common Exam Questions
  • Given a piece of code, identify the bug in the
    code.
  • You may need to identify infinite loops.

13
Infinite Loop Example 1
  • include ltstdio.hgt
  •  
  • main ()
  • int index 1
  •   while (index lt 10)
  • printf ("Index d\n", index)

Index 1 Index 1 Index 1 Index 1 Index
1 forever
Here, I have deleted part 3 the index
statement.
14
Infinite Loop, Example 2
  • include ltstdio.hgt
  •  
  • main ()
  • int index 1
  •   while (index gt 0)
  • printf ("Index d\n", index)
  • index

Index 1 Index 2 Index 3 Index 4 Index
5 forever
Here, I have changed Part 2 the test condition.
15
Why this is important
  • Or, how I crashed the NYU Computer System

16
Amazon.com, Version 3.0
  • New Features
  • Users can now enter more than one book.
  • Program keeps track of a running total.
  • Uses a while loop to get the work done.

17
Understanding Sentinels
  • Sentinel a special value that indicates the
    end of data entry.
  • For example
  • -1 means end of data.
  • 0 means end of data.
  • END means ends of data
  • Depends on the specific application you are
    building.

18
Version 3.0 Code
1. Priming
  • / Prime the while loop with user data /
  • printf ("Enter the three digit book order number
    (-1 to end) ")
  • scanf ("d", order_number)
  • while (order_number ! -1)
  • printf ("Enter the cover price ")
  • scanf ("f", price)
  • printf ("Enter number of copies you would like
    to order ")
  • scanf ("d", number_books)
  • / Calculate Running Total Sales /
  • sales price number_books
  • printf ("Running Total .2f\n\a", sales)
  • / Prompt user for next book /
  • printf ("Enter the three digit book order number
    (-1 to end) ")
  • scanf ("d", order_number)

2. Test Condition
3. Update
19
Next Homework
  • HW 2 requires that you write several example
    programs that continue to prompt for user data.
  • Use Amazon.com, Version 3.0 as an example.

20
III. Number Ranges
  • Suppose you have the following program
  • main ()
  • / Set x 2,147,483,646 /
  • int x 2147483646
  • printf ("x d\n", x)
  • printf ("x1 d\n", x)
  • printf ("x2 d", x)
  • getchar()

x 2147483646 x1 2147483647 x2 -2147483648
21
Why Number Ranges?
  • When you declare a variable, each variable is
    allocated a specific amount of memory.
  • For example, an integer is allocated four units
    of memory.
  • Because each variable has a finite amount of
    memory it will have a finite number range.

22
Integer Number Ranges
  • Within Borland, integers take up four units of
    memory.
  • Integers therefore have a specific range of
    -2,147,483,648 to 2,147,483,647
  • When you go beyond the upper limit, it wraps
    around to the beginning.
  • Similar to a car odometer that has gone past its
    limit.

23
Platform Issues
  • Number ranges vary from operating system to
    operating system.
  • For example, an integer range on an IBM mainframe
    will be much bigger than a regular PC.
  • This is an important issue as it may prevent your
    program from being portable.

24
Data Type Range Table
  • Available on the course home page.
  • Under the Programs Section
  • int, longs, shorts, floats, double, chars.

25
char variables
  • char variables hold a single character.
  • Characters are stored as ASCII integer values
    (see Appendix D, page 891 in the text book.)
  • Examples
  • C (upper case) is stored as ASCII number 67
  • c (lower case) is stored as ASCII number 99

26
For example
  • include ltstdio.hgt
  • include ltconio.hgt
  • main ()
  • char c
  • printf ("Enter a character ")
  • scanf ("c", c)
  • printf ("You entered c\n", c)
  • printf ("You entered d\n", c)
  • getch()

Enter a character c You entered c You
entered 99
Note the difference in format specifiers.
27
IV. For Loops
  • Indefinite Loop
  • You do not know ahead of time how many times your
    loop will execute.
  • For example, you do not know how many books a
    person might order.
  •  Definite Loop
  • You know exactly how many times you want the loop
    to execute.

28
Basic For Loop Syntax
  • For loops are good for creating definite loops.
  • int counter
  • for (counter 1counter lt 10counter)
  • printf ("d\n", counter)

1. Priming Set the start value.
2. Test Condition Set the stop value.
3. Update Update the interval.
Note that each section is separated by a
semicolon.
29
For Loop Variations
  • Increment may be negative
  • for (i100 igt1 i--)
  • This counts from 100 to 1.
  • Increment may be greater than 1
  • for (i100 igt5 i-5)
  • This counts from 100 to 5 in steps of 5

30
Nested For Loops
  • It is also possible to place
  • a for loop inside another
  • for loop.
  • int rows, columns
  •  
  • for (rows1 rowslt5 rows)
  • for (columns1 columnslt10 columns) printf
    ("")
  • printf ("\n")

Output

Outer Loop
Inner Loop
31
Nested For Loops, Example 2
  • include ltstdio.hgt
  • main ()
  • int rows, columns
  • for (rows1 rowslt5 rows)
  • for (columns1 columnsltrows columns)
  • printf ("")
  • printf ("\n")
  • getchar()

Output
Outer Loop
Inner Loop
Write a Comment
User Comments (0)
About PowerShow.com