LN #2 (3 Hrs) Variables, Sequence Boolean Logic & Selection CTPS Department of CSE,Coimbatore

Similar documents
Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

How Do Robots Find Their Way?

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Grade 8 Common Mathematics Assessment Multiple Choice Answer Sheet Name: Mathematics Teacher: Homeroom: Section A No Calculator Permitted

Relational & Logical Operators, if and switch Statements

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Chapter 4: Making Decisions

Assessment - Unit 3 lessons 16-21

Logic & program control part 3: Compound selection structures

LN #3 (3 Hrs) Repetition, Computational state CTPS Department of CSE,Coimbatore

Microsoft Visual Basic 2005: Reloaded

TICKET PRE-SALE INFORMATION

Selection Statements

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

CHAPTER : 9 FLOW OF CONTROL

Private Swimming Lessons

SELECTION. (Chapter 2)

Using Variables to Write Pattern Rules

Computational Expression

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Objectives/Outcomes. Introduction: If we have a set "collection" of fruits : Banana, Apple and Grapes.

Chapter 3. Iteration

Decision Structures. Chapter 4

Math 55 - Spring 04 - Lecture notes # 1 - Jan 20 (Tuesday)

CS Introduction to Programming Fall 2016

Physics 1P21/1P91. Software Registration. 1. Sapling Learning 2. FlipIt Physics 3. REEF Polling

npm run pull npm start

Repetition Structures

Chapter 4: Making Decisions

CS1150 Principles of Computer Science Boolean, Selection Statements

Functional Mathematics 4368

Pseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Selections. CSE 114, Computer Science 1 Stony Brook University

1. Logging into My Media Mall

Programming Logic and Design Sixth Edition

CS61A Lecture 9 Immutable Data Structures. Jom Magrotker UC Berkeley EECS July 2, 2012

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSCE 206: Structured Programming in C++

CHAPTER 5 FLOW OF CONTROL

ChipRider AQA GCSE Computer Science Mobile Assignment

Connect to CCPL

ibutton Solo Setup Instructions

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Understanding the problem

Rock-Paper-Scissors Multiple versions Nested If / Else If / Else Random Numbers

Logical Operators and if/else statement. If Statement. If/Else (4.3)

THEHOMEPAGE. Vero Beach Computer Group. In this Issue... VOLUME 28, ISSUE 3-4 MARCH/APRIL

Officials Support Center

boolean & if-then-else

