Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 5: Decision Control Structures.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 2: Your First Program.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Introduction to C Programming CE Lecture 3 Control Structures in C.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
COMP Loop Statements Yi Hong May 21, 2015.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Switch statement.
Control Structures I Chapter 3
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
The switch Statement, and Introduction to Looping
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 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Flow of Control.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Control Structures – Selection
Topics The if Statement The if-else Statement Comparing Strings
Control Structures: Selection Statement
The System.exit() Method
Lecture Notes – Week 2 Lecture-2
Conditionals.
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Controlling Program Flow
Presentation transcript:

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 5: Decision Control Structures

Today We Are Going To: Utilize if statements to control the flow of a program Utilize if statements to control the flow of a program Explore nested if statements Explore nested if statements Revisit boolean variables Revisit boolean variables Explore special constructs used for commonly-occuring types of decisions Explore special constructs used for commonly-occuring types of decisions

Topic Making Decisions

The Structure of a Decision You make decisions all the time You make decisions all the time Example: If the temperature is below 30° wear a heavy coat, otherwise if it is below 60° wear a sweatshirt, otherwise wear shorts Example: If the temperature is below 30° wear a heavy coat, otherwise if it is below 60° wear a sweatshirt, otherwise wear shorts Express this thought process using if statements in C++ Express this thought process using if statements in C++

Syntax of an if Statement if (condition1) {statement1;statement2;} else if (condition2) {statement3;statement4;} [repeat as necessary] else{statement5;statement6;}

Breakdown Blocks are used to group multiple actions into a single unit Blocks are used to group multiple actions into a single unit Sometimes not necessary, but aid readability Sometimes not necessary, but aid readability Only the first consequence is required; all of the else s are optional Only the first consequence is required; all of the else s are optional Conditions: parens required Conditions: parens required Conditions: Boolean expressions Conditions: Boolean expressions (Recall the set of boolean operators) (Recall the set of boolean operators)

Example #1 Write a program that will prompt for an input number test whether it is positive Write a program that will prompt for an input number test whether it is positive How many tests will you need? How many tests will you need? Perform them in succession Perform them in succession Mechanics: Mechanics: Create new project (Win32 Console App) Create new project (Win32 Console App) Add a source file Add a source file Create the program structure Create the program structure

Example #1 (cont.) Specification: Is that enough? Specification: Is that enough? Analysis Analysis Design Design Implementation Implementation

Example #2 Now take the program you just wrote and modify it so that it uses else blocks Now take the program you just wrote and modify it so that it uses else blocks No need to repeat the preparation; just modify the implementation No need to repeat the preparation; just modify the implementation

Example #3 Write another program to test an input number; this time test to see whether the number is even or odd Write another program to test an input number; this time test to see whether the number is even or odd Analysis Analysis Design Design Worth Noting: Worth Noting: I use lots of parentheses in conditions I use lots of parentheses in conditions Watch for the == problem again Watch for the == problem again

Example #4 Now modify that program so that it uses a boolean variable to perform the test Now modify that program so that it uses a boolean variable to perform the test Again, no need to repeat early steps; just modify the implementation Again, no need to repeat early steps; just modify the implementation

Example #4 (Worth noting) Worth Noting: Worth Noting: Trusted operator precedence in the assignment (usually I would not) Trusted operator precedence in the assignment (usually I would not) Still placed parentheses around isEven Still placed parentheses around isEven Prepending “is” is a common approach to creating boolean variable names Prepending “is” is a common approach to creating boolean variable names Could we have used this approach with pos/neg test? Could we have used this approach with pos/neg test?

Topic Nesting if Statements

Example #5 Write a program that will convert from a percentage grade to a letter grade Write a program that will convert from a percentage grade to a letter grade Break it down by letter grade first Break it down by letter grade first Then determine + or - Then determine + or - (Note: Try entering a decimal to show the need to filter input.) (Note: Try entering a decimal to show the need to filter input.)

Example #6 Convert this to an unnested if structure Convert this to an unnested if structure Is this not easier to read? Is this not easier to read? Consider: When would a nested if be required? Consider: When would a nested if be required? Example: Imagine a teacher who bases letter grades on percentage, but + and – on an extra credit project Example: Imagine a teacher who bases letter grades on percentage, but + and – on an extra credit project

Topic Combining Conditions

Example #7 Now add an input filter to ensure that the percentage lies within a valid range Now add an input filter to ensure that the percentage lies within a valid range Use logical operators to combine conditions Use logical operators to combine conditions Be generous with parentheses Be generous with parentheses Remember to enclose condition in parentheses Remember to enclose condition in parentheses

Topic Enumerated Possibilities

The switch Statement In some cases a large nested if may be simplified with a switch statement In some cases a large nested if may be simplified with a switch statement Requires that decision condition be an enumerated type: char, byte, short, or int Requires that decision condition be an enumerated type: char, byte, short, or int Condition is the enumerated value, rather than a boolean expression Condition is the enumerated value, rather than a boolean expression

Syntax of a switch Statement switch (enum_value) { case val_1:statement1; case val_1:statement1;statement2;break; case val_2:statement3; case val_2:statement3;statement4;break; case val_3:statement5; case val_3:statement5;statement6;break; default:statement7; default:statement7;statement8;}

Things to Remember If break is omitted execution will continue with the statements in the following case If break is omitted execution will continue with the statements in the following case ==> This can be useful in some cases ==> BUT it can make the switch difficult to debug The default case is optional but strongly recommended The default case is optional but strongly recommended

Today We Covered: The basics of if statements The basics of if statements Considered nested if statements Considered nested if statements Revisited the use of boolean variables Revisited the use of boolean variables Specialized statements used to simplify types of decisions that occur frequently Specialized statements used to simplify types of decisions that occur frequently