Presentation is loading. Please wait.

Presentation is loading. Please wait.

16/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,

Similar presentations


Presentation on theme: "16/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,"— Presentation transcript:

1 16/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +, -, *, /, %, unary -+, and function calls. Evaluate to real or integer values.

2 26/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures String Expressions Used to do string manipulation. Operations consist of string methods ("object-oriented functions") and regular functions. Evaluate to string values String comparison is character-by- character based on ASCII values (see appendix A in textbook) including the null terminator. So “10” < “2”.

3 36/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Relational Expressions Used to test the vales of two arithmetic expressions against each other, or to test the vales of two string expressions against each other. Operations consist of >, =. Evaluate to boolean values (ie, true or false).

4 46/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Boolean Expressions AKA logical expressions Used to combine boolean values. Operations consist of && (and), || (or), and ! (not). Evaluate to boolean values (ie, true or false). See tables 4.3-4.5 in text book (page 170) for definitions of boolean operators.

5 56/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Conditions Operations consist of arithmetic, string, relational, and boolean operators. Evaluate to boolean values (ie, true or false). See table 4.6 in text book (page 171) for precedence rules. Short-Circuit Evaluation: evaluation of expression stops when value of expression is determined.

6 66/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Basic Logic Control Structures SequenceSelectionIteration

7 76/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Sequence Execution starts at the top of a block, and execution proceeds line-by-line through the block; execution continues after the sequence structure. <block>

8 86/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Selection (if) A condition is evaluated, and if it is true, a block is executed; execution continues after the selection structure. if ( ) <block>

9 96/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Iteration (while) A condition is evaluated, and if it is true, a block is executed; this is repeated until the condition is false, then execution continues after the iteration structure. AKA indefinite iteration. while ( ) <block>

10 106/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Block A block consists of EITHER a single statement terminated by a semi-colon OR a { followed by one or more statements each terminated by a semi-colon followed by a } (technically, in either case, the statements are optional).

11 116/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Nested Logic Control Structures A statement in a block may be another logic control structure. These are the only three logic control structures (with nesting) needed to develop any program. Structured Programming Programs that only use the above logic control structures, ie, do not use the goto statement. Programs that only use the above logic control structures, ie, do not use the goto statement.

12 126/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Additional Logic Control Structures These are not needed, but they are 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 executed at least once) Iteration (do) (similar to while loop but loop is executed at least once)

13 136/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Two-Way Selection (If-Else) If ( ) <true-block> if (! ) <false-block> can be re-written as if ( ) <true-block>else<false-block>

14 146/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Multi-Way Selection (Switch) switch ( ) { case : … …} Most of the time, a break statement should be the last statement of each block.

15 156/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures For Loops i = 1;// initialization while (i < 10)// continuation condition { cout << i << “\n”;// block ++ i;// update } Can be re-written as For (i = 1; i < 10; ++ i) { cout << i << “\n”; }

16 166/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Do Loop do // executed at least once // executed at least once while ( ); string done; do{ // some processing …. cout << “Done? “; cin >> done; } while (done != “yes”);

17 176/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Break & Continue The break statement branches control to the next statement after the logic control structure. The continue statement branches control back to the condition in the logic control structure. Neither is used that much, but rarely may be usefull.

18 186/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Loop Control Counters: a variable counts the number of times to loop through data. Sentinals: a special value at the end of a data list signals when the loop should stop. Flags: a boolean variable is used to record a condition when the loop should stop.

19 196/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Pseudo-code A program design method where a program is initially written in a natural language (eg English), then is repeated re- written in more detail until it is written in a programming language (eg C++).

20 206/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Formatting C++ does not impose any format. But typically: if ( ) { } if ( ) { }else{ }

21 216/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Formatting while ( ) {<block>} for ( ; ; ) {<block>}

22 226/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Formatting if (c1) { while (c2) while (c2) { if (c3) if (c3) { do do {...... } while (c4); } while (c4); } else else { for (init; c5; update) for (init; c5; update) {...... } } }}

23 236/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Formatting Be consistent with whatever style you choose to make the code easier to understand!

24 246/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Goto Unconditional transfer of control to anywhere in the same function. Format: … goto ; goto ; …<label>: …

25 256/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures When to use a goto There may be rare and obscure reasons to use a goto, but generally it should not be used – the result may be “spaghetti code” (incomprehensible logic). NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER use a goto in this course!

26 266/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Infinite Loops A loop that never terminates on its own. A logic error. Example: // print the values 1 to 5 i = 1; while (i <= 5) {// notice the lack of cout << i << “\n”; // incrementing i - this cout << i << “\n”; // incrementing i - this }// actually prints 1s forever

27 276/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Development Pointers Use top-down design, incremental design, and pseudo- code. Think about how you would solve the problem using paper and pencil. The computer solution will probably be similar. After determining the steps you would take, then determine the equivalent operations in the programming language. Then code those operations. The patterns in the output to be achieved frequently will point you towards the logic of the program. Think about the operations to produce the output, the calculations needed to do that, and finally the input needed to do that. Work backwards from output to input. You know what you want to produce – how do you get there?

28 286/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures State of the Machine The state of the machine refers to all of the variables used in a program – what the values mean at any point during the execution of that program. Understanding this is key to understanding the program, because it leads to understanding what is happening at any one point in the program.

29 296/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Examples ch04pp05.cpp ch05pp01.cpp ch05pp03.cpp ch05pp05.cpp ch05pp09.cpp ch05pp11.cpp

30 306/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Reading Chapter 4 & 5 cover these same concepts. Reviewing them may help clarify the material.


Download ppt "16/27/2015 11:53 PM6/27/2015 11:53 PM6/27/2015 11:53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,"

Similar presentations


Ads by Google