STUDENT FAQS (LAUNCHPAD, WRITER'S HELP 2.0, AND LEARNINGCURVE)

COP 1220 Introduction to Programming in C++ Course Justification

Personal Banking Upgrade 2.MO Guide

Geometry Lesson 2.1 Conditional Statements. September 4, 2007

Go to drive.google.com. Google Drive. Select Create an account

5. Selection: If and Switch Controls

PA3: Violet's Vending Venture (Version 3.0)

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Fridays, Saturdays, and Sundays March 2 May 6 (none on Easter, April 1) Saturday times are 9 12:50 p.m., 1 4:50 p.m., 5 9 p.m.

Practice Midterm Exam Solutions

CS 302 ALGORITHMS AND DATA STRUCTURES

Frequently Asked Questions ORDERING ON MYHERBALIFE.COM INDIA, January 2013

Nights & Weekend ROB & PHIL DECEMBER 12, 2008

Baked Potato Bar, 1:00 Cost: $2. You can purchase a potato after everyone on the list has her potato

Orange Coast College BUSINESS & COMPUTING DIVISION EVENING, WEEKEND & ONLINE COURSES. that fit your schedule and goals!

Introduction to Computing Lecture 05: Selection (continued)

plot those points on a graph. 5.Evaluate the equation y = 225 x 2 at the points x={ 15, 12, 9, 0, 9, 12, 15}. Then

Math 202 Test Problem Solving, Sets, and Whole Numbers 19 September, 2008

TO DO LIST FOR THE 2018 CHORUS PHOTO CHAIRMAN

Problem Solving with Decisions. T.Fatin Alhila

Algoritma dan Struktur Data Leon Andretti Abdillah. 08 Control Flow Statements Selection by Using Switch and Case

Lecture 1. Course Overview, Python Basics

LECTURE 04 MAKING DECISIONS

LAB 5: SELECTION STATEMENTS

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Introduction to the MBTA Online Corporate Pass Program

D365 Icons and Tooltips USER GUIDE

61A Lecture 3. Friday, September 5

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Personal Online Banking External Transfers

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

Year 10 Semester 1 Assessment Calendar

1/15 2/19 3/23 4/28 5/12 6/23 Total/120 % Please do not write in the spaces above.

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

AP Computer Science Principles Exam Reference Sheet

Introduction to Programming

DATA TYPES. Numeric Data

IMPORTANT WORDS TO KNOW UNIT 1

Doubletwist won 39 t sync all music

REFERENCE MATERIALS. Assignment, Display, and Input Evaluates expression and assigns the result to the variable a.

Instructions for Filling Out The Indiana 811 Membership Agreement

Practice Midterm Examination #1

Transcription:

LN #2 (3 Hrs) Variables, Sequence Boolean Logic & Selection CTPS 2018

Objectives To understand variables and their values. To study the computational structure, form and functional elements for sequence & selection. To learn Boolean logic.

Variables A variable is like a box, a place holder, a memory location where is stored data in.

Let answer be a variable Get the value from user. Let us say the user types 2 The value 2 is stored in the variable answer

Access the value 2 stored in variable answer and do some processing if (answer is equal to 2) then print "You chose to go left " else print " a most dangerous path"

Variable x x

X = 0 X = X+1

X = 0 X = X+1

X = 0 X = X+1

X = 0 X = X+1

X = 0 X = X+1

Sequence Is a series of steps that take place one after another. 1. Enter the name 2. Enter mark1 3. Enter mark2 4. Enter mark3 5. Total = mark1+mark2+mark3 6. Average = Total / 3 7. Print Total, Average

Normally, statements in a program are executed sequentially, in the order that they were written.

Many solutions feature several choices or decisions. These decisions lead to different paths. These paths represent the result of making a choice.

Selection Is a decision Decisions may be answered as: Yes or No True or False Based on the answer ( Y/N or T/F) a path is chosen

Robots are dumb What does a robot need to know, to solve a maze? What commands and behaviors would be useful? R

R

R

THERE IS EMPTY SPACE R MOVE FORWARD

Conditionals

Boolean Value Anything that results in TRUE or FALSE can go into the if part of a conditional

Boolean Logic A way to figure out the truth of an expression using the simple concept oftrue or FALSE. Boolean logic means you are working with stuff that is eithertrue or FALSE.

Consider sunny_day. A day can either be sunny or not. so, sunny_day can be eithertrue or FALSE. sunny_day is a Boolean Variable that can have a Boolean Value(TRUE or FALSE). Now, say you want to go body surfing at the beach, but you don't want to go when it's cold or rainy, just when it is sunny. You can use Boolean logic

Boolean Expression IF (sunny_day) THEN go body surfing at the beach The Boolean expression sunny_day will have a truth value of TRUE or FALSE

Now let's say you don't swim so well. You need to have your water_wings on too. You now add water_wings to the conditional.

Boolean Expression IF (sunny_day) AND (water_wings) THEN go body surfing at the beach

Boolean Expression IF (sunny_day) AND (water_wings) THEN go body surfing at the beach The Boolean expression will have a truth value of TRUE or FALSE

Boolean Expression IF (sunny_day) AND (water_wings) THEN go body surfing at the beach If both of sunny_day and water_wings are TRUE, then the output (truth value) istrue ie, you head to the beach.. But if even one of these are FALSE, then the output (truth value) is FALSE ie,you stay home and dream of the beach

Operator AND Everything has to be TRUE true AND true = true true AND false AND true = false false AND false = false

Operator OR Only one thing has to be TRUE true OR true = true true OR false OR true = true false OR false = false

Expressions An expression is simply one or more variables and/or constants joined by operators An expression is evaluated and produces a result The result of all arithmetic expressions are either integers or reals An expression can also yield a result that is either true or false- BOOLEAN Such an expression is called a relational expression The result reflects how something "relates to something else. 32 Department of CSE

"Is the value of x greater than the value of y? Note that the preceding poses a question. Relational expressions are usually intended to answer yes/no, or true/false, questions. Obviously, boolean values and boolean variables play an important role in relational expressions. 33 Department of CSE

Operators To build relational expressions, two types of operators are used, relational operators and logical operators Relational operators Logical operators 34 Department of CSE

Expression Value of expression 3 < 4 True 7.6 <= 9 True 4 == 7 False 8.3!= 2.1 True Initial values a = 3 b=4 c=5 d=6 Expression a ==b c< d (a==b) && (c<d) (a==b) && (c<d) Value of expression False True False True result = (a==b) && (c<d)!result False true 35 Department of CSE

Truth assignment: TRUE or FALSE Let, (a < b (a >= b && c == d)) be statement 1 (a < b c == d) be statement 2 In the statements 1 and 2: a < b, c == d, a >= b are conditions to be checked fortrue or FALSE to determine the truth value of the entire expression

Logical expression (a < b (a >= b && c == d)) statement 1 (a < b c == d).. statement 2 # A. Let, a is less than b be True We inspect the first of the two conditions (a < b) to see if it is true It is true in both statements 1 and 2 TRUE is returned by both the statements

# B. Let, a is less than b be FALSE In statement 1 : We inspect the second of the two conditions (a >= b && c == d) to see if it is true We are asking whether both a >= b AND c == d are true If a < b is false, then a >= b is of course true

Therefore whether true or false is returned entirely depends on the condition : c == d If c == d is true then true is returned [ as a < b is false, a >= b is true ] the statement 1 returns true If c == d is false and false is returned [ as a < b is false, a >= b is true ] the statement 1 returns false

In statement 2 : We inspect the second of the two conditions c == d If c == d is true then true is returned a < b is false, the statement 2 returns true If c == d is false and false is returned as a < b is false, the statement 2 returns false

Evaluate the Boolean expressions 1. (T AND (NOT F) OR (T AND (NOT F))) 2. ((NOT F) OR (F AND (NOT T)) AND F)

Can you say how many choices are available for selection?

Multiple Choices One Choice Two Choices Multiple Choices

Selection Statements Selection statement gives a program the ability to choose which instruction(s) to next execute based on conditions. Types 1-Way selection (if statement). 2-Way selection (if-else statement). Multi-Way selection (or n-way selection, switch/case).

Considerations What is the form and type of expression that controls the selection? How are clauses specified if at all? If nesting is allowed, how is it specified and implemented?

One-Way Selection The if structure is a one-way selection structure. When a control expression in an if statement is evaluated to be true, the statements associated with the structure are executed. IF condition ENDIF THEN actions Figure: Selection - One Way

IF (I have a fishing pole) ENDIF THEN I am going fishing IF (orderamount < 40 ) ENDIF THEN ShippingCost =10

Model the following using one-way selection structure: England will qualify for the quarter-finals with Portugal if they win or draw with Romania. If the shop has Croissants buy croissants and leave the shop. If you like salty food then add the amount of salt you prefer.

IF England wins/draws with Romania THEN They will qualify for quarter-finals with Portugal IF the shop has Chips THEN buy Chips IF you like salty food THEN add the amount of salt you prefer

Two-Way Selection The if/else structure is a two-way selection structure. If the control expression in the if statement evaluates to true, one block of statements is executed; otherwise (else), another block is executed. IF condition THEN if-true actions ELSE if- false actions ENDIF Figure: Selection - Two Way

IF (the sun is shining) THEN I will go fishing ELSE I will play computer games IF (orderamount < 40 ) THEN ShippingCost = 10 ELSE ShippingCost = 0 ENDIF

Model using Two-Way selection structure: If there is a fire outside the door then go to another exit. Otherwise, remove the flap covering the handle, turn the handle, pull the door into the plane and throw it out the doorway, as far away as possible.

IF there is a fire outside the door THEN Go to another exit. ELSE 1. Remove the flap covering the handle. 2. Turn the handle. 3. Pull the door into the plane 4. Throw it out the doorway, as far away as possible

Complex Conditionals

Selection from Multiple Choices 1 The problem of determining a students letter grade given the numerical grade.

Consider the requirements for computing shipping cost based on the information given in table(shipping Cost Policy)

IF (orderamount > 0 AND orderamount < 20 ) THEN ShippingCost = 10 ELSE IF (orderamount >= 20 AND orderamount < 40) THEN ShippingCost = 5 ELSE ShippingCost = 0 ENDIF ENDIF

Find the largest of the three numbers A,B and C IF ( A >B ) THEN IF ( A > C ) THEN print A is largest ELSE print Cis largest END IF ELSE IF ( B > C ) THEN print B is largest ELSE print C is largest END IF END IF

Model using Multi-Way selection structure: The phone systems giving you instructions in this form. The message will say something like this: 1. If you wish to buy a ticket then press 1. 2. If you wish to listen to some music then press 2. 3. If you wish to listen to recorded information then press 3. 4. If you wish to speak to an operator then press 4. 5. If you wish to exit press 5.

if ( you wish to buy a ticket ) then press 1 else if ( you wish to listen to some music ) then press 2 else if ( you wish to listen to recorded information ) then press 3 else if ( you wish to speak to the operator ) then press 4 else press 5.

Calculate the sales tax and total amount due for different tax codes. tax code 0: sales_tax = 0 tax code 1: sales_tax = purch_amt * 0.03 tax code 2: sales_tax = purch_amt * 0.05 tax code 3: sales_tax = purch_amt * 0.07

Model using appropriate selection structure: 1. IF the user has struck the Confirm Purchase button, THEN initiate billing procedures. 2. IF the camera sensor detects adequate exposure, THEN close the camera shutter. 3. IF a song has just finished playing, THEN begin playing the next song in the playlist.

4. Shop opening timings 5. Which selection structure is suitable for allowing the users to perform basic operations(add, sub, mul, div, modulo division)?...can you model it?

4. if the day is Monday then the opening hours are 8am-8pm. elseif the day is Tuesday then opening hours are 8am-12pm. elseif the day is Wednesday then opening hours are 8am-8pm. elseif the day is Thursday then opening hours are 8am-8pm. elseif the day is Friday then opening hours are 8am-8pm. elseif the day is Saturday then opening hours are 9am-5pm. else the day is Sunday and the opening hours are 10am-4pm.

Predict the output

What has been described? Variables and values Sequence and Selection Expressions & Operators Boolean logic IF construct Credits Programming Languages,2nd edition,tucker and Noonan www.cse.msu.edu/ Organization of Programming Languages-Cheng (Fall 2004) Computing Without Computers,A Gentle Introduction to Computer Programming,Data Structures and Algorithms,Version 0.15,Paul Curzon Google images