CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
CS0007: Introduction to Computer Programming
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
C++ for Engineers and Scientists Third Edition
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
CS0004: Introduction to Programming Input and Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
CS0004: Introduction to Programming Variables – Strings.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Decisions in Python Bools and simple if statements.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Algorithm Design.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
22/11/ Selection If selection construct.
Algorithms Writing instructions in the order they should execute.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
Decision Structures, String Comparison, Nested Structures
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
CEC 220 Digital Circuit Design Boolean Algebra I Wed, Sept 2 CEC 220 Digital Circuit Design Slide 1 of 13.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Expressions Methods if else Statements Loops Potpourri.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Selection Part 2 Using Logical Operators, how strings are compared and random numbers.
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
CS0004: Introduction to Programming
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 4: Decisions and Conditions
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4 The If…Then Statement
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4: Making Decisions.
Multiple variables can be created in one declaration
Decision Making in Code Logical Tests & Truth Logical Expressions
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Chapter 4 – Decisions 4.1 Relational and Logical Operators
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
VB Decisions, Conditions & If
Logical Operations In Matlab.
Microsoft Visual Basic 2005: Reloaded Second Edition
VB Decisions & Conditions
Conditional Logic Presentation Name Course Name
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
The Selection Structure
Presentation transcript:

CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements

Review  Date literals are surrounded by…  Number (pound) signs (#)  Some date functions  Today  Returns today’s date  DateDiff(DateInterval.Day, date1, date2)  Returns the number of days between date1 and date2 as an integer.  See course webpage for different date intervals  theDate.AddYears(n), theDate.AddMonths(n), and theDate.AddDays(n)  Returns the date after adding n years, months, and days (respectively) to theDate.  CDate(dateString)  Typecasts the string dataString to a date data type  Input Dialog Box  InputBox(prompt, title)  Output Dialog Box  MessageBox.show(prompt, title)

Conditions  A condition is an expression involving relational operators that is either true or false.  They may also include logical operators  Relational Operators – compare two entities. Return either True or False.  Logical Operators – combine two or more relational expressions. Return either True or False.  Conditions can involve both literals and variables.  With conditions we can make decisions about what we should execute based on input.  Conditions are used in both decisions and loops.

Relational Operators

 Examples:  3 = 4  Returns False, because 3 is not equal to 4  3 <> 4  Returns True, because 3 is not equal to 4  “Apple” < “Orange”  Returns true, because “A” comes before “O” alphabetically  “apple” > “Orange”  Returns True because “O” has a lower ASCII value than “a”  What the computer actually does when comparing Strings is compare each character left to right by their ASCII values.  ASCII values- numeric representation of characters  See course webpage for ASCII numbers for characters  Important Note: Capital letters have lower ASCII values then lowercase letters  #12/16/1986# <= #2/8/2011#  Returns True, because 12/16/1986 precedes 2/8/2011 chronologically  #12/16/1986# <= #12/16/1986#  Returns True, because 12/16/1986 is the same as 12/16/1986

Logical Operators  Three Logical Operators  And, Or, and Not Op1Op2Op1 And Op2 TTT TFF FTF FFF Op1Op2Op1 Or Op2 TTT TFT FTT FFF Op1Not Op2 TF FT

Logical Operators  Examples:  (2 < 3) And (4 < 6)  Returns True  (2 6)  Returns False  (2 6)  Returns True  (2 > 3) Or (4 > 6)  Returns False  Not (2 > 3)  Returns True  Not (2 < 3)  Returns False

Boolean Data Type, Boolean Methods, and a Boolean Function  You have variables store the result of a condition by using the Boolean data type. Dim boolVar As Boolean  Boolean variables can take the values of True or False (both are keywords in VB)  There are a couple pre-defined helpful boolean string functions (Assume strVar and strVar2 are both string variables)  strVar.EndWith(strVar2)  Returns True is strVar ends with strVar2  strVar.StartsWith(strVar2)  Returns True is strVar starts with strVar2  isNumeric(strVar)  Returns True if strVar is a string of a number

If Statements  An if statement allows a program to decide on a course of action based on whether a condition is True or False  General Form If condition Then some code End If  If the condition is true then some code will execute, otherwise it won’t  There is also an if/else statement If condition Then some code Else some other code End If  If the condition is true then some code will execute, otherwise some other code will execute  After the End If, the program continues normally.

If Statement Example  New Topics:  Variable Scoping  Random Numbers  Rnd() function  Single Data Type  Can hold only half the numbers Double can  Event procedures calling other event procedures  Relational Operators  Logical Operators  If/Else Statement

Nested If Statements  You can put if statements in other if statements. This is called nesting. If condition Then If condition2 Then some code End If Else some different code End If  If condition is false some different code will be executed, if condition is true AND condition2 is true then some code will be executed.

Nested If Statement Example  New Topics:  Boolean Function: isNumeric  Nested If Statement

ElseIf Clauses  You can check for multiple conditions in the same if statement using ElseIf clauses. If condition Then some code ElseIf condition2 Then some other code ElseIf condition3 Then yet some more code Else even more code End If  some code will execute if condition is true, some other code will execute if condition2 is true, yet some more code will execute if condition3 is true, and even more code will execute if none of the above are true.

ElseIf Clause Example  New Topic:  Generating Random Integers  ElseIf Clauses