Copyright ©2005  Department of Computer & Information Science If-Then-Else Structures.

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

If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Selection (decision) control structure Learning objective
Lesson 5 - Decision Structure By: Dan Lunney
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Conditional Statements Introduction to Computing Science and Programming I.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
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.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Decision Structures.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 11 Describing Process Specifications and Structured Decisions Systems Analysis and Design Kendall and Kendall Fifth Edition.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Computer Science Selection Structures.
Chapter 3 Making Decisions
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Copyright ©2005  Department of Computer & Information Science Switch Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Problem Solving with Decisions
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
CPS120: Introduction to Computer Science Decision Making in Programs.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
CPS120: Introduction to Computer Science Decision Making in Programs.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
CPS120: Introduction to Computer Science Decision Making in Programs.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Sequence, Selection, Iteration The IF Statement
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Control Structures.
Conditionals & Boolean Expressions
Topics The if Statement The if-else Statement Comparing Strings
2-1 Making Decisions Sample assignment statements
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Objectives After studying this chapter, you should be able to:
Code Shape III Booleans, Relationals, & Control flow
Chapter 4: Decision Structures and Boolean Logic
Selection Statements.
ICT Programming Lesson 3:
Programming Concepts and Database
Using Decision Structures
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 4: Decision Structures and Boolean Logic
Control Structures.
Conditionals.
Presentation transcript:

Copyright ©2005  Department of Computer & Information Science If-Then-Else Structures

Copyright ©2005  Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to program a single alternative structure.Understand how to program a single alternative structure. Understand how to program a dual alternative structure.Understand how to program a dual alternative structure. Understand how to program nested conditional structures.Understand how to program nested conditional structures.

Copyright ©2005  Department of Computer & Information Science Types of Programming Structures All modern programming languages include support for three basic families of programming structures:All modern programming languages include support for three basic families of programming structures: –Sequential structures –Decision structures –Looping structures

Copyright ©2005  Department of Computer & Information Science What is a decision structure? Decision structures consist of:Decision structures consist of: –Some type of T/F test –One or more executable blocks of code Which block of code executes depends on the result of the T/F test (“the condition”).Which block of code executes depends on the result of the T/F test (“the condition”).

Copyright ©2005  Department of Computer & Information Science JavaScript Decision Structures JavaScript includes support for three types of decision structures:JavaScript includes support for three types of decision structures: –Single Alternative Decision Structures –Dual Alternative Decision Structures –Multiple Alternative Decision Structures

Copyright ©2005  Department of Computer & Information Science Relational Operators To develop valid conditions, we need to use relational operators.To develop valid conditions, we need to use relational operators. OperatorMeaningOperatorMeaning > Greater Than <= Less Than or Equal to >= Greater Than or Equal to ==Equality < Less Than !=Inequality

Copyright ©2005  Department of Computer & Information Science Creating Good Conditions Test values that are of the SAME DATA TYPE.Test values that are of the SAME DATA TYPE. Clarify your complex conditions, created with Boolean operators, with parentheses.Clarify your complex conditions, created with Boolean operators, with parentheses.

Copyright ©2005  Department of Computer & Information Science Single Alternatives Also called the “If-Then” structure.Also called the “If-Then” structure. Includes a condition and 1 executable block.Includes a condition and 1 executable block. The executable block runs ONLY when the condition evaluates to TRUE; nothing happens for a FALSE.The executable block runs ONLY when the condition evaluates to TRUE; nothing happens for a FALSE.

Copyright ©2005  Department of Computer & Information Science Single Alternative – General Form if(condition) { /* executable block goes here */ goes here */ }//end if

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_01.html.

Copyright ©2005  Department of Computer & Information Science Comparing Strings & Casing When comparing strings, case matters: elephant!=ELEPHANT!=ElephantWhen comparing strings, case matters: elephant!=ELEPHANT!=Elephant When can make comparisons case- insensitive by using the String.toUpperCase() method …When can make comparisons case- insensitive by using the String.toUpperCase() method …

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_02.html.

Copyright ©2005  Department of Computer & Information Science Dual Alternatives Also called the “If-Then-Else” structure.Also called the “If-Then-Else” structure. Includes a condition and 2 executable blocks – the “if” block and the “else” block.Includes a condition and 2 executable blocks – the “if” block and the “else” block. The “if” block executes when the condition tests to TRUE; the “else” block executes when the condition tests to FALSE.The “if” block executes when the condition tests to TRUE; the “else” block executes when the condition tests to FALSE.

Copyright ©2005  Department of Computer & Information Science Dual Alternative – General Form if(condition) { /* true block */ }else{ /* false block */ }//end if

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_03.html.

Copyright ©2005  Department of Computer & Information Science The Conditional Operator For simple if-then-else branches, JavaScript provides a shortcut called the conditional operator.For simple if-then-else branches, JavaScript provides a shortcut called the conditional operator. General Form:General Form: Condition ? Action If True : Action If False

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_04.html.

Copyright ©2005  Department of Computer & Information Science Nesting Conditional Structures Sometimes, we need to test more than one factor before we write our executable code. One way to do this is by using nested conditionals …Sometimes, we need to test more than one factor before we write our executable code. One way to do this is by using nested conditionals … By nesting conditional structures, we write ONE COMPLETE conditional structure inside a SINGLE BRANCH of a parent structure.By nesting conditional structures, we write ONE COMPLETE conditional structure inside a SINGLE BRANCH of a parent structure.

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_05.html.

Copyright ©2005  Department of Computer & Information Science The else if Clause In some cases, we can simplify a nested branch by using an else if clause.In some cases, we can simplify a nested branch by using an else if clause. if(cond1) { //execute if cond1 is TRUE }else if(cond2){ //execute if cond1 is FALSE, //but cond2 is TRUE }else{ //cond1 & cond2 are both FALSE }//end if if(cond1) { //execute if cond1 is TRUE }else if(cond2){ //execute if cond1 is FALSE, //but cond2 is TRUE }else{ //cond1 & cond2 are both FALSE }//end if

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called ifThenElse_06.html.

Copyright ©2005  Department of Computer & Information Science Summary Modern programming languages support sequential, decision and looping structures.Modern programming languages support sequential, decision and looping structures. JavaScript provides support for single alternative if structures and dual alternative if structures.JavaScript provides support for single alternative if structures and dual alternative if structures. Continued …

Copyright ©2005  Department of Computer & Information Science Summary We can use the conditional operator in place of simple if-then-else (dual alternative) structures.We can use the conditional operator in place of simple if-then-else (dual alternative) structures. We can nest entire conditional structures inside a single branch of a parent structure.We can nest entire conditional structures inside a single branch of a parent structure.