1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Decisions If statements in C.
More on Algorithms and Problem Solving
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Structured Program Development in C
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Computer Organization Six logical units in every.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
CPS120: Introduction to Computer Science Decision Making in Programs.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2 Flow of Control Copyright © 2016 Pearson, Inc. All rights reserved.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Decision making If.. else statement.
The if…else Selection Statement
- Standard C Statements
C++ Conditions Test Cases
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Programming Fundamentals
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Control Statements Paritosh Srivastava.
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009

2 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.

3 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

4 More Rules for Thinking 2.Know the tools available to you –Control structures of the language

 2003 Prentice Hall, Inc. All rights reserved 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 Control Structures

 2003 Prentice Hall, Inc. All rights reserved if Selection Structure Flowchart of pseudocode statement A decision can be made on any expression. zero - false nonzero - true Example: is true

 2003 Prentice Hall, Inc. All rights reserved if/else Selection Structure

 2003 Prentice Hall, Inc. All rights reserved The while Repetition Structure

10 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

 2003 Prentice Hall, Inc. All rights reserved 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 if Selection Structure Flowchart of pseudocode statement A decision can be made on any expression. zero - false nonzero - true Example: is true

 2003 Prentice Hall, Inc. All rights reserved 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 if/else Selection Structure

 2003 Prentice Hall, Inc. All rights reserved 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";

if-else Statement Syntax Formal syntax: if ( ) else Note each alternative is only ONE statement! To have multiple statements execute in either branch  use compound statement 2-16 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Branching Mechanisms if-else statements – Choice of two alternate statements based on condition expression – Example: if (hrs > 40) grossPay = rate* *rate*(hrs-40); else grossPay = rate*hrs; 2-17 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

if-else Statement Syntax Formal syntax: if ( ) else Note each alternative is only ONE statement! To have multiple statements execute in either branch  use compound statement 2-18 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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 2-19 Copyright © 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; } else { cout << "I wish these were golf scores.\n"; wager = 0; } 2-20 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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 2-21 Copyright © 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: 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 2-22 Copyright © 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!"; else cout << "You’re speeding."; Note proper indenting! 2-23 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Multiway if-else Not new, just different indenting Avoids "excessive" indenting – Syntax: 2-24 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Multiway if-else Example 2-25 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

 2003 Prentice Hall, Inc. All rights reserved if/else Selection Structure Ternary conditional operator ( ?: ) –Three arguments (condition, value if true, value if false ) Code could be written: cout = 60 ? “Passed” : “Failed” ); ConditionValue if trueValue if false

27 What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b); min = a;

28 What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b); min = a; else min = b;

29 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;

30 What is wrong, if anything, with the following? Go To File ex2-26-and-more.cc Exercise 2.26 – Dangling-Else Problem Decide what prints Also, another messed-up if statement!