Decision I (if Statement)

Slides:



Advertisements
Similar presentations
 Control structures  Algorithm & flowchart  If statements  While statements.
Advertisements

CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
The Program Design Phases
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
The Ohio State University
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
CNG 140 C Programming (Lecture set 3)
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Chapter 4 (Conditional Statements)
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Programming Fundamentals
Chapter 6: Conditional Statements and Loops
Chapter 4 Control Statements: Part I
More Selections BIS1523 – Lecture 9.
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Computers & Programming Languages
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 4: Decision Structures and Boolean Logic
Pages:51-59 Section: Control1 : decisions
Selection Statements.
Looping III (do … while statement)
Understanding Conditions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
Life is Full of Alternatives
Chapter 4: Selection Structures: if and switch Statements
Pages:51-59 Section: Control1 : decisions
Chapter 4: Decision Structures and Boolean Logic
Life is Full of Alternatives Part 3
ICS103: Programming in C 4: Selection Structures
Control Structures.
Presentation transcript:

Decision I (if Statement) Control structures regulate the flow of execution. So far, you know of three types of control structure: sequential, selection, repetition if statement is used for selection/decision in C++

Outline What is Decision if statement (section 4.3) Relational & equality operators Logical operators Multiple Decision - Nested if statements (section 4.7) if statement with compound alternatives (section 4.4) CSCE 106

Decision Or selection is the choice of alternate paths (branches) depending on a condition that may arise in the logical flow of the algorithm. Decision/Selection construct is a question with two outcomes. In flow charts the notation used for decision is the diamond shape with the condition/question written inside. The diamond shape has one arrow going in and two arrows marked true or false coming out. Notice how the two selection diagrams differ. One with two alternatives, and one with only a dependant statement. CSCE 106

if Statement if allows a question to be asked if (condition) statementT; else statementF; if allows a question to be asked There are two basic forms for an if statement: a dependent (conditional) statement a choice between two alternatives two alternatives optional else is optional in the structure of an if statement. if with Dependent Statement When condition evaluates to true, the statement is executed; when false, it is skipped E.g.: if (x != 0) product = product * x; CSCE 106

Table 4.1 Rational and Equality Operators CSCE 106

Relational & Equality Operators (cont’d) Relational expressions (conditions) are used to perform tests for selecting among alternative statements to execute. Typical forms: variable relational-operator variable variable relational-operator constant variable equality-operator variable variable equality-operator constant Evaluate to Boolean (bool) value of true or false CSCE 106

Examples x x <= 0 y x >= y -5 7 -5 -5 7 true false CSCE 106

Logical Operators What if you have more complex conditions, like checking ranges (0<x<y)? The normal way you represent ranges is not valid in programming. You use logical operators to form more complex conditions && (and) || (or) ! (not) E.g. (10000 < salary) && (salary <= 20000) (salary < minSalary) || (dependents > 5) (temperature > 90.0) && (humidity > 0.90) winningRecord && (!probation) CSCE 106

Table 4.3 && Operator CSCE 106

Table 4.4 || Operator CSCE 106

Table 4.5 ! Operator CSCE 106

Exercise Please analyse, design and implement an algorithm to calculate and output the tax due according to the annual income which is input by the user. The tax rate should be calculated as follows: Tax rate = 10% for annual income < 20,000 LE = 20% for annual income >= 20,000 The selection construct has to be used to solve this exercise. CSCE 106

Solution Steps Problem statement/Requirements phase. Analysis Input: income Output: taxDue Additional program variables. Processing formulas. Design phase. Implementation phase. Testing phase. Let’s first start with analysing the problem. What is the input, output, and processing? Then design by drawing the flow chart. Please draw the flow chart in your notes. CSCE 106

Design if (income < 20000) taxDue = income * 0.1; else START Design INPUT income True income < 20000 ? if (income < 20000) taxDue = income * 0.1; else taxDue = income * 0.2; taxDue = income * 0.1 False taxDue = income * 0.2 OUTPUT taxDue STOP CSCE 106

Multiple Selection Problem Modification Tax rate = 10% for annual income < 20,000 LE = 20% for 20,000<=annualincome<30000 = 30% for annual income >= 30,000 CSCE 106

Design Modification Nested if Statements if (income < 20000) taxDue = income * 0.1; else if (income < 30000) taxDue=income*0.2; taxDue=income*0.3; True income < 20000 ? taxDue = income * 0.1 False True income < 30000 ? taxDue = income * 0.2 False taxDue = income * 0.3 Nested if Statements CSCE 106

Nested if Statements Nested logic, in general, is one control structure containing another similar control structure E.g. one if statement inside another Makes it possible to code decisions with several alternatives CSCE 106

General Form if (condition1) statement1; else if (condition2) . else if (conditionn) statementn; else statemente; CSCE 106

Example if (x > 0) numPos = numPos + 1; else if (x < 0) numNeg = numNeg + 1; else // x must equal 0 numZero = numZero + 1; CSCE 106

if Statements with Compound Alternatives Uses { } to group multiple statements. Any statement type (e.g. assignment, function call, if) can be placed within { }. Entire group of statements within { } are either all executed or all skipped when part of an if statement. CSCE 106

Example if (transactionType == ‘c’) { // process check cout << “Check for $” << transactionAmount << endl; balance = balance - transactionAmount; } else { // process deposit cout << “Deposit of $” << transactionAmount << endl; balance = balance + transactionAmount; CSCE 106

Next lecture we will continue decision construct in C++ CSCE 106