Selection (decision) control structure Learning objective

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
Understanding the Three Basic Structures
Lesson 5 - Decision Structure By: Dan Lunney
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Making Decisions In Python
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The Program Design Phases
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 3 Making Decisions
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Problem Solving with Decisions
Programming Logic and Design, Second Edition, Comprehensive
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Copyright 2003 Scott/Jones Publishing Making Decisions.
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.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Agenda Basic Logic Purpose if statement if / else statement
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
5.04 Apply Decision Making Structures
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Controlling Program Flow with Decision Structures.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
COMP Loop Statements Yi Hong May 21, 2015.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Decisions Chapter 4.
Chapter 4 MATLAB Programming
The Selection Structure
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Programming Fundamentals
Alternate Version of STARTING OUT WITH C++ 4th Edition
Computers & Programming Languages
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Understanding the Three Basic Structures
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
REPETITION Why Repetition?
Presentation transcript:

Selection (decision) control structure Learning objective Learning Objectives . . .. . . . . . . . . . . . . . . . .. . . . . . . page 2 If Statements . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . page 3 Introduction menu

Selection (decision) control structure Menu Upon completion of this module, the student will be able to: Understand, discuss, and trace the flow of control in single and nested if statements given the flow chart or pseudocode. Write an If-Then-Else statement that will perform a given task. Write an If-Then statement that will perform a given task. Write a set of nested If statements that will perform a given task. Introduction menu

Selection (decision) control structure iF statements The main selection control structure or branching statement is the IF-then-Else statement. Branching : is the selections of one or two alternate paths for execution based upon the evaluation of a conditional expression. The selection structure alter the control (or flow) of the program based on the value that is returned when a logical expression is evaluated. The returned value is known as the Boolean value. These values return either true or false. A Boolean Value: is an expression that is either true or false (see conditional expressions) When a single alternative selection structure returns the value of true, the actions on the true branch of the structure are executed. If the expression returns false, no actions are executed and the program control continues to the next executable statement structure in the program. Introduction menu

Selection (decision) control structure iF statements An example of a single alternative selection is a program that is designed to allow the user to input two numbers. The program will then display the difference of the two numbers, Only if the first number is greater than the second number. The flow chart for this program would look like this :  This statement is referred to as a dual alternative statement because there are two possible courses of action. The actions chosen depends on the results of evaluating a Boolean expression. A true conditions one course of action; a false condition contains another course of action. Dual alternative : The if, then, else statement is a dual alternative statement because there is an action to be taken on the true branch and on the false branch. Example:  A Boolean expression : is an expression that is either true or false (see conditional expressions)

Selection (decision) control structure iF statements The format of the IF-Then-Else statement is : if (Boolean expression to be evaluated) then (statement or statements to be executed if the expression is true) else (statement or statements to be evaluated is the statement is false). Note some programming language do not use the word then: if (condition) (true alternative) else (false alternative) Example: if (the sun is shining) then I will go fishing I will play computer games. Are there two alternatives? (see flow chart for example). 

Selection (decision) control structure iF statements A simple version of the IF statement is a single alternative statement. This is the IF-Then statement with no else (or false) alternative. Example : If (I have a fishing pole) then I am going fishing. Note this statement does not tell you what to do if the condition is false. Flow chart example  Another example: If revenue > cost) then  Print: Profit (revenue cost). Note: We made a profit. C++ code example : If (revenue > cost) cout < < “Profit is $ “ < < revenue – cost;

Selection (decision) control structure iF statements If more than two alternatives are needed it is necessary to use nested if statements. Two or more is statements are combined so that then or else part of the first if statements, is an if statement and so on. The form looks like this Nested IF: A nested if is an If statement in which the true or false alternative is itself an If statement.

Selection (decision) control structure iF statements If ( ) then . Else If( ) then Executions will continue through the nested If statement until a true condition is found, and its corresponding statement is executed, then control transfers to the statement following the nested statement.

Selection (decision) control structure iF statements Control exists the nested if when a true condition is executed. Example : A classic example of a nested if statement is to determine a students letter grade given the numerical grade. See the flow chart 

Selection (decision) control structure iF statements See the Pseudocode  Notice: when the correct letter grade is determined the nested If statement is excited at that point.  Notice: The grade F (If (grade > 49 ) ) does not require evaluation. If the grade is not greater than 59 ( ( grade > 59 ) Evaluates to false. The default grade of F is assigned.

Selection (decision) control structure iF statements This task can also be completed by using a sequence of its statements executed one after the other. If (grade > 89) then letter grade = A Endif If (grade > 79) then letter grade = B If (grade > 69) then letter grade = C If (grade > 59) then letter grade = D Else letter grade = F This method seems to be less efficient since each if statement is always executed. There is no early out when a true condition is found.

Introduction to condition statements Quick Check When a dual alternative statement is needed the program uses an: If Then Else Quiz questions If Then Nested If None of these are correct

TRY AGAIN!

CORRECT!

Introduction to condition statements Quick Check Consider the following statement: If (x = 8) then Print x Else Print “x is too large” What is printed when x = 9? 9 Quiz questions X is too large Nothing is printed

TRY AGAIN!

CORRECT!

Introduction to condition statements Quick Check When a single alternative is needed the best way would be to use an: If Then Else Quiz questions If Then Nested If None of these are correct

TRY AGAIN!

CORRECT!

Introduction to condition statements Quick Check When more than two alternatives are needed the best way would be to us a: If then Else Quiz questions If Then Nested If None of these are correct

TRY AGAIN!

CORRECT!

Selection (decision) control structure Quick Check An If statement is some times called a Branching statement? TRUE Quiz questions OR FALSE

TRY AGAIN!

CORRECT!

Selection (decision) control structure Quick Check All component statements of an If statement must always be executed. TRUE Quiz questions OR FALSE

TRY AGAIN!

CORRECT!

Selection (decision) control structure Quick Check One type of selection statement is an If Then statement. TRUE Quiz questions OR FALSE

TRY AGAIN!

CORRECT!

Selection (decision) control structure Quick Check One alternative of the If Then statement is always executed. TRUE Quiz questions OR FALSE

TRY AGAIN!

CORRECT!

Selection (decision) control structure iF statements You have completed the Selection decision control structure lesson.