Upload
beverley-simpson
View
244
Download
0
Embed Size (px)
DESCRIPTION
Fundamental Programming Review of Expressions data processing occurs in expressions which appear in assignment and output statements expressions exhibit the input-process-output model: one or more inputs, one output operators transform input value(s) into an output value - numeric operators: /, *, -, + rules control the order in which operators are applied – e.g. multiplication & division before addition and subtraction
Citation preview
Fundamental Programming 310201 1
Fundamental Programming
More Expressions and Data Types
Fundamental Programming 310201 2
Status this week we continue to:
develop familiarity with the C++ language develop skill in software development (in lab) introduce fundamental programming concepts
last class we started to look at expressions they are important – they process the data we looked at numeric expressions and operators
today we look at another type of expression and learn a bit more about data types
Fundamental Programming 310201 3
Review of Expressions data processing occurs in expressions which
appear in assignment and output statements expressions exhibit the input-process-output
model: one or more inputs, one output operators transform input value(s) into an
output value - numeric operators: /, * , - , + rules control the order in which operators
are applied – e.g. multiplication & division before addition and subtraction
Fundamental Programming 310201 4
Pseudocode Examplewrite “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “read StudentMarkset Percentage to 100 * StudentMark / NbrMarkswrite “ Student’s percentage: “write Percentageif Percentage < 50 then write “ (Fail)”else write “ (Pass)”
numeric expression
operators
is this an operator?
is this an expression?
Fundamental Programming 310201 5
Truth-Valued Expressions yes, < is an operator - it’s a binary
operator< operand > < < operand >
and, yes, Percentage < 50 is an expression so, what is the value of this expression
- what does the expression evaluate to ? Answer: True or False (depending on value of Percentage)
Fundamental Programming 310201 6
Conditions in programming, truth-valued expressions
appear as the tests used to control selection (if-then-else) and repetition (while) statements
these tests are usually called conditions
Fundamental Programming 310201 7
Relational Operators less-than operator “<“ is one of a number of
operators we can use to compare two values other operators are: <=, =, >=, >, they are called relational operators in C++, not-equal is “!=“ also in C++, equality is “==“
if (ConversionType == 1)...
to distinguish from assignment operator ConversionType = 1;
Fundamental Programming 310201 8
Relational Operators in C++
a very common coding error is: if (ConversionType = 1)...
this code would not give a compilation error trap for young players - beware! more on this later…
no surprises with other relational operators in C++: <, <=, >=, >
Fundamental Programming 310201 9
More on Conditions consider the following logic…
if Percentage >= 65 and Percentage < 75 then write “ (Credit)”
is Percentage >= 65 and Percentage < 75 an expression?
is “and” an operator?
Fundamental Programming 310201 10
Boolean Operators yes, “and” is an operator - a binary operator and, yes, Percentage >= 65 and Percentage < 75 is an
expression - a truth-valued expression “or” is also a binary operator
if Percentage < 0 or Percentage > 100 then write “ Invalid Percentage!” “not” is a unary operator
if not Percentage >= 50 then write “ (Fail)” “and”, “or”, “not” called Boolean operators
- after Boole
Fundamental Programming 310201 11
Order of Use - General relational operators before Boolean operators
Percentage >= 65 and Percentage < 75 unary operator (not) before binary operators
(and & or) so, not Percentage >= 50 and Percentage > 0
is the same as: (not Percentage >= 50) and Percentage > 0
which is different from: not (Percentage >= 50 and Percentage > 0)
Fundamental Programming 310201 12
Order of Use – C++ in C++, truth-valued expressions must be enclosed in
parentheses the following gives a compilation error
if Percentage >= 50…
it must be:if (Percentage >= 50)…
instead of: if not Percentage >= 50 and Percentage > 0…
it’s… if ((not Percentage >= 50) and (Percentage > 0))…
Fundamental Programming 310201 13
Boolean Operators in C++ ANSI standard for C++ allows use of “and”, “or”, and
“not” in expressions in older compilers (like Turbo 4.5) use:
and: && or: || not: !
instead of: if ((not Percentage >= 50) and (Percentage > 0))…
it’s… if ((! Percentage >= 50) && (Percentage > 0))…
Fundamental Programming 310201 14
ActivityConsider the following expression: not Num1 Num2 or 4 / Num1 + Num2 > 5 The table below list combinations of values forNum1 and Num2. For each combination of values, show the value (True or False) of the expression.
Num1
Num2 not Num1 Num2 or 4 / Num1 + Num2 > 5
1 22 3
Fundamental Programming 310201 15
Activity Break
Fundamental Programming 310201 16
Activity FeedbackAlways clarify and simplify (if only in your mind): not Num1 Num2 or 4 / Num1 + Num2 > 5 (not Num1 Num2) or ((4 / Num1) + Num2 > 5) (Num1 = Num2) or ((4 / Num1) + Num2 > 5)
More activities like this in the Study Guide…
Num1
Num2 not Num1 Num2 or 4 / Num1 + Num2 > 5
1 2 True2 3 False
Fundamental Programming 310201 17
The Character Data Type in previous example programs, all values
assigned to variables have been numbers sometimes we wish to assign other types of
values to variables one other type of value we can assign to a
variable is a character – “a”, “b”, “c”, etc
an example to show how a character variable might be used is…
Fundamental Programming 310201 18
Character Variableswrite “Number of marks in exam ==> “ read NbrMarks set MoreMarks to “Y”while MoreMarks = “Y”
write “Student’s mark: “read StudentMark
set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage
write “ Another mark ? [Y/N] ==> “read MoreMarks
Fundamental Programming 310201 19
in C++#include <iostream.h>void main (void){ int NbrMarks = 0; float StudentMark = 0, Percentage = 0;
char MoreMarks = ‘Y’; cout << "Number of marks in exam ==> "; cin >> NbrMarks;
while (MoreMarks == ‘Y’) { cout << "Student’s mark ==> "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; cout << endl;
cout << “ Another mark ? [Y/N] ==> "; cin >> MoreMarks; }}
note: characters are enclosed in single quotes
Fundamental Programming 310201 20
The Boolean Data Type one other type of value we can assign to a
variable is a truth value – True or False how are True and False stored in a computer?
all data is stored as a string of 1s and 0s in C++, True is stored as 1, False as 0 try:
cout << (2 > 1);cout << (2 < 1);
an example to show how a Boolean variable might be used is…
Fundamental Programming 310201 21
Boolean Variableswrite “Number of marks in exam ==> “ read NbrMarks set MoreMarks to Truewhile MoreMarks
write “Student’s mark: “read StudentMark
set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage
write “ Another mark ? [Y/N] ==> “read Replyif Reply = “N” then set MoreMarks to False
note: you may prefer to use while MoreMarks = True…
Fundamental Programming 310201 22
ANSI Standard C++#include <iostream.h>void main (void){
int NbrMarks = 0; float StudentMark = 0, Percentage = 0;
char Reply = ‘Y’; bool MoreMarks = true; cout << "Number of marks in exam ==> "; cin >> NbrMarks; while (MoreMarks) { cout << "Student’s mark ==> "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; cout << endl; cout << “ Another mark ? [Y/N] ==> "; cin >> Reply; if (Reply == ‘N’) { MoreMarks = false; } }}
note: bool data type notsupported by old compilers
note: you may preferwhile (MoreMarks == true)…
Fundamental Programming 310201 23
Old C++ Compilers replace… :
int NbrMarks = 0; float StudentMark = 0, Percentage = 0;
char Reply = ‘Y’; bool MoreMarks = true; :
with… :
const true = 1, false = 0; int NbrMarks = 0; float StudentMark = 0,
Percentage = 0; char Reply = ‘Y’; int MoreMarks = true; :
note: constantsare similar to variables, but their value isfixed – not variable
note: use int instead of bool
Fundamental Programming 310201 24
Strings strings of characters are also important in
programming – e.g. names, student IDs, etc we will deal with strings in more detail later for now, the only place we will use a string is
when we output a message to the display cout << "Number of marks in exam ==> ";
note: strings are enclosed in double quotes characters are enclosed in single quotes
Fundamental Programming 310201 25
Code in C++ (for old compiler) set HiddenChar to “d”set AskAgain to Truewhile AskAgain
write “Guess the hidden character (0 to Exit) ==> “read Reply
if Reply = HiddenChar or Reply = “0” then set AskAgain to False if Reply = HiddenChar then write “Congratulations!”
Fundamental Programming 310201 26
A Start#include <iostream.h>void main (void){ const true = 1,
false = 0; char HiddenChar = 'd', Reply = ' '; int AskAgain = true;
}
Fundamental Programming 310201 27
Activity Break
Fundamental Programming 310201 28
A Solution #include <iostream.h>void main (void){ const true = 1,
false = 0; char HiddenChar = 'd', Reply = ' '; int AskAgain = true;
while (AskAgain) {
cout << "Guess the hidden character (0 to Exit) ==> "; cin >> Reply; if ((Reply == HiddenChar) || (Reply == '0')) { AskAgain = false; if (Reply == HiddenChar) { cout << "Congratulations!"; }
} }}
Fundamental Programming 310201 29
Summary of Concepts expressions used in:
assignment statements output statements conditions (selection and repetition statements)
operators in numeric expressions : +, -, *, / operators in truth-valued expressions :
relational : <, <=, =, >=, >, Boolean : and, or, not
we now have four types of data: numbers, characters, Boolean, strings
Fundamental Programming 310201 30
C++ Syntax Summary input : cin >> <variable>; output : cout << <expression>; assignment : <variable> = <expression>; a selection statement:
if ( <condition> ){ <if-block statements> }
else{ <else-block statements> }
a repetition statement: while ( <condition> ){ <while-block statements> }
Fundamental Programming 310201 31
C++ Syntax Summary data types declared as:
numbers – int or float characters – char Boolean – bool strings left to later
in C++: truth-valued expression enclosed in parentheses equality operator is “ ==“ not-equal operator is “ !=“ characters enclosed in single quotes strings enclosed in double quotes True is 1, False is 0
Fundamental Programming 310201 32
C++ Syntax for Old Compilers use &&, ||, ! for “and”, “or”, and “not” in place of bool, use int variables, with
constants true (1) and false (0)