Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction."— Presentation transcript:

1 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 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 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 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 5 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 5 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 25 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 25 Hofstra University, CSC005 Typical Statements

26 26 Nassau Community CollegeProf. Vincent Costa Acknowledgements: An Introduction to Programming Using Visual Basic 2012, All Rights ReservedAn Introduction to Programming Using Visual Basic 2012 26 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 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 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 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 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 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


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

Similar presentations


Ads by Google