INFSCI 0015 Data Structures Lecture 4: Simple Loops - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 4: Simple Loops

Description:

To make sense an expression in statement should have side effect. Block and sequential execution ... Side effect: same as num = num 1. Value: same as num 1 ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 23
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 4: Simple Loops


1
INFSCI 0015 - Data StructuresLecture 4 Simple
Loops
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Course Logistics
  • We are at a turning point
  • We have got through C basics
  • Are you able to follow?
  • How challenging was the quiz?
  • We are writing first small but meaningful
    programs
  • Can you understand what is going on?
  • This is the last chance to catch up

3
Once more about INFSCI 0015
  • Programming data structures
  • Double load? No, but still not 1/2 1/2!
  • Is this course for me?
  • Students have different experience
  • Good in programming ? lazy course
  • Some programming experience ? OK
  • No programming experience ? work hard

4
What does in mean - work hard?
  • Get an extra book (KR2 is not enough)
  • Attend lectures and browse it again
  • Do your reading
  • Explore all examples - run, modify...
  • Solve all problems
  • Ask questions in the forums
  • Come and talk with us, use office hours
  • Spread the load over time

5
Some tough problems
  • How to start?
  • How to type the program?
  • How to compile and run?
  • How to submit
  • Which files?
  • How to use drop box?
  • How to update the submission?
  • Read the instructions, watch the forums!

6
Expressions again
  • Expression something that has a value
  • Types of expressions we know
  • Literal constants 33 or 3.14
  • Variables count
  • Simple - two operands and operator 3 5
  • Complex (count - (44 - 12) / 7) num
  • Some expressions has side effect
  • x 0 / is an operator! /
  • printf(Hello, World!\n)

7
From expressions to statements
  • Statement expression with a semicolon
  • 33
  • 35
  • x 0
  • x y 0 / x (y 0) /
  • printf(Hello, World!\n)
  • To make sense an expression in statement should
    have side effect

8
Block and sequential execution
  • Block ..
  • A group of statements
  • Statements are sequentially executed
  • Syntactically equivalent to a statement
  • Example
  • a a 1
  • b a 2

9
Block and sequential execution
  • Flowcharts are used to show the control flow
    inside the program
  • Sequential execution inside a block means that
    the control (over the processor) flows downwards
    from statement to next statement

a a 1
b a 2
10
While loop
  • while (expression)
  • loopstatement
  • nextstatement
  • If expression is not 0 (true) - dive into the
    loop
  • If expression is 0 (false) - skip to
    nextstatement
  • I.e, while expression is true, do the loop

11
While loop
  • while (expression)
  • statement1
  • ...
  • statementK
  • nextstatement
  • If expression is not 0 - dive into the loop
  • If expression is 0 - skip to nextstatement
  • I.e, while expression is not 0, do the loop

12
Flowchart of the while loop
Yes
Expression is equal to 0?
No
Statement1
StatementK
Nextstatement
13
Example 4_1 - Counting to Zero
  • / Lecture 4 Example 1 - counting to zero
  • Course 0015
  • Author Peter Brusilovsky /
  • include ltstdio.hgt
  • main()
  • int counter 5 / setting the counter /
  • printf("Start counting...\n")
  • while (counter)
  • printf("d\n" , counter)
  • counter counter - 1
  • printf("Fire!\n")

14
Increment/decrement expressions
  • Post-Increment num
  • Side effect same as num num 1
  • Value same as num
  • Pre-Increment num
  • Side effect same as num num 1
  • Value same as num 1
  • There are also pre and post-decrements
  • count-- or --count

15
Defining symbolic constants
  • define is a preprocessor directive for defining
    symbolic constants
  • Example
  • define commission 3.0
  • commission is a symbolic constant
  • Now every appearance of commission is literally
    replaced by 3.0
  • It is not a variable, it cant be assigned a value

16
Example 4_2 - Counting to Zero
  • / Lecture 4 Example 2 - counting to zero
  • Author Peter Brusilovsky 9/12/00 /
  • include ltstdio.hgt
  • define HOW_MANY 5
  • main()
  • int counter HOW_MANY / setting the counter
    /
  • printf("Start counting...\n")
  • while (counter)
  • printf("d\n" , counter--)
  • printf("Fire!\n")

17
Why to use symbolic constants?
  • Remember example 2.1?
  • / Temperature Converter /
  • include ltstdio.hgt
  • main()
  • printf("100 Fahrenheit 6.2f Celsius\n",
    (5.0/9.0)(100-32))
  • Need to replace 100 to 150 in two places (in real
    programs in dozens places!)

18
Why to use symbolic constants?
  • /
  • New Example 2.1 Temperature Converter
  • Author Peter Brusilovsky
  • Objective use of define and printf
  • /
  • include ltstdio.hgt
  • define FAHR 68.8 / the temperature to be
    converted /
  • main()
  • printf("6.2f Fahrenheit 6.2f Celsius\n",
  • FAHR, (5.0/9.0)(FAHR-32))

19
More About Preprocessor
  • Preprocessor is a separate part of C compiler,
    which differs from the rest of it
  • Makes file insertions
  • include ltstdio.hgt
  • Resolves definitions
  • define NUM 100.0
  • Removes comments
  • / this is simply a comment /

20
Example 2_1
  • /
  • Example 2.1 Temperature Converter
  • Author Peter Brusilovsky
  • Objective use of define and printf
  • /
  • include ltstdio.hgt
  • define FAHR 100.0 / the temperature to be
    converted /
  • main()
  • printf("6.2f Fahrenheit 6.2f Celsius\n",
  • FAHR, (5.0/9.0)(FAHR-32))

21
Example 2.1 after preprocessing
  • ...content of the file stdio.h...
  • main()
  • printf("6.2f Fahrenheit 6.2f Celsius\n",
  • 100.0, (5.0/9.0)(100.0-32))

22
Example 4_3 More Interest
  • main()
  • int years / years the capital stays in bank /
  • float interest_rate / interest rate in
    percents /
  • float capital / capital in dollars /
  • float annual_interest / annual interest in
    dollars /
  • printf("Startup capital (.cc) ")
  • scanf("f",capital)
  • printf("Interest rate in percents (xx.xx) ")
  • scanf("f",interest_rate)
  • printf("How many years? ")
  • scanf("d", years)
  • while (years)
  • annual_interest capital interest_rate /
    100
  • capital capital annual_interest
  • --years
  • printf("New capital 9.2f\n", capital)
Write a Comment
User Comments (0)
About PowerShow.com