COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Types and Arithmetic Operators
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Data types and variables
Introduction to Python
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
A Review of Programming and C++
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Fundamental Programming Fundamental Programming More on Selection.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Fundamental Programming Fundamental Programming for Loops.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Controlling Program Flow with Decision Structures.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Fundamental Programming Fundamental Programming Introduction to Functions.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Data Types and Expressions
BASIC ELEMENTS OF A COMPUTER PROGRAM
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
Variables, Expressions, and IO
2.1 Parts of a C++ Program.
Chapter 7 Additional Control Structures
Building Java Programs
Fundamental Programming
Building Java Programs Chapter 2
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Boolean Expressions to Make Comparisons
Fundamental Programming
Building Java Programs Chapter 2
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters 4, 5 Study Guide Book 1, Module 4  Variables  Branching or selection statements  Looping statements  Expressions, Operators, Operands, etc

COIT29222 Structured Programming 2 Variables  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…

COIT29222 Structured Programming 3 Example Pseudocode write “Number of marks in exam ==> “ read M1 write “Student’s mark ==> “ read M2 set M3 to M2 / M1 set M4 to 100 * M3 write “Student’s percentage: “ write M4

COIT29222 Structured Programming 4 Variables write “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “ read StudentMark set ProportionOfMarks to StudentMark / NbrMarks set Percentage to 100 * ProportionOfMarks write “ Student’s percentage: “ write Percentage 4 variables: NbrMarks, StudentMark, ProportionOfMarks, Percentage

COIT29222 Structured Programming 5 Alternative Design 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 3 variables: NbrMarks, StudentMark,Percentage  In C++, this looks like…

COIT29222 Structured Programming 6 Sample Program In C++ #include void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; cout "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; }

COIT29222 Structured Programming 7 Declaring 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, …

COIT29222 Structured Programming 8 Naming 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…

COIT29222 Structured Programming 9 Initialising 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!

COIT29222 Structured Programming 10 More 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 student’s mark: 15.5 (marks)  A distance: (meters)  A temperature: -2.3 (degrees C)

COIT29222 Structured Programming 11 Why “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  These two numbers can be represented as:  * and *  float variables are stored in two parts:  number part : in above cases  power part : 18 or –12 in above cases  Decimal point “floats around”, depending on power part – more details later

COIT29222 Structured Programming 12 More 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++ - it’s “common practice” float StudentMark = 0, Percentage = 0;  Notice how a comma is used here to separate two use of punctuation -, and ;

COIT29222 Structured Programming 13 Branching  Our example program always performs the exact same actions: get number of marks in exam get student’s mark calculate the student’s percentage display the student’s 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”

COIT29222 Structured Programming 14 Sample Dialogs Number of marks in exam ==> 80 Student’s mark ==> 60 Student’s percentage: 75 (Pass) or… Number of marks in exam ==> 80 Student’s mark ==> 20 Student’s percentage: 25 (Fail)

COIT29222 Structured Programming 15 Branching 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

COIT29222 Structured Programming 16 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)”

COIT29222 Structured Programming 17 Example of if-then-else 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)” NbrMarks: ? else StudentMark: ? write “ (Pass)” Percentage: ?  in C++, this looks like…

COIT29222 Structured Programming 18 if-else Statements #include void main (void) { float StudentMark = 0; cout "; cin >> StudentMark; if (StudentMark >= 50) { cout << "Pass!"; } else { cout << "Fail!"; }

COIT29222 Structured Programming 19 if-else Statements  Notice that comparison between StudentMark and 50 is enclosed in parentheses if (StudentMark >= 50)  Also, braces mark start and end of the if-branch (shown below), and the else-branch { cout << "Pass!"; }  Indentation makes code easier to read – it’s not required by the compiler

COIT29222 Structured Programming 20 if-else-if Statements  if-else-if statements can be coded as: if (StudentMark >= 75) { cout << “Distinction!"; } else if (StudentMark >= 50) { cout << “Pass!"; } else { cout << "Fail!"; }

