Title: INFSCI 0015 Data Structures Lecture 4: Simple Loops
1INFSCI 0015 - Data StructuresLecture 4 Simple
Loops
- Peter Brusilovsky
- http//www2.sis.pitt.edu/peterb/0015-011/
2Course 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
3Once 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
4What 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
5Some 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!
6Expressions 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)
7From 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
8Block and sequential execution
- Block ..
- A group of statements
- Statements are sequentially executed
- Syntactically equivalent to a statement
- Example
-
- a a 1
- b a 2
-
9Block 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
10While 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
11While 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
12Flowchart of the while loop
Yes
Expression is equal to 0?
No
Statement1
StatementK
Nextstatement
13Example 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")
14Increment/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
15Defining 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
16Example 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")
17Why 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!)
18Why 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))
19More 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 /
20Example 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))
-
21Example 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))
-
22Example 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)