Boolean Expressions and If statements

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

5.04 Apply Decision Making Structures
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.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
true (any other value but zero) false (zero) expression Statement 2
Chapter 5 - VB.Net by Schneider
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
JavaScript, Third Edition
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Computer Science Up Down Controls, Decisions and Random Numbers.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Sub Procedures And Functions
Chapter # 2 Part 2 Programs And data
LOGICAL CONTROL STRUCTURES (chp. 8)
5.04 Apply Decision Making Structures
Visual Basic 6 (VB6) Data Types, And Operators
UNIT 4 Lesson 13 If statements.
Data Types, Arithmetic Operations
Overview: Programming Concepts
Lists, Loops, Validation, and More
The Selection Structure
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
JavaScript: Control Statements.
Control Structures – Selection
للمزيد زورونا على موقعنا الإلكتروني:
And now for something completely different . . .
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Chapter 7 Conditional Statements
Input and Output.
Fundamentals of visual basic
Chapter 4: Control Structures I (Selection)
Conditional Logic Presentation Name Course Name
BNF 23-Feb-19.
SELECTIONS STATEMENTS
Expressions.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Syntax vs Semantics Backus-Naur Form Extended BNF Derivations
Relational Operators.
Chapter 3: Selection Structures: Making Decisions
Input and Output.
Chapter 3: Selection Structures: Making Decisions
Input and Output Chapter 3.5
Decision Making Using the IF and EVALUATE Statements
Presentation transcript:

Boolean Expressions and If statements

TRUE or FALSE All boolean expressions evaluate to the value of either: Examples : see next slide

Examples Examples: 3>2 (evaluates to TRUE) 2>3 (evaluates to FALSE)

Operators and Operands In the expression 3 > 2 The “>” sign is called an “operator” 3 and 2 are called “operands” The value is TRUE In the expression 3 + 2 The “+” sign is called an “operator” The value is 5

Numerical Operators vs Relational Operators The following are numerical operators + - * / The following are relational operators > < = <= >= <>

Relational Operators Symbol > < = >= <= <>` Meaning greater than Less than Equal to Greater than OR equal to Less than OR equal to Is not equal to Examples 2>3 is false, 3>2 is true 2<3 is true, 3<2 is false 2=2 is true, 2=3 is false 2>=2 is true, 2>=3 is false 3>=2 is false 2<=2 is true, 2<=3 is true 3<=2 is false 2<>2 is false, 2<>3 is true

IF statement

How can we use this? Dim age As Integer Given the following application, the message “You may vote” will appear only if the age is 18 or more. NOTHING will be displayed if the age is less than 18. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim age As Integer age = Integer.Parse(TextBox1.Text) If age >= 18 Then MessageBox.Show("You may vote.") End If End Sub see NEXT SLIDE for results

VS Example Continued Either something happens or it doesn’t. NOTHING HAPPENS

Nothing happens if expression is FALSE The program on the previous slide WILL NOT DISPLAY ANYTHING if the age is less than 18.

Else section

Alternate actions with ELSE Alternate actions may be specified with an optional “Else” section Either the If section will execute OR the Else section will execute BUT NOT BOTH Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim age As Integer age = Integer.Parse(TextBox1.Text) If age >= 18 Then MessageBox.Show("You may vote.") Else MessageBox.Show("You may eat ice cream.") End If End Sub

Example Continued IF section executes Else section executes VS

Syntax

If without and Else If BOOLEAN EXPRESSION Then statement1 statement2 statement3 etc. End If

If with Else If BOOLEAN EXPRESSION Then statement1 statement2 statement3 etc. Else statement1 statement2 statement3 End If

Nested If Statements

Nested If Statements The statements in the IF and ELSE sections can be other If statements. Example: If BOOLEAN EXPRESSION Then statement1 statement2 etc. If ANOTHER BOOLEAN EXPRESSION Then statement3 statement4 etc. End If statement5 statement6 etc. End If

Nesting can be in If, Else or both The nesting can be done in the If or Else sections (or both) Example: If BOOLEAN EXPRESSION Then statement1 statement2 etc. Else statement1 statement2 etc. If ANOTHER BOOLEAN EXPRESSION Then statement3 statement4 etc. End If statement5 statement6 etc. End If

You must have and End If for each Nested If Example 2 If BOOLEAN EXPRESSION Then If BOOLEAN EXPRESSION Then If BOOLEAN EXPRESSION Then Statement1 … End IF End IF End IF EXAMPLE 1 If BOOLEAN EXPRESSION Then Statement1 … If BOOLEAN EXPRESSION Then Statement1 … If BOOLEAN EXPRESSION Then Statement1 … End IF Statement1 … End IF Statement1 … End IF

Several Unconnected Ifs

When NOT to use Else -4 (minus 4) Multiple conditional sections that are independent of each other should NOT use else Example: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim num As Integer num = Integer.Parse(TextBox1.Text) If num > 0 Then MessageBox.Show("positive") End If If num Mod 2 = 0 Then MessageBox.Show("even") End Sub -4 (minus 4) Does NOT display “positive” DOES display “even”.

ElseIf

ElseIf ElseIf sections can be used to specify multiple alternate courses of action ElseIf is ONE WORD (no space) The first boolean expression to evaluate to TRUE causes that section to be executed. All other sections are then skipped. Example on next slide …

ElseIf Example If age >= 65 Then Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim age As Integer age = Integer.Parse(TextBox1.Text) If age >= 65 Then MessageBox.Show("You may collect social " & _ "security, drink and vote.") ElseIf age >= 21 Then MessageBox.Show("You may drink and vote.") ElseIf age >= 18 Then MessageBox.Show("You may vote.") Else MessageBox.Show("You may eat ice cream.") End If End Sub

Backus Naur Form (BNF)

What is BNF “Meta-language” Used to show the syntax of another language

Basic Symbols in BNF The meta-symbols of BNF are: ::= meaning "is defined as" | meaning "or" < > angle brackets used to surround names of constructs.

[ Optional item ] optional items are enclosed in brackets, [ and ], example: <if_statement> ::= if <boolean_expression> then <statement_sequence> [ else <statement_sequence> ] end if

{ repetitive item } repetitive items (zero or more times) are enclosed in curly braces, { and }, example: <if_statement> ::= if <boolean_expression> then <statement_sequence> { elseIf <boolean_expression> then <statement_sequence> } [ else <statement_sequence> ] end if ;

One character literals (e.g. “;”) Literals of only one character are surrounded by quotes (") to distinguish them from meta-symbols (e.g. < and [ ) example: <comment> ::= “’”<any text><end of line>

Select Case

Syntax (BNF form) { Case <expressionlist> Select [ Case ] <testexpression> { Case <expressionlist> [ <statements> ] } [ Case Else [ <statements> ] ] End Select

Boolean Operators: AND, OR, NOT