COIT29222 Structured Programming 21 Activity – Code in C++ write “Select conversion - (1) C to F, (2) F to C ==> “ read ConversionType write “Input temperature ==> “ 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”

COIT29222 Structured Programming 22 A Start #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; }

COIT29222 Structured Programming 23 A Solution #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + (Temperature * 1.8); cout << " degrees Fahrenheit"; } else { cout << (Temperature - 32) / 1.8; cout << " degrees Centigrade"; } note: C++ uses == instead of = for testing equality

COIT29222 Structured Programming 24 Looping  Our program to calculate the exam percentage for a single student performs the following 5 tasks: get number of marks in exam get student’s mark calculate the student’s percentage display the student’s 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…

COIT29222 Structured Programming 25 Looping  To calculate the percentage for 2 students, a program must perform 9 tasks: get number of marks in exam get student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail” get student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail”

COIT29222 Structured Programming 26 Looping  To calculate the percentage for 3 students, a program must perform 13 tasks: get number of marks in exam get student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail” get student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail” get student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail”

COIT29222 Structured Programming 27 Looping  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 student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail”

COIT29222 Structured Programming 28 Looping  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 student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail”  Another design is…

COIT29222 Structured Programming 29 Looping  Keep asking if the user has more students: get number of marks in exam - repeat while user wants more students - get student’s mark calculate the student’s percentage display the student’s percentage display “Pass” or “Fail” ask user if they have more students

COIT29222 Structured Programming 30 Looping 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

COIT29222 Structured Programming 31 Pseudocode Example write “Number of marks in exam ==> “ read NbrMarks write “Number of students ==> “ read NbrStudents set NbrProcessed to 0 while NbrProcessed < NbrStudents write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage set NbrProcessed to NbrProcessed + 1

COIT29222 Structured Programming 32 Example of while write “Number of marks in exam ==> “ NbrMarks:80 read NbrMarks NbrStudents: 2 write “Number of students ==> “ NbrProcessed: 2 read NbrStudents StudentMark:40 set NbrProcessed to 0 Percentage:50 while NbrProcessed < NbrStudents write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage set NbrProcessed to NbrProcessed + 1

COIT29222 Structured Programming 33 Activity Show the output produced by the following pseudocode. set TotalCost to 20 set ItemCost to 5 set NumberOfItems to 0 while TotalCost > 0 set TotalCost to TotalCost - ItemCost set NumberOfItems to NumberOfItems + 1 write “Number of items purchased: “ write NumberOfItems

COIT29222 Structured Programming 34 while Statement #include void main (void) { int NbrTimesTold = 0, NbrTimesToTell = 0; cout "; cin >> NbrTimesToTell; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!"; cout << endl; NbrTimesTold = NbrTimesTold + 1; }

COIT29222 Structured Programming 35 Activity Feedback  The program displays “No new taxes!” the number of times requested by the user… cout << endl; moves to start of next line How many times must I tell you? ==> 3 No new taxes!

COIT29222 Structured Programming 36 while Statement  Things to notice:  Comparison enclosed in parentheses while (NbrTimesTold < NbrTimesToTell) { cout << endl; cout << " No new taxes!"; NbrTimesTold = NbrTimesTold + 1; }  Use of braces and indentation

COIT29222 Structured Programming 37 One 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 void Main (void) { Cout << "Hello World!"; }

COIT29222 Structured Programming 38 C++ is Case-Sensitive #Include void Main (void) { Cout << "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

COIT29222 Structured Programming 39 Data 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...

COIT29222 Structured Programming 40 Sample Program write “Number of marks: “ read NbrMarks write “Student’s mark: “ read StudentMark set ProportionOfMarks to StudentMark / NbrMarks set PercentageOfMarks to ProportionOfMarks * 100 write “ Student’s percentage: “ write PercentageOfMarks  In this program, all the data processing occurs in assignment statements

