Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Programming 310201 1 Fundamental Programming More Expressions and Data Types.

Similar presentations


Presentation on theme: "Fundamental Programming 310201 1 Fundamental Programming More Expressions and Data Types."— Presentation transcript:

1 Fundamental Programming 310201 1 Fundamental Programming More Expressions and Data Types

2 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

3 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

4 Fundamental Programming 310201 4 Pseudocode Example write “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage if Percentage < 50 then write “ (Fail)” else write “ (Pass)” numeric expression operators is this an operator? is this an expression?

5 Fundamental Programming 310201 5 Truth-Valued Expressions  yes, < is an operator - it’s a binary operator  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)

6 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

7 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;

8 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++: =, >

9 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?

10 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 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

11 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)

12 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))…

13 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))…

14 Fundamental Programming 310201 14 Activity Consider the following expression: not Num1  Num2 or 4 / Num1 + Num2 > 5 The table below list combinations of values for Num1 and Num2. For each combination of values, show the value (True or False) of the expression. Num1Num2not Num1  Num2 or 4 / Num1 + Num2 > 5 12 23

15 Fundamental Programming 310201 15 Activity Break

16 Fundamental Programming 310201 16 Activity Feedback Always 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… Num1Num2not Num1  Num2 or 4 / Num1 + Num2 > 5 12True 23False

17 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…

18 Fundamental Programming 310201 18 Character Variables write “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

19 Fundamental Programming 310201 19 in C++ #include void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; char MoreMarks = ‘Y’; cout "; cin >> NbrMarks; while (MoreMarks == ‘Y’) { cout "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; cout << endl; cout "; cin >> MoreMarks; } note: characters are enclosed in single quotes

20 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 1); cout << (2 < 1);  an example to show how a Boolean variable might be used is…

21 Fundamental Programming 310201 21 Boolean Variables write “Number of marks in exam ==> “ read NbrMarks set MoreMarks to True while 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 Reply if Reply = “N” then set MoreMarks to False note: you may prefer to use while MoreMarks = True…

22 Fundamental Programming 310201 22 ANSI Standard C++ #include void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; char Reply = ‘Y’; bool MoreMarks = true; cout "; cin >> NbrMarks; while (MoreMarks) { cout "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; cout << endl; cout "; cin >> Reply; if (Reply == ‘N’) { MoreMarks = false; } note: bool data type not supported by old compilers note: you may prefer while (MoreMarks == true)…

23 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: constants are similar to variables, but their value is fixed – not variable note: use int instead of bool

24 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 ";  note:  strings are enclosed in double quotes  characters are enclosed in single quotes

25 Fundamental Programming 310201 25 Code in C++ (for old compiler) set HiddenChar to “d” set AskAgain to True while 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!”

26 Fundamental Programming 310201 26 A Start #include void main (void) { const true = 1, false = 0; char HiddenChar = 'd', Reply = ' '; int AskAgain = true; }

27 Fundamental Programming 310201 27 Activity Break

28 Fundamental Programming 310201 28 A Solution #include void main (void) { const true = 1, false = 0; char HiddenChar = 'd', Reply = ' '; int AskAgain = true; while (AskAgain) { cout "; cin >> Reply; if ((Reply == HiddenChar) || (Reply == '0')) { AskAgain = false; if (Reply == HiddenChar) { cout << "Congratulations!"; }

29 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

30 Fundamental Programming 310201 30 C++ Syntax Summary  input : cin >> ;  output : cout ;  assignment : = ;  a selection statement: if ( ) { } else { }  a repetition statement: while ( ) { }

31 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

32 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)


Download ppt "Fundamental Programming 310201 1 Fundamental Programming More Expressions and Data Types."

Similar presentations


Ads by Google