1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction.

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

 Control structures  Algorithm & flowchart  If statements  While statements.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 4: Control Structures: Selection
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
1 Spreadsheets SELECTION. 2 Aims  Understand the principles of selection in Excel  Examine the basic IF statement format  Look into Nested IF statements.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
B065: PROGRAMMING OPERATORS AND SELECTION 1. Starter  Have a go at the All Logic Signs activity.  COMP1  Programming  8 Selection  All Logic Signs.xls.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Decision Making Selection structures (if....else and switch/case)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 2. Program Construction in Java. 2.4 Selection (decisions)
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
MIT App Inventor Lesson 4 – Decision Making. Agenda Comparisons ◦ Relational Operators Conditions (state checking) ◦ Boolean Operators.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Chapter#3 Part1 Structured Program Development in C++
Computer Programming TCP1224 Chapter 5 The Selection Structure.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Conditionals. Conditional Execution A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
SELECTION CONTROL STRUCTURE Prepared by: Careene McCallum-Rodney.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Sequence, Selection, Iteration The IF Statement
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4: Control Structures
Chapter 4: Decision Structures and Boolean Logic
CSC115 Introduction to Computer Programming
SELECTION STATEMENTS (1)
Control Structures.
ICS 3U Tuesday, September 21st.
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSC530 Data Structure - Decision
Chapter 4: Decision Structures and Boolean Logic
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Chapter 4: Decision Structures and Boolean Logic
Lecture 9: Implementing Complex Logic
Presentation transcript:

1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Session 5 Simple Conditionals Pearson Custom Computer Science CSC 104 Nassau Community College CSC 104 Programming Logic and Problem Solving

2 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Another Brain Teaser A farmer has to take a wolf, a sheep and cabbage across a river in a boat. The small boat has room for the farmer and one of the above. How would he do it, without leaving alone two of the passengers that might eat each other? The wolf will eat the sheep and the sheep will eat the cabbage. The wolf will not eat the cabbage.

3 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Another Brain Teaser Take the sheep to the other side and unload it Go back and take the cabbage, unload it on the other side but load the sheep. Go back to the original side and unload the sheep Take the wolf to the other side and unload it Go back to the original side and get the sheep Another problem solved!

4 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Computer Decisions We are going to talk about how a computer can make decisions based upon certain circumstances or conditions It’s a form of Control Structure

5 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic Hofstra University, CSC005 Control Statements //Write a "Good morning" greeting if //the time is less than 10 var d=new Date() var time=d.getHours() if (time<10) { document.write(" Good morning ") } comment declare control

6 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Life is full of decisions! How do you determine if you need an umbrella? (if it is raining, then use an umbrella) How do you determine if you need a jacket? (if the temperature is below 70 degrees, then I need a jacket) How do you determine if you have to come to CSC 104 class? (if it is Monday or Wednesday, then I have CSC 104) How does the clerk at a store determine if you can purchase beer or they should send you home? (if the person is 21 or older, then sell beer, else send home)

7 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. A Computer Makes Decisions Too These decisions are called selection statements or conditional statements Typically referred to as if statements or if then statements because they follow the syntax: (if x, then y, end if). The computer can handle simple if statements, simple if/else statements, complex if and if/else statements as well as nested if and if/else statements. We are going to focus on simple if, simple if/else statements and if time permits, nested if/else statements.

8 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. If Statement An if statement allows the computer to check a condition and decide which statement(s) to execute based upon the evaluation of that condition A condition evaluates to true or false. If the condition is true, the then statement(s) are executed, if the condition is false, the else statement(s) are executed

9 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Simple if Statements Structure: if (value1 relationalOperator value2) then statement(s) to be executed if the condition evaluates to TRUE end if There are 6 relational operators: greater than <=less than or equal to >=greater than or equal to =equal to not equal to

10 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 1 If statement exampleResults for each of the different values if (weather = “raining”) then bring umbrella end if values for weather: raining: bring umbrella sunny: drizzling:

11 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 2 If statement exampleResults for each of the different values if (temp < 70) then bring jacket end if values for temp: 65: jacket 70: 80:

12 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 3 If statement exampleResults for each of the different values if (sales > 5000) then bonus is 500 end if values for sales: 1000: 5000: 5050: bonus is 500

13 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 4 If statement examplesResults for each of the different values if (feeling = “hungry”) then eat end if values for feeling: full: hungry: eat starving:

14 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Typical Statements The if statement allows the program to test the state of the program variables using a Boolean expression

15 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Selection Statements Flow of control of if statement

16 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 1 Write the following if statement: “get more paper” if the paper count is less than 10 If statementResults for 3 different test values values for paper count

17 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 2 Write the following if statement: “buy new computer” if the computer’s age is 3 or older If statementResults for 3 different test values values for computer's age

18 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 3 Write the following if statement: “Grade of A” if a student has an average of 90 or higher If statementResults for 3 different test values values for average

19 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 4 Write the following if statement: “return allowed” if a customer is attempting to return an item within 90 days of purchase If statementResults for 3 different test values values for days of purchase

20 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Simple if/else Statements Structure: if (condition) then statement(s) to be executed if the condition evaluates to TRUE else statement(s) to be executed if the condition evaluates to False end if ( recall that a condition is in the format “value1 relationalOperator value2”) There are 6 relational operators: greater than <=less than or equal to >=greater than or equal to =equal to not equal to

21 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 1 If/else statement example Results for each of the different values if (age >= 21) then sell beer else send home end if values for age: 20: send home 21: sell beer 22: sell beer

22 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 2 If/else statement example Results for each of the different values if (hoursWorked > 40) then overtime pay else regular pay end if values for hoursWorked: 30: regular pay 40: regular pay 45: overtime pay

23 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 3 If/else statement example Results for each of the different values if (itemsPurchased >= 5) then 20% discount else 10% discount end if values for itemsPurchased: 1: 10% discount 5: 20% discount 10: 20% discount

24 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Example 4 If/else statement examples Results for each of the different values if (number > 100) then high else low end if values for number: 90: low 100: low 110: high

25 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic Hofstra University, CSC005 Typical Statements

26 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic Hofstra University, CSC005 //If the time is less than 10, you will get "Good morning," //Otherwise you will get a "Good day" greeting. var d = new Date() var time = d.getHours() if (time < 10) { document.write("Good morning!") } else { document.write("Good day!") }

27 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 1 Write the following if/else statement: “too much” if the price is more than $50, “I can afford that” if price is $50 or less If/else statementResults for 3 different test values

28 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 2 Write the following if statement: “wash dishes” if the dishwasher is full, “check tomorrow” otherwise If/else statementResults for 3 different test values

29 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 3 Write the following if statement: “pass” if a student has an average of 70 or higher, “fail” otherwise If/else statementResults for 3 different test values

30 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 Exercise 4 Write the following if statement: “regular price” if age is less than 65, “senior discount” otherwise If/else statementResults for 3 different test values

31 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 international, open membership, not- for-profit technology standards consortium. Homework Handed out at the next class