COIT29222 Structured Programming 41 Anatomy of an Assignment  Let’s analyse the assignment statement…  Here, the two assignment statements are: set ProportionOfMarks to StudentMark / NbrMarks set PercentageOfMarks to ProportionOfMarks * 100  That is: set to  Here, the values of interest are: StudentMark / NbrMarks ProportionOfMarks * 100

COIT29222 Structured Programming 42 Expressions  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

COIT29222 Structured Programming 43 Data Processing  So, where does the data processing happen? answer: some of it happens in assignment statements  It can also happen in output statements…

COIT29222 Structured Programming 44 Alternative Design write “Number of marks: “ read NbrMarks write “Student’s mark: “ read StudentMark set ProportionOfMarks to StudentMark / NbrMarks write “ Percentage: “ write ProportionOfMarks * 100

COIT29222 Structured Programming 45 Anatomy of an Output  The anatomy of our assignment statement is: set to  the anatomy of our output statement is: write  so, where does all the data processing happen? Expressions !

COIT29222 Structured Programming 46 Expressions  Clearly, expressions are important - that’s where the data processing happens  Let’s 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 we’ve seen before...

COIT29222 Structured Programming 47 Expressions 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-output model again Example: StudentMark / NbrMarks input process output StudentMark ? NbrMarks (a single value - depends on inputs)

COIT29222 Structured Programming 48 Operators  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

COIT29222 Structured Programming 49 Operands  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

COIT29222 Structured Programming 50 Binary 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

COIT29222 Structured Programming 51 Unary Operators  The + and - operators are also unary operators (they can be used with just one operand) Examples: as in set AbsoluteZero to as in set BoilingPointOfWater to +100 expression operand operator

COIT29222 Structured Programming 52 Numeric 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

COIT29222 Structured Programming 53 Power 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?

COIT29222 Structured Programming 54 Nested 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?

COIT29222 Structured Programming 55 Activity Feedback  Using StudentMark = 50, NbrMarks = 100…  Division first: (StudentMark / NbrMarks) * 100 =(50 / 100) * 100 = 50  Multiplication first: StudentMark / (NbrMarks * 100) =50 / (100 * 100) =  Will a C++ program do it in the correct order?

COIT29222 Structured Programming 56 Order 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 * / 4 * 3  Will the following be evaluated correctly? StudentMark / NbrMarks * 100

COIT29222 Structured Programming 57 Activity Feedback  Evaluate: 4 * unary operator first (- applies to 2) multiplication before addition (4 * -2) + 3 = = -5

COIT29222 Structured Programming 58 Activity Feedback  Evaluate the following: / 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

COIT29222 Structured Programming 59 Activity 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

COIT29222 Structured Programming 60 Order of Use  Avoid errors by using parentheses: (4 * -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?

COIT29222 Structured Programming 61 Activity 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 doesn’t 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…

COIT29222 Structured Programming 62 #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + Temperature * 1.8; cout << " degrees Fahrenheit"; } else { cout << Temperature - 32 / 1.8; cout << " degrees Centigrade"; }

COIT29222 Structured Programming 63 #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + Temperature * 1.8; cout << " degrees Fahrenheit"; } else { cout << Temperature - 32 / 1.8; cout << " degrees Centigrade"; } problem here: division occurs before subtraction Feedback

COIT29222 Structured Programming 64 #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + (Temperature * 1.8); cout << " degrees Fahrenheit"; } else { cout << (Temperature – 32) / 1.8; cout << " degrees Centigrade"; } a solution: enclose subtraction in parentheses clarification: parentheses make intention clear Feedback

COIT29222 Structured Programming 65 C++ Syntax Summary  input : cin >> ;  output : cout ;  assignment : = ;  A selection statement:  if ( )  { }  else  { }  A repetition statement:  while ( )  { }

COIT29222 Structured Programming 66 Summary  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