CISC 181 Introduction to Computer Science Dr Mc
- Slides: 30
CISC 181 Introduction to Computer Science Dr. Mc. Coy Lecture 3 (2) & 4 September 8 & 10, 2009 1
Programming Gets Tougher • • • Need rules for thinking about more difficult programming problems Take your time – think first. Make sure you understand what it is you are trying to do before you try to do it. 2
Rules 1. Think before you code – Use some abstract short-hand design • • Flowcharts/activity diagrams Pseudocode – informal language for writing “algorithms” – Set of actions to be executed – Specified order for the actions 3
More Rules for Thinking 2. Know the tools available to you – Control structures of the language 4
5 2. 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) – Sequence structure • Programs executed sequentially by default – Selection structures • if, if/else, switch – Repetition structures • while, do/while, for 2003 Prentice Hall, Inc. All rights reserved.
6 2. 4 2003 Prentice Hall, Inc. All rights reserved. Control Structures
7 2. 5 if Selection Structure • Flowchart of pseudocode statement A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true 2003 Prentice Hall, Inc. All rights reserved.
8 2. 6 if/else Selection Structure 2003 Prentice Hall, Inc. All rights reserved.
9 2. 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 that later) – Sequentially – Nested 10
11 2. 5 if Selection Structure • Selection structure – Choose among alternative courses of action – Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” – 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. ) 2003 Prentice Hall, Inc. All rights reserved.
12 2. 5 if Selection Structure • Flowchart of pseudocode statement A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true 2003 Prentice Hall, Inc. All rights reserved.
13 2. 5 if Selection Structure • Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed"; • 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-exit 2003 Prentice Hall, Inc. All rights reserved.
14 2. 6 if/else Selection Structure 2003 Prentice Hall, Inc. All rights reserved.
15 2. 6 if/else Selection Structure • if – Performs action if condition true • if/else – Different actions if conditions true or false • Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” • C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed"; 2003 Prentice Hall, Inc. 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 in either branch use compound statement Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -16
Branching Mechanisms • if-else statements – Choice of two alternate statements based on condition expression – Example: if (hrs > 40) gross. Pay = rate*40 + 1. 5*rate*(hrs-40); else gross. Pay = rate*hrs; Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -17
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 in either branch use compound statement Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -18
Compound/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 readability Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -19
Compound Statement in Action • Note indenting in this example: if (my. Score > your. Score) { cout << "I win!n"; wager = wager + 100; } else { cout << "I wish these were golf scores. n"; wager = 0; } Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -20
Common Pitfalls • Operator "=" vs. operator "==" • One means "assignment" (=) • One means "equality" (==) – VERY different in C++! – Example: if (x = 12) Note operator used! Do_Something else Do_Something_Else Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -21
The Optional else • else clause is optional – If, in the false branch (else), you want "nothing" to happen, leave it out – Example: if (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 statement Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -22
Nested Statements • if-else statements contain smaller statements – Compound or simple statements (we’ve seen) – Can also contain any statement at all, including another ifelse stmt! – Example: if (speed > 55) if (speed > 80) cout << "You’re really speeding!"; else cout << "You’re speeding. "; • Note proper indenting! Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -23
Multiway if-else • Not new, just different indenting • Avoids "excessive" indenting – Syntax: Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -24
Multiway if-else Example Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2 -25
26 2. 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” ); Condition 2003 Prentice Hall, Inc. All rights reserved. Value if true Value if false
What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b); min = a; 27
What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b); min = a; else min = b; 28
What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b) min = a; max = b; else min = b; max = a; 29
What is wrong, if anything, with the following? Go To File ex 2 -26 -and-more. cc Exercise 2. 26 – Dangling-Else Problem Decide what prints Also, another messed-up if statement! 30
- Cisc 181
- Cisc 181
- Cisc complex instruction set computer
- English is my favourite subject noun
- Sciencefusion think central
- Introduction to computer science midterm exam
- Introduction to computer science midterm exam test
- Introduction to computer science quiz
- Python programming an introduction to computer science
- Majesteit groot is zijn majesteit
- Ms 181 weebly
- Csce 181
- Csce 181
- Csce courses tamu
- Sda hymnal 100
- Hmn 181
- Nasp code of ethics
- 181
- 181
- Decret 181
- Dönem ayırıcı hesaplar örnek
- Dönem ayirici hesaplar 180 181 280 281 380 381
- Cisc 101
- Similarities between risc and cisc
- Risc versus cisc
- Pengertian risc
- Disadvantages of risc processor
- Isa cisc
- Cisc 1050
- Intel risc
- Cisc pipeline