1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
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.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
 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
Lecture 3 Structured Program Development in C
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
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.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
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.
Branching statements.
The if…else Selection Statement
More on the Selection Structure
- Standard C Statements
A mechanism for deciding whether an action should be taken
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Programming Fundamentals
Lecture 2: Logical Problems with Choices
COMS 261 Computer Science I
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
COMS 261 Computer Science I
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10

2 Announcements Read Sections 4.1 – 4.6 Read Lab 3 –For lab on Thursday Homework 3 –Due Wednesday, 9/28/05 –Page –4.1, 4.2, 4.3, 4.10, 4.11, 4.12, 4.33, 4.37, 4.39, 4.47

3 Review Incrementing Defining a constant Conversions Nonfundamental types –Classes –Strings

4 Outline Control Construct –Conditional or selection constructs

5 Conditional Constructs Provide –Ability to control whether a statement list is executed Two constructs –If statement if if-else if-else-if –Switch statement Left for reading

6 Flowchart –Graphical representation of an algorithm –Drawn using certain special-purpose symbols connected by arrows called flowlines –Rectangle symbol (action symbol) Indicates any type of action –Oval symbol Indicates beginning or end of a program, or a section of code (circles)

7 The If Selection Structure Selection structure –Used to choose 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 goes to the next statement –If the condition is false Print statement is ignored Program goes to the next statement

8 The if Selection Structure Translation of PseudoCode into C++ if (grade >= 60) cout << "Passed"; –Indentation to make the program easier to read Diamond symbol (decision symbol) –Indicates decision is to be made –Contains an expression that can be true or false Boolean expression Test the condition, follow appropriate path

9 The If Selection Structure grade >= 60 print passed true false

10 The If Selection Structure Syntax if (Expression) Action If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression Action truefalse

11 Sorting Two Numbers cout << "Enter two integers: "; int value1; int value2; cin >> value1 >> value2; if (value1 > value2) { int rememberValue1 = value1; value1 = value2; value2 = rememberValue1; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;

12 The if/else Selection Structure if –Only performs an action if the condition is true if/else –A different action is performed when condition is true and when condition is false Psuedocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed”

13 The if/else Selection Structure Translation of PseudoCode into C++ if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

14 The if/else Selection Structure Flowchart grade >= 60 print passed print failed true false

15 The if/else Selection Structure Syntax if (Expression) Action 1 else Action 2 If Expression is true then execute Action 1 otherwise execute Action 2

16 Finding the Max cout << "Enter two integers: "; int value1; int value2; cin >> value1 >> value2; int max; if (value1 < value2) { max = value2; } else { max = value1; } cout << "Maximum of inputs is: " << max << endl;

17 The if/else Selection Structure Ternary conditional operator ( ?: ) –Takes three arguments –(condition, value if true, value if false ) –The only ternary C++ operator Using the ternary conditional operator cout = 60 ? “Passed” : “Failed” );

18 The if/else Selection Structure Nested if/else structures –Test for multiple cases by placing if/else selection structures inside if/else selection structures –Once a condition is met, the rest of the statements are skipped

19 The if/else Selection Structure if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”

20 The if/else Selection Structure Compound statement: –Set of statements within a pair of braces –Example: if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } –Without the braces, cout << "You must take this course again.\n"; would be automatically executed

21 The if/else Selection Structure Block –Compound statements –All statements between {} Suggestion –Always use {} to denote the action blocks of an if/else selection structure

22 Errors I make Syntax errors –Errors caught by compiler Logic errors –Errors which have their effect at execution time Non-fatal logic errors –Program runs, but has incorrect output Fatal logic errors –Program exits prematurely

23 Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice –if-else-if statement if-else statements “glued” together –Switch statement An advanced construct

24 A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

25 cout << "Enter simple expression: "; int left; int right; char operator; cin >> left >> operator >> right; cout << left << " " << operator << " " << right << " = "; switch (operator) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }