Title: coit29222
1COIT29222-Structured Programming Lecture Week 02
- Reading Textbook(4th Ed.), Chapter 2
- Textbook (6th Ed.), Chapters 4, 5
- Study Guide Book 1, Module 4
- Variables
- Branching or selection statements
- Looping statements
- Expressions, Operators, Operands, etc
2Variables
- Instead of using memory locations directly, all
modern programming languages use names to refer
to memory locations - We call these things variables the values they
refer to vary as the program runs - Using names, instead of memory locations, makes
programs easier to read - Instead of
3Example Pseudocode
- write Number of marks in exam gt
- read M1
- write Students mark gt
- read M2
- set M3 to M2 / M1
- set M4 to 100 M3
- write Students percentage
- write M4
4Variables
- write Number of marks in exam gt
- read NbrMarks
- write Students mark gt
- read StudentMark
- set ProportionOfMarks to StudentMark / NbrMarks
- set Percentage to 100 ProportionOfMarks
- write Students percentage
- write Percentage
- 4 variables NbrMarks, StudentMark,
ProportionOfMarks, Percentage
5Alternative Design
- write Number of marks in exam gt
- read NbrMarks
- write Students mark gt
- read StudentMark
- set Percentage to 100 StudentMark / NbrMarks
- write Students percentage
- write Percentage
- 3 variables NbrMarks, StudentMark,Percentage
- In C, this looks like
6Sample Program In C
- include ltiostream.hgt
- void main (void)
-
- int NbrMarks 0
- float StudentMark 0,
- Percentage 0
- cout ltlt "Number of marks in exam gt "
- cin gtgt NbrMarks
- cout ltlt "Students mark gt "
- cin gtgt StudentMark
- Percentage 100 StudentMark / NbrMarks
- cout ltlt " Students percentage "
- cout ltlt Percentage
7Declaring Variables
- In C, you must declare variables before you
use them - Also, you must declare the type of value each
variable will hold - The first variable declared is NbrMarks it can
hold any integer value - int NbrMarks 0
- An integer is any ve or ve whole number, or
zero -3, -2, -1, 0, 1, 2, 3,
8Naming Variables
- Start with a letter - A to Z
- Can contain upper or lower case letters, or
numerals - 0 to 9 - Good idea to limit names to 32 characters
- Must avoid C reserved words like int, float,
void, etc - More later
9Initialising Variables
- Our declaration of NbrMarks also gives it an
initial value - 0 - int NbrMarks 0
- If you do not give a variable an initial value,
it will hold whatever data was left in memory by
the previous program - Incorrect initialisation of variables is a very
common logic error made by programmers - Please read the previous point again!
10More Declarations
- Our program also declares two floating point
variables StudentMark and Percentage - float StudentMark 0,
- Percentage 0
- We use variables of type float to hold values
that may not be an integer - Examples
- An students mark 15.5 (marks)
- A distance 123.456 (meters)
- A temperature -2.3 (degrees C)
11Why Floating Point
- Some programs need to store very big numbers
eg. 1,234,500,000,000,000,000 others need to
store very small numbers - 0.000000000012345 - These two numbers can be represented as
- 1.2345 1018 and 1.2345 10-12
- float variables are stored in two parts
- number part 1.2345 in above cases
- power part 18 or 12 in above cases
- Decimal point floats around, depending on power
part more details later
12More On Declarations
- We could declare these variables like this
- float StudentMark 0
- float Percentage 0
- Instead, a comma was used to separate two
variables of the same type - float StudentMark 0, Percentage 0
- Breaking the declaration over two lines is
optional in C - its common practice - float StudentMark 0,
- Percentage 0
- Notice how a comma is used here to separate two
use of punctuation - , and
13Branching
- Our example program always performs the exact
same actions - get number of marks in exam
- get students mark
- calculate the students percentage
- display the students percentage
- In practice, most programs are capable of
performing different actions, depending on the
inputs provided by the user - e.g., we may want to also state if the student
passed the exam so, sometimes the program will
need to output Pass, otherwise Fail
14Sample Dialogs
- Number of marks in exam gt 80
- Students mark gt 60
- Students percentage 75 (Pass)
- or
- Number of marks in exam gt 80
- Students mark gt 20
- Students percentage 25 (Fail)
15Branching Statements
- To enable programs to perform different actions
(depending on user input) all programming
languages include one or more branching or
selection statements - The branching statement selects between one of
more possible paths through the program - All programming languages include some form of
if-then-else statement
16Pseudocode Example
- write Number of marks in exam gt
- read NbrMarks
- write Students mark gt
- read StudentMark
- set Percentage to 100 StudentMark / NbrMarks
- write Students percentage
- write Percentage
- if Percentage lt 50 then
- write (Fail)
- else
- write (Pass)
17Example of if-then-else
- write Number of marks in exam gt
- read NbrMarks
- write Students mark gt
- read StudentMark
- set Percentage to 100 StudentMark / NbrMarks
- write Students percentage
- write Percentage
- if Percentage lt 50 then
- write (Fail)
NbrMarks ? - else
StudentMark ? - write (Pass)
Percentage ? - in C, this looks like
18if-else Statements
- include ltiostream.hgt
- void main (void)
-
- float StudentMark 0
- cout ltlt "Students mark gt "
- cin gtgt StudentMark
- if (StudentMark gt 50)
-
- cout ltlt "Pass!"
-
- else
-
- cout ltlt "Fail!"
-
19if-else Statements
- Notice that comparison between StudentMark and 50
is enclosed in parentheses - if (StudentMark gt 50)
- Also, braces mark start and end of the if-branch
(shown below), and the else-branch -
- cout ltlt "Pass!"
-
- Indentation makes code easier to read its not
required by the compiler
20if-else-if Statements
- if-else-if statements can be coded as
- if (StudentMark gt 75)
-
- cout ltlt Distinction!"
-
- else if (StudentMark gt 50)
-
- cout ltlt Pass!"
-
- else
-
- cout ltlt "Fail!"
-
21Activity Code in C
- write Select conversion - (1) C to F, (2) F to C
gt - read ConversionType
- write Input temperature gt
- read Temperature
- write Converts to
- if ConversionType 1 then
- write 32 (Temperature 1.8)
- write degrees Fahrenheit
- else
- write (Temperature 32) / 1.8
- write degrees Centigrade
22A Start
- include ltiostream.hgt
- void main (void)
-
- int ConversionType 0
- float Temperature 0
- cout ltlt "Select conversion - (1) C to F, (2) F
to C gt " - cin gtgt ConversionType
23A Solution
- include ltiostream.hgt
- void main (void)
-
- int ConversionType 0
- float Temperature 0
- cout ltlt "Select conversion - (1) C to F, (2) F
to C gt " - cin gtgt ConversionType
- cout ltlt "Input temperature gt "
- cin gtgt Temperature
- if (ConversionType 1)
-
- cout ltlt 32 (Temperature 1.8)
- cout ltlt " degrees Fahrenheit"
-
- else
-
- cout ltlt (Temperature - 32) / 1.8
- cout ltlt " degrees Centigrade"
-
note C uses instead of for testing
equality
24Looping
- Our program to calculate the exam percentage for
a single student performs the following 5 tasks - get number of marks in exam
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
- To calculate the exam percentage for more than
one student, a program must perform some of these
tasks more than once
25Looping
- To calculate the percentage for 2 students, a
program must perform 9 tasks - get number of marks in exam
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
26Looping
- To calculate the percentage for 3 students, a
program must perform 13 tasks - get number of marks in exam
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
27Looping
- We could have
- -One program to handle 2 students,
- -Another program to handle 3 students, etc
- It would be better to have a single program to
handle any number of students - We would like to design a program to repeat some
actions more than once - We want our program to
- get the number of marks in exam from user
- - repeat as required -
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
28Looping
- One design is
- Ask user for number of students at start
- get number of marks in exam
- get number of students
- - repeat once for each student -
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
- Another design is
29Looping
- Keep asking if the user has more students
- get number of marks in exam
- - repeat while user wants more students -
- get students mark
- calculate the students percentage
- display the students percentage
- display Pass or Fail
- ask user if they have more students
30Looping Statements
- To enable programs to perform different numbers
of actions (depending on data inputs) all
programming languages include one or more looping
or repetition statements - All programming languages include some form of
while statement... - Other common looping statements are do-while and
for
31Pseudocode Example
- write Number of marks in exam gt
- read NbrMarks
- write Number of students gt
- read NbrStudents
- set NbrProcessed to 0
- while NbrProcessed lt NbrStudents
- write Students mark gt
- read StudentMark
- set Percentage to 100 StudentMark /
NbrMarks - write Students percentage
- write Percentage
- set NbrProcessed to NbrProcessed 1
32Example of while
write Number of marks in exam gt
NbrMarks 80 read NbrMarks
NbrStudents 2 write Number of
students gt NbrProcessed 2 read
NbrStudents
StudentMark 40 set NbrProcessed to 0
Percentage 50 while NbrProcessed
lt NbrStudents write Students mark gt
read StudentMark set Percentage to 100
StudentMark / NbrMarks write Students
percentage write Percentage set
NbrProcessed to NbrProcessed 1
33Activity
Show the output produced by the following
pseudocode. set TotalCost to 20 set ItemCost to
5 set NumberOfItems to 0 while TotalCost gt 0
set TotalCost to TotalCost - ItemCost set
NumberOfItems to NumberOfItems 1 write Number
of items purchased write NumberOfItems
34while Statement
- include ltiostream.hgt
- void main (void)
-
- int NbrTimesTold 0,
- NbrTimesToTell 0
- cout ltlt "How many times must I tell you? gt "
- cin gtgt NbrTimesToTell
- while (NbrTimesTold lt NbrTimesToTell)
-
- cout ltlt No new taxes!"
- cout ltlt endl
- NbrTimesTold NbrTimesTold 1
-
35Activity Feedback
- The program displays No new taxes! the number
of times requested by the user - cout ltlt endl moves to start of next line
How many times must I tell you? gt 3 No
new taxes! No new taxes! No new taxes!
36while Statement
- Things to notice
- Comparison enclosed in parentheses
- while (NbrTimesTold lt NbrTimesToTell)
-
- cout ltlt endl
- cout ltlt " No new taxes!"
- NbrTimesTold NbrTimesTold 1
-
- Use of braces and indentation
37One More Detail About C
- All compilers are fussy about punctuation
- C compilers are also fussy about case
- The following code has 3 compilation errors
- Include ltiostream.hgt
- void Main (void)
-
- Cout ltlt "Hello World!"
-
38C is Case-Sensitive
- Include ltiostream.hgt
- void Main (void)
-
- Cout ltlt "Hello World!"
-
- This program has three errors
- it should be include , not Include
- it should be main , not Main
- it should be cout , not Cout
- C compilers are case-sensitive
39Data Processing
- We said that computers are data processing
devices they convert input data into output
data - Where does all the data processing occur in a
program? - Recall our sample program...
40Sample Program
- write Number of marks
- read NbrMarks
- write Students mark
- read StudentMark
- set ProportionOfMarks to StudentMark / NbrMarks
- set PercentageOfMarks to ProportionOfMarks 100
- write Students percentage
- write PercentageOfMarks
- In this program, all the data processing occurs
in assignment statements
41Anatomy of an Assignment
- Lets analyse the assignment statement
- Here, the two assignment statements are
- set ProportionOfMarks to StudentMark / NbrMarks
- set PercentageOfMarks to ProportionOfMarks
100 - That is
- set lt variable gt to lt value of interest gt
- Here, the values of interest are
- StudentMark / NbrMarks
- ProportionOfMarks 100
42Expressions
- We use the term expression to mean
- the description of a value of interest
- we describe the value that we wish to assign to a
data object in an expression - so
- StudentMark / NbrMarks
- ProportionOfMarks 100
- are two expressions
43Data Processing
- So, where does the data processing happen?
- answer some of it happens in
- assignment statements
- It can also happen in output statements
44Alternative Design
- write Number of marks
- read NbrMarks
- write Students mark
- read StudentMark
- set ProportionOfMarks to StudentMark / NbrMarks
- write Percentage
- write ProportionOfMarks 100
45Anatomy of an Output
- The anatomy of our assignment statement is
- set lt variable gt to lt expression gt
- the anatomy of our output statement is
- write lt expression gt
- so, where does all the data processing happen?
-
Expressions !
46Expressions
- Clearly, expressions are important - thats
where the data processing happens - Lets take a closer look at expressions
- Previously, we said that data was numbers and
text -for now, we just deal with expressions to
process numbers - The anatomy of an expression is one weve seen
before...
47Expressions as a Black Box
- We can think of an expression as a black box
- Expressions have one or more input values and
produce one output value - the input-process-outpu
t model again - Example StudentMark / NbrMarks
- input process
output - StudentMark
? - NbrMarks
(a single value - -
depends on inputs)
48Operators
- we use the term operator to mean
- a symbol, or name, used to represent an
operation that can be performed on data - in the two expressions
- StudentMark / NbrMarks
- ProportionOfMarks 100
- the operators are
- / for division
- for multiplication
- and - are used for addition and subtraction
- , -, , / all work in C as you would expect
49Operands
- We use the term operand to mean
- an input to an expression
- In the two expressions
- StudentMark / NbrMarks
- ProportionOfMarks 100
- The operands are
- StudentMark and NbrMarks
- ProportionOfMarks and 100
50Binary Operators
- In the following examples
- StudentMark / NbrMarks
- ProportionOfMarks 100
- NbrMarks - StudentMark
- StudentMark 10
- Each operator is used with two operands
- So / , , - and are binary operators
they can all be used with two operands
51Unary Operators
- The and - operators are also unary
operators (they can be used with just one
operand) - Examples
- -273.15 as in set AbsoluteZero to
-273.15 - 100 as in set BoilingPointOfWater
to 100 -
- expression
- - 273.15
- operand
- operator
52Numeric Expressions
- expressions that evaluate to a number are called
numeric expressions - numeric expression come in all shapes and sizes
- a number by itself a literal
- set NbrTimesTold to 0
- the name of a variable
- write Percentage
- expressions that use operators
- set NbrTimesTold to NbrTimesTold 1
53Power of Expressions
- The arithmetic operators , -, and / give us
a powerful language to process numbers - The power comes from the ability to nest little
expressions inside bigger expressions - Instead of
- set ProportionOfMarks to StudentMark / NbrMarks
- write ProportionOfMarks 100
- We can write
- write StudentMark / NbrMarks 100
- question which operator is applied first here?
and, does it matter?
54Nested Expressions
- which operator is applied first here?
- Is the division first?
- StudentMark / NbrMarks 100
- divide StudentMark by NbrMarks, then multiply by
100 - Or is the multiplication first?
- StudentMark / NbrMarks 100
- multiply NbrMarks by 100, then divide StudentMark
by result of multiplication - Activity does it matter?
55Activity Feedback
- Using StudentMark 50, NbrMarks 100
- Division first
- (StudentMark / NbrMarks) 100
- (50 / 100) 100
- 50
- Multiplication first
- StudentMark / (NbrMarks 100)
- 50 / (100 100)
- 0.005
- Will a C program do it in the correct order?
56Order of Use
- There are rules to decide the order in which
operators in an expression are applied - Unary operators before binary operators
- Multiplication () and division (/) before
addition () and subtraction (-) - Otherwise, left to right
- Evaluate the following
- 4 -2 3
- 2 12 / 4 3
- Will the following be evaluated correctly?
- StudentMark / NbrMarks 100
57Activity Feedback
- Evaluate
- 4 -2 3
- unary operator first (- applies to 2)
- multiplication before addition
- (4 -2) 3
- -8 3
- -5
58Activity Feedback
- Evaluate the following
- 2 12 / 4 3
- Multiplication and division before addition
- Left to right otherwise so division before
multiplication here - 2 (12 / 4) 3
- 2 3 3
- Multiplication before addition
- 2 (3 3)
- 2 9
- 11
59Activity Feedback
- Will the following be evaluated correctly?
- StudentMark / NbrMarks 100
- Yes it will since the division occurs before
the multiplication, this is the same as - (StudentMark / NbrMarks) 100
60Order of Use
- Avoid errors by using parentheses
- (4 -2) 3
- 2 ( ( 12 / 4 ) 3 )
- Sometimes you can rewrite an expression to make
it easier to read instead of - StudentMark / NbrMarks 100
- You can write
- 100 StudentMark / NbrMarks
- Is this easier to understand? if so, why?
61Activity Feedback
- The expression
- 100 StudentMark / NbrMarks
- may seem easier to read than
- StudentMark / NbrMarks 100
- Possibly because, in the first expression above,
the order in which operators are applied doesnt
matter - left for student to check
- Always keep you code as simple as possible
- The following program is designed to convert
temperatures between Fahrenheit and Centigrade - It has a logic error fix it
62- include ltiostream.hgt
- void main (void)
-
- int ConversionType 0
- float Temperature 0
- cout ltlt "Select conversion - (1) C to F, (2) F
to C gt " - cin gtgt ConversionType
- cout ltlt "Input temperature gt "
- cin gtgt Temperature
- if (ConversionType 1)
-
- cout ltlt 32 Temperature 1.8
- cout ltlt " degrees Fahrenheit"
-
- else
-
- cout ltlt Temperature - 32 / 1.8
- cout ltlt " degrees Centigrade"
-
63Feedback
- include ltiostream.hgt
- void main (void)
-
- int ConversionType 0
- float Temperature 0
- cout ltlt "Select conversion - (1) C to F, (2) F
to C gt " - cin gtgt ConversionType
- cout ltlt "Input temperature gt "
- cin gtgt Temperature
- if (ConversionType 1)
-
- cout ltlt 32 Temperature 1.8
- cout ltlt " degrees Fahrenheit"
-
- else
-
- cout ltlt Temperature - 32 / 1.8
- cout ltlt " degrees Centigrade"
-
problem here division occurs before subtraction
64Feedback
- include ltiostream.hgt
- void main (void)
-
- int ConversionType 0
- float Temperature 0
- cout ltlt "Select conversion - (1) C to F, (2) F
to C gt " - cin gtgt ConversionType
- cout ltlt "Input temperature gt "
- cin gtgt Temperature
- if (ConversionType 1)
-
- cout ltlt 32 (Temperature 1.8)
- cout ltlt " degrees Fahrenheit"
-
- else
-
- cout ltlt (Temperature 32) / 1.8
- cout ltlt " degrees Centigrade"
-
clarification parentheses make intention clear
a solution enclose subtraction in parentheses
65C Syntax Summary
- input cin gtgt ltvariablegt
- output cout ltlt ltexpressiongt
- assignment ltvariablegt ltexpressiongt
- A selection statement
- if ( lttestgt )
- ltif-block statementsgt
- else
- ltelse-block statementsgt
- A repetition statement
- while ( lttestgt )
- ltwhile-block statementsgt
66Summary
- Five base programming statements are input,
output, assignment, selection, repetition - These statements provide a platform for
programming in all popular languages - Data processing happens in expressions
- Different types of expressions literals,
variables names, ones that use operators - Arithmetic operators are , -, , /
- Rules control order of application
- Parentheses are used to impose ordering