Full Transcript

1 4 Control Statements: Part 1  2006 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn: ...

1 4 Control Statements: Part 1  2006 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn:  Basic problem-solving techniques.  To use the if and if...else selection statements to choose among alternative actions.  To use the while repetition statement to execute statements in a program repeatedly.  Counter-controlled repetition and sentinel-controlled repetition.  To use the increment, decrement and assignment operators.  2006 Pearson Education, Inc. All rights reserved. 3 4.1 Introduction 4.4 Control Structures 4.5 if Selection Statement 4.6 if...else Double-Selection Statement 4.7 while Repetition Statement 4.8 Formulating a problem: Counter-Controlled Repetition 4.9 Formulating a problem: Sentinel-Controlled Repetition 4.10 Formulating a Problem: Nested Control Statements  2006 Pearson Education, Inc. All rights reserved. 4 4.1 Introduction Before writing a program – Have a thorough understanding of problem – Carefully plan your approach for solving it While writing a program – Know what “building blocks” are available – Use good programming principles  2006 Pearson Education, Inc. All rights reserved. 5 4.4 Control Structures Sequential execution – Statements executed in sequential order Transfer of control – Next statement executed is not the next one in sequence Structured programming – Eliminated goto statements  2006 Pearson Education, Inc. All rights reserved. 6 4.4 Control Structures (Cont.) Only three control structures needed – No goto statements – Demonstrated by Böhm and Jacopini – Three control structures Sequence structure – Programs executed sequentially by default Selection structures – if, if…else, switch Repetition structures – while, do…while, for seven different types of control statements: (sequence, if, if... else, switch, while, do... while and for)  2006 Pearson Education, Inc. All rights reserved. 7 4.4 Control Structures (Cont.) Single-entry/single-exit control statements – Three types of control statements Sequence statement Selection statements Repetition statements – Combined in one of two ways Control statement stacking – Connect exit point of one to entry point of the next Control statement nesting  2006 Pearson Education, Inc. All rights reserved. 8 C++ Keywords Keywords common to the C and C++ programming languages auto break case char Const Continue default do double Else enum extern float for Goto if int long register Return short signed sizeof static Struct switch typedef union unsigned Void volatile while C++-only keywords and and_eq asm bitand Bitor bool catch class compl const_cast delete dynamic_cast explicit export False friend inline mutable namespace New not not_eq operator or or_eq private protected public reinterpret_cas static_cast t template this throw true Try typeid typename using virtual wchar_t xor xor_eq Fig. 4.3 | C++ keywords.  2006 Pearson Education, Inc. All rights reserved. 9 4.5 if Selection Statement (Single-Selection Statement) Selection statements – Example: if ( grade >= 60 ) cout = 60 ) cout 5 ) cout 5 ) cout

Use Quizgecko on...
Browser
Browser