CP1_4_Decision_1112

# CP1_4_Decision_1112 - low of Control 1 low of Control 1...

This preview shows pages 1–6. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: low of Control 1 low of Control 1 Flow of Control 1 Flow of Control 1 Decisions Decisions Control Structures Control Structures More than 40 years ago, when structured programming was dominant, it was proved that every program, regardless of complexity, could be rewritten using only subunits of three different kinds: Almost all of our demo programs so far have taken this form – sequences of executable statements. Sequence Where certain statements or sequences of statements are selected for execution only if particular conditions Decision are satisfied. Where sequences of statements are executed repeatedly until a particular condition is satisfied. Iteration 2 CSIS1117B Computer Programming 1 2011-2012 In this class we cover the second of these: Decision Decision Decision Often, in our programs, we want certain statements to be executed only if certain criteria are satisfied. For example, recall our demo involving division by zero. If e divide by zero it will cause a run me error we divide by zero it will cause a run-time error. //… Will result in a runtime error int number_submitted; cin << number_submitted; nt otal mark a1 mark + a2 mark + a3 mark; if number_submitted is 0 int total_mark = a1_mark + a2_mark + a3_mark; int avg_per_submission = total_mark / number_submitted; /… //… So, before a division, we want to check if the denominator on ero We en erform the division only if the 3 CSIS1117B Computer Programming 1 2011-2012 is non-zero . We then perform the division only if the condition is satisfied. Selection control Selection control We can achieve what we want by attaching a condition (also known as a test expression ). If the condition evaluates as true , the statement is executed. Otherwise, the statement is skipped. //… if ( number_submitted != 0 ) nt vg per submission otal mark umber submitted conditions int avg_per_submission = total_mark / number_submitted; if ( number_submitted == 0 ) cout << "Number submitted should not be zero" << endl; … execute the statement only if the condition is true is not equal to equal to 4 CSIS1117B Computer Programming 1 2011-2012 is equal to Don't confuse '=' and '==' Don't confuse '=' and '==' As we noted in our last lecture: don't write '=' when you mean '=='. It is a common mistake, especially for new programmers. In our example, we use the condition (number_submitted == 0) . f we made the mistake and typed b b itt d 0) this If we made the mistake and typed (number_submitted = 0) , this would be an assignment expression . But remember that the overall value of an assignment expression is the value of its RHS. The type bool is compatible with int . An int can be assigned to a bool, in which case a value of zero is interpreted as false ....
View Full Document

{[ snackBarMessage ]}

### Page1 / 25

CP1_4_Decision_1112 - low of Control 1 low of Control 1...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online