16/27/2015 11:53 pm6/27/2015 11:53 pm6/27/2015 11:53 pmlogic control structures arithmetic...
Post on 21-Dec-2015
227 views
TRANSCRIPT
1104/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Arithmetic ExpressionsArithmetic Expressions
Used to do arithmetic.Used to do arithmetic.
Operations consist of +, -, *, /, %, unary -+, Operations consist of +, -, *, /, %, unary -+, and function calls.and function calls.
Evaluate to real or integer values.Evaluate to real or integer values.
2204/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
String ExpressionsString Expressions
Used to do string manipulation.Used to do string manipulation.Operations consist of string methods Operations consist of string methods ("object-oriented functions") and regular ("object-oriented functions") and regular functions.functions.Evaluate to string valuesEvaluate to string valuesString comparison is character-by-String comparison is character-by-character based on ASCII values (see character based on ASCII values (see appendix A in textbook) including the null appendix A in textbook) including the null terminator. So “10” < “2”.terminator. So “10” < “2”.
3304/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Relational ExpressionsRelational Expressions
Used to test the vales of two arithmetic Used to test the vales of two arithmetic expressions against each other, or to test expressions against each other, or to test the vales of two string expressions against the vales of two string expressions against each other.each other.
Operations consist of >, <, ==, !=, <=, >=.Operations consist of >, <, ==, !=, <=, >=.
Evaluate to boolean values (ie, true or Evaluate to boolean values (ie, true or false).false).
4404/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Boolean ExpressionsBoolean Expressions
AKA logical expressionsAKA logical expressions
Used to combine boolean values.Used to combine boolean values.
Operations consist of && (and), || (or), and Operations consist of && (and), || (or), and ! (not).! (not).
Evaluate to boolean values (ie, true or Evaluate to boolean values (ie, true or false).false).
See tables 4.3-4.5 in text book (page 170) See tables 4.3-4.5 in text book (page 170) for definitions of boolean operators.for definitions of boolean operators.
5504/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
ConditionsConditions
Operations consist of arithmetic, string, Operations consist of arithmetic, string, relational, and boolean operators.relational, and boolean operators.Evaluate to boolean values (ie, true or Evaluate to boolean values (ie, true or false).false).See table 4.6 in text book (page 171) for See table 4.6 in text book (page 171) for precedence rules.precedence rules.Short-Circuit Evaluation: evaluation of Short-Circuit Evaluation: evaluation of expression stops when value of expression stops when value of expression is determined.expression is determined.
6604/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Basic Logic Control StructuresBasic Logic Control Structures
SequenceSequence
SelectionSelection
IterationIteration
7704/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
SequenceSequence
Execution starts at the top of a block, and Execution starts at the top of a block, and execution proceeds line-by-line through execution proceeds line-by-line through the block; execution continues after the the block; execution continues after the sequence structure.sequence structure.
<block><block>
8804/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Selection (if)Selection (if)
A condition is evaluated, and if it is true, a A condition is evaluated, and if it is true, a block is executed; execution continues block is executed; execution continues after the selection structure.after the selection structure.
if (<condition>)if (<condition>)
<block><block>
9904/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Iteration (while)Iteration (while)
A condition is evaluated, and if it is true, a block A condition is evaluated, and if it is true, a block is executed; this is repeated until the condition is executed; this is repeated until the condition is false, then execution continues after the is false, then execution continues after the iteration structure. AKA indefinite iteration.iteration structure. AKA indefinite iteration.
while (<condition>)while (<condition>)
<block><block>
101004/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
BlockBlock
A block consists of EITHER a single A block consists of EITHER a single statement terminated by a semi-colon OR statement terminated by a semi-colon OR a { followed by one or more statements a { followed by one or more statements each terminated by a semi-colon followed each terminated by a semi-colon followed by a } (technically, in either case, the by a } (technically, in either case, the statements are optional).statements are optional).
111104/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Nested Logic Control StructuresNested Logic Control Structures
A statement in a block may be another A statement in a block may be another logic control structure.logic control structure.These are the only three logic control These are the only three logic control structures (with nesting) needed to structures (with nesting) needed to develop any program.develop any program.Structured ProgrammingStructured Programming Programs that only use the above logic Programs that only use the above logic
control structures, ie, do not use the goto control structures, ie, do not use the goto statement.statement.
121204/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Additional Logic Control StructuresAdditional Logic Control Structures
These are not needed, but they are These are not needed, but they are supplied because they are very useful:supplied because they are very useful: Two-Way Selection (if-else)Two-Way Selection (if-else) Multi-Way Selection (switch)Multi-Way Selection (switch) Iteration (for) (AKA definite iteration)Iteration (for) (AKA definite iteration) Iteration (do) (similar to while loop but loop is Iteration (do) (similar to while loop but loop is
executed at least once)executed at least once)
131304/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Two-Way Selection (If-Else)Two-Way Selection (If-Else)
If (<condition>)If (<condition>)<true-block><true-block>
if (!<condition>)if (!<condition>)<false-block><false-block>
can be re-written ascan be re-written asif (<condition>)if (<condition>)
<true-block><true-block>elseelse
<false-block><false-block>
141404/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Multi-Way Selection (Switch)Multi-Way Selection (Switch)
switch (<expression>)switch (<expression>)
{{
case <constant>:case <constant>:
<block> …<block> …
}}
Most of the time, a break statement shouldMost of the time, a break statement should
be the last statement of each block.be the last statement of each block.
151504/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
For LoopsFor Loops
i = 1;i = 1; // initialization// initializationwhile (i < 10)while (i < 10) // continuation condition// continuation condition{{
cout << i << “\n”;cout << i << “\n”; // block// block++ i;++ i; // update// update
}}Can be re-written asCan be re-written asFor (i = 1; i < 10; ++ i)For (i = 1; i < 10; ++ i){{
cout << i << “\n”;cout << i << “\n”;}}
161604/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Do LoopDo Loop
dodo<block><block> // executed at least once// executed at least once
while (<condition>);while (<condition>);
string done;string done;dodo{{
// some processing ….// some processing ….cout << “Done? “;cout << “Done? “;cin >> done;cin >> done;
} while (done != “yes”);} while (done != “yes”);
171704/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Break & ContinueBreak & Continue
The break statement branches control to The break statement branches control to the next statement after the logic control the next statement after the logic control structure.structure.
The continue statement branches control The continue statement branches control back to the condition in the logic control back to the condition in the logic control structure.structure.
Neither is used that much, but rarely may Neither is used that much, but rarely may be usefull.be usefull.
181804/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Loop ControlLoop Control
Counters: a variable counts the number of Counters: a variable counts the number of times to loop through data.times to loop through data.
Sentinals: a special value at the end of a Sentinals: a special value at the end of a data list signals when the loop should data list signals when the loop should stop.stop.
Flags: a boolean variable is used to record Flags: a boolean variable is used to record a condition when the loop should stop.a condition when the loop should stop.
191904/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Pseudo-codePseudo-code
A program design method where a A program design method where a program is initially written in a natural program is initially written in a natural language (eg English), then is repeated re-language (eg English), then is repeated re-written in more detail until it is written in a written in more detail until it is written in a programming language (eg C++).programming language (eg C++).
202004/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
FormattingFormattingC++ does not impose any format.C++ does not impose any format.But typically:But typically:
if (<condition>)if (<condition>){{ <block><block>}}
if (<condition>)if (<condition>){{ <true-block><true-block>}}elseelse{{ <false-block><false-block>}}
212104/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
FormattingFormatting
while (<condition>)while (<condition>){{
<block><block>}}
for (<initialization>; <condition>; <update>)for (<initialization>; <condition>; <update>){{
<block><block>}}
222204/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
FormattingFormattingif (c1)if (c1){{ while (c2)while (c2) {{ if (c3)if (c3) {{ dodo {{ ...... } while (c4);} while (c4); }} elseelse {{ for (init; c5; update)for (init; c5; update) {{ ...... }} }} }}}}
232304/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
FormattingFormatting
Be consistent with whatever style you Be consistent with whatever style you choose to make the code easier to choose to make the code easier to understand!understand!
242404/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
GotoGoto
Unconditional transfer of control to Unconditional transfer of control to anywhere in the same function.anywhere in the same function.Format:Format:
… … goto <label>;goto <label>; … …<label>:<label>: … …
252504/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
When to use a gotoWhen to use a goto
There may be rare and obscure reasons to There may be rare and obscure reasons to use a goto, but generally it should not be use a goto, but generally it should not be used – the result may be “spaghetti code” used – the result may be “spaghetti code” (incomprehensible logic).(incomprehensible logic).
NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER use a goto in this course!NEVER, NEVER use a goto in this course!
262604/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Infinite LoopsInfinite Loops
A loop that never terminates on its own.A loop that never terminates on its own.A logic error.A logic error.Example:Example:
// print the values 1 to 5// print the values 1 to 5i = 1;i = 1;while (i <= 5)while (i <= 5){{ // notice the lack of// notice the lack of cout << i << “\n”; cout << i << “\n”; // incrementing i - this// incrementing i - this}} // actually prints 1s // actually prints 1s
foreverforever
272704/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
Development PointersDevelopment Pointers
Use top-down design, incremental design, and pseudo-Use top-down design, incremental design, and pseudo-code.code.Think about how you would solve the problem using Think about how you would solve the problem using paper and pencil. The computer solution will probably be paper and pencil. The computer solution will probably be similar. After determining the steps you would take, then similar. After determining the steps you would take, then determine the equivalent operations in the programming determine the equivalent operations in the programming language. Then code those operations.language. Then code those operations.The patterns in the output to be achieved frequently will The patterns in the output to be achieved frequently will point you towards the logic of the program. Think about point you towards the logic of the program. Think about the operations to produce the output, the calculations the operations to produce the output, the calculations needed to do that, and finally the input needed to do needed to do that, and finally the input needed to do that. Work backwards from output to input. You know that. Work backwards from output to input. You know what you want to produce – how do you get there?what you want to produce – how do you get there?
282804/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
State of the MachineState of the Machine
The state of the machine refers to all of The state of the machine refers to all of the variables used in a program – what the the variables used in a program – what the values mean at any point during the values mean at any point during the execution of that program. Understanding execution of that program. Understanding this is key to understanding the program, this is key to understanding the program, because it leads to understanding what is because it leads to understanding what is happening at any one point in the happening at any one point in the program.program.
292904/18/2304/18/23 21:5921:59 Logic Control StructuresLogic Control Structures
ExamplesExamples
ch04pp05.cppch04pp05.cpp
ch05pp01.cppch05pp01.cpp
ch05pp03.cppch05pp03.cpp
ch05pp05.cppch05pp05.cpp
ch05pp09.cppch05pp09.cpp
ch05pp11.cppch05pp11.cpp