DOC PREVIEW
UD CISC 181 - Introduction to Computer Science

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CISC181 Introduction to Computer ScienceDr McCoy1Dr. McCoyLecture 3 (2) & 4September 8 & 10, 2009Programming Gets Tougher• Need rules for thinking about more difficult programming problems• Take your time – think first.M k d t d h titi2•Make sure you understand what it is you are trying to do before you try to do it.Rules1. Think before you code– Use some abstract short-hand design• Flowcharts/activity diagrams•Pseudocode–informal language for writing3Pseudocode informal language for writing “algorithms”– Set of actions to be executed– Specified order for the actionsMore Rules for Thinking2. Know the tools available to you– Control structures of the language452.4 Control Structures• Sequential execution– Statements executed in order• Transfer of control– Next statement executed not next one in sequence•3 control structures (Bohm and Jacopini) 2003 Prentice Hall, Inc. All rights reserved.3 control structures (Bohm and Jacopini)– Sequence structure• Programs executed sequentially by default– Selection structures• if, if/else, switch– Repetition structures• while, do/while, for62.4 Control Structures 2003 Prentice Hall, Inc. All rights reserved.272.5 if Selection Structure• Flowchart of pseudocode statement 2003 Prentice Hall, Inc. All rights reserved.A decision can be made on any expression. zero - falsenonzero - trueExample:3 - 4 is true82.6 if/else Selection Structure 2003 Prentice Hall, Inc. All rights reserved.92.7 The while Repetition Structure 2003 Prentice Hall, Inc. All rights reserved.Control Structures and Programming• Each C++ Program made up of these 7 control structures combined appropriately (turns out that we can make the last of these more specific but we’ll see that10these more specific, but we ll see that later)– Sequentially– Nested112.5 if Selection Structure• Selection structure– Choose among alternative courses of action– Pseudocode example: If student’s grade is greater than or equal to 60Print “Passed”If the condition istrue 2003 Prentice Hall, Inc. All rights reserved.–If the condition is true• Print statement executed, program continues to next statement– If the condition is false• Print statement ignored, program continues– Indenting makes programs easier to read• C++ ignores whitespace characters (tabs, spaces, etc.)122.5 if Selection Structure• Flowchart of pseudocode statement 2003 Prentice Hall, Inc. All rights reserved.A decision can be made on any expression. zero - falsenonzero - trueExample:3 - 4 is true3132.5 if Selection Structure• Translation into C++If student’s grade is greater than or equal to 60Print “Passed”if ( grade >= 60 ) cout << "Passed";  2003 Prentice Hall, Inc. All rights reserved.• Diamond symbol (decision symbol)– Indicates decision is to be made– Contains an expression that can be true or false• Test condition, follow path• if structure – Single-entry/single-exit142.6 if/else Selection Structure 2003 Prentice Hall, Inc. All rights reserved.152.6 if/else Selection Structure• if– Performs action if condition true• if/else– Different actions if conditions true or false• Pseudocodefd d h l 2003 Prentice Hall, Inc. All rights reserved.if student’s grade is greater than or equal to 60print “Passed”elseprint “Failed” • C++ codeif ( grade >= 60 ) cout << "Passed";elsecout << "Failed";if‐else Statement Syntax• Formal syntax:if (<boolean_expression>)<yes_statement>else<no_statement>• Note each alternative is only ONE statement!• To have multiple statements execute ineither branch  use compound statement2‐16Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Branching Mechanisms• if‐else statements– Choice of two alternate statements basedon condition expression– Example:if (hrs > 40)grossPay = rate*40 + 1.5*rate*(hrs‐40);elsegrossPay = rate*hrs;2‐17Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.if‐else Statement Syntax• Formal syntax:if (<boolean_expression>)<yes_statement>else<no_statement>• Note each alternative is only ONE statement!• To have multiple statements execute ineither branch  use compound statement2‐18Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.4Compound/Block Statement• Only "get" one statement per branch• Must use compound statement { }for multiples– Also called a "block" stmt• Each block should have block statement– Even if just one statement– Enhances readability2‐19Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Compound Statement in Action• Note indenting in this example:if (myScore > yourScore){cout << "I win!\n";wager=wager + 100;wager wager + 100;}else{cout << "I wish these were golf scores.\n";wager = 0;}2‐20Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Common Pitfalls• Operator "=" vs. operator "=="• One means "assignment" (=)• One means "equality" (==)VERY different in C++!–VERY different in C++!– Example:if (x = 12) Note operator used!Do_SomethingelseDo_Something_Else2‐21Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.The Optional else• else clause is optional– If, in the false branch (else), you want "nothing" to happen, leave it out–Example:pif (sales >= minimum)salary = salary + bonus;cout << "Salary = %" << salary;– Note: nothing to do for false condition, so there is no else clause!– Execution continues with cout statement2‐22Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Nested Statements• if‐else statements contain smaller statements– Compound or simple statements (we’ve seen)– Can also contain any statement at all, including another if‐else stmt!– Example:if (speed > 55)if (speed > 80)cout << "You’re really speeding!";elsecout << "You’re speeding.";• Note proper indenting!2‐23Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Multiway if‐else• Not new, just different indenting• Avoids "excessive" indenting– Syntax:2‐24Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.5Multiwa y if‐else Example2‐25Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.262.6 if/else Selection Structure• Ternary conditional operator (?:)– Three arguments (condition, value if true, value if false)• Code could be written:cout << ( grade >= 60 ? “Passed” : “Failed” ); 2003


View Full Document
Download Introduction to Computer Science
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Computer Science and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Computer Science 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?