C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.

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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
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.
 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
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
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.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
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 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
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.
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.
1 Lecture 3 Control Structures else/if and while.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Chapter#3 Part1 Structured Program Development in C++
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
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#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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
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.
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
- Standard C Statements
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
2.6 The if/else Selection Structure
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)

The Hashemite University2 Outline Introduction. if statement. if/else statement. Nested if/else statements. Examples.

The Hashemite University3 Control Structures Sequential execution Statements executed one after the other in the order written Transfer of control When the next statement executed is not the next one in sequence All programs written in terms of 3 control structures: Sequence structure Built into C++. Programs executed sequentially by default. Selection structures C++ has three types - if, if/else, and switch Repetition structures C++ has three types - while, do/while, and for

The Hashemite University4 Combination of Control Structures Two types of control structures combination: Control structure stacking: use them one after the other. Control structure nesting: use one control structure inside the body of another control structure.

The Hashemite University5 The if Selection Structure I Selection structure used to choose among alternative courses of action Pseudocode example: If student’s grade is greater than or equal to 60 Display “Passed” If the condition is true print statement executed and program goes on to next statement If the condition is false print statement is ignored and the program goes onto the next statement Indenting makes programs easier to read C++ ignores white-space characters

The Hashemite University6 The if Selection Structure II Translation of pseudocode statement into C++: 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 the condition, follow appropriate path if structure is a single-entry/single-exit structure

The Hashemite University7 The if Selection Structure III Flowchart (or UML activity diagram) of pseudocode statement true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero - true Example: is true

The Hashemite University8 The if/else Selection Structure I if Only performs an action if the condition is true (single selection structure) if/else A different action is performed when condition is true and when condition is false (double selection structure). Psuedocode if student’s grade is greater than or equal to 60 Display “Passed” else Display “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

The Hashemite University9 The if/else Selection Structure II Ternary conditional operator ( ?: ) (the only C++ ternary operator) Takes three arguments (condition, value if true, value if false ) Our C++ code could be written as: cout = 60 ? “Passed” : “Failed” );//omitting ( ) is syntax error OR grade >= 60 ? cout << “Passed” : cout << “Failed”; Remember that the precedence of ?: is low, so do not forget the parenthesis that is used to force its priority. truefalse display “Failed”display “Passed” grade >= 60

The Hashemite University10 Nested if/else Structure I Nested if/else structures Test for multiple cases by placing if/else selection structures inside if/else selection structures. if student’s grade is greater than or equal to 90 Display “A” else if student’s grade is greater than or equal to 80 Display“B” else if student’s grade is greater than or equal to 70 Display “C” else if student’s grade is greater than or equal to 60 Display “D” else Display“F” Once a condition is met, the rest of the statements are skipped The above code can be rewritten using else if as in the next slide

The Hashemite University11 Nested if/else Structure II if student’s grade is greater than or equal to 90 Display “A” else if student’s grade is greater than or equal to 80 Display “B” else if student’s grade is greater than or equal to 70 Display“C” else if student’s grade is greater than or equal to 60 Display “D” else Display“F” Pay attention to the indentation level and how it is differed from the previous slide.

The Hashemite University12 The if/else Body I The body of if or else is the statement that will be implemented after if or else. If no braces after if or else then the body will be only the first statement after them. If there are braces, then the body will be the compound statement.

The Hashemite University13 The if/else Body II 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 and the body of the else will be the first statement after it only. Block of code Compound statements with declarations

The Hashemite University14 The if/else Body III Every if/else or if is considered as one statement with their bodies. How?? Placing lines of codes between else and the body of its if is syntax error.  Leaving the parenthesis after the if empty (you have not put an expression for the condition) is syntax error.  if () // no condition  syntax error  cout<<"b"; If( grade<50) { cout<<“F”; cout<<“Fail”; } cout<<“you fail”; // syntax error else Cout<<“pass”; if (grade > 60) cout<<"b"; cout<<"hello"; // will produce a syntax error else cout<<"c";

The Hashemite University15 Examples On board Grading system

The Hashemite University16 Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 2: Sections 2.4, 2.5, 2.6