23 March, 2000 CS1001 Lecture 6 Logical Expressions Logical Operators Selective Execution Web page --

Slides:



Advertisements
Similar presentations
Chapter 3 Program Design And Branching Structures.
Advertisements

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
Making Decisions In Python
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
C++ for Engineers and Scientists Third Edition
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 © 2000 John Urrutia. All rights reserved. Qbasic Looping Statements & Formatted Output.
Conditional Control Flow Constructs. Sequential Control Flow Execution order follows the textual order straight line flow Many simple problems can not.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
Flow of Control Part 1: Selection
Logic Disjunction A disjunction is a compound statement formed by combining two simple sentences using the word “OR”. A disjunction is true when at.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy.
Thinking Mathematically
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.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Quadratic Equations. PROGRAM QuadraticEquations_1 IMPLICIT NONE REAL A, B, C, Discriminant, Root_1, Root_2 ! Get the coefficients PRINT *, "Enter the.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 Type Called bool  Bool has only two possible values: True and False.
Lecture 3 Selection Statements
AND.
Sequence, Selection, Iteration The IF Statement
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Selection—Making Decisions
Classwork/Homework Classwork – Page 90 (11 – 49 odd)
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
And now for something completely different . . .
Chapter 2 Programming Basics.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
The Selection Structure
Presentation transcript:

23 March, 2000 CS1001 Lecture 6 Logical Expressions Logical Operators Selective Execution Web page --

23 March, 2000 Program logical flow In structured program, three basic control structures: sequenceselection(Ch 3)repetition (Ch 4) Statement sequence1 Logical- expression Logical- expression Statement sequence2 Statement sequence2 Statement sequence2 Statement sequence1 Statement sequence2 Statement sequence3

23 March, 2000 Logical Expressions Logical- expression Statement sequence1 Statement sequence2 Statement sequence3 Control structures Simple or compound Constructs: IF IF-ELSE IF CASE

23 March, 2000 Simple Logical Expressions Logical constants or variables Form: expression1 relational-operator expression2 where expression1 and expression2 can be –numeric 5.2, 27, –Character ‘open’ ‘close’ –Logical.TRUE.

23 March, 2000 Relational Operators Operators that show relationship between the two variables.LT.<is less than.GT.>is greater than.EQ.==is equal to.LE.<=is less than or equal to.GE.>=is greater than or equal to.NE./=is not equal to

23 March, 2000 Examples 5.2 <=27 result is.TRUE. (B**2) >= (2.0*A*C) Relationship between character strings –depends on internal representation of the characters (see Appendix A of text for ASCII coding) –‘A’, ‘B’, …, ‘Z’ are 65,66,…,90 –‘a’,’b’,…’z’ are 97,98,…,122 –‘A’ ‘6’

23 March, 2000 Compound Logical Expressions Two logical expressions combined with a logical operator expression 1 logical-operator expression 2 expression 1 and expression 2 are logical expressions

23 March, 2000 Logical Operators.NOT..AND..OR..EQV..NEQV. Logical negation Conjunction, true only if both are true Disjunction, true if one or both are true Equivalence, true if both true or both false Nonequivalence, true if one is true and the other is false..NOT. Q P.AND. Q P.OR Q P.EQV. Q P.NEQV. Q

23 March, 2000 Truth Tables for NOT, AND, & OR PQP.AND. QP.OR. Q.TRUE..TRUE..TRUE..TRUE..TRUE..FALSE..FALSE..TRUE..FALSE..TRUE..FALSE..TRUE..FALSE..FALSE. Q.NOT. Q.TRUE..FALSE..FALSE..TRUE.

23 March, 2000 Truth Table for EQV & NEQV PQP.EQV. Q P.NEQV. Q.TRUE..TRUE..TRUE..FALSE..TRUE..FALSE..FALSE..TRUE..FALSE..TRUE..FALSE..TRUE..FALSE..FALSE..TRUE..FALSE.

23 March, 2000 Precedence Rule Arithmetic operations (and functions) Relational operations Logical operations in the order.NOT.,.AND.,.OR.,.EQV. (or.NEQV.)

23 March, 2000 Examples N = = 5.OR. N = = 10.NOT. ( (M Y) ) suppose M=3, N=4, X=10, Y=14, and Z=4:.NOT. ( (3 14)).NOT. (.TRUE..AND. (14 > 14) ).NOT. (.TRUE..AND..FALSE.).NOT. (.FALSE.).TRUE.

23 March, 2000 Selective Execution Simple IF statement Logical IF statement General IF and ELSE construct Nested IF statements Compound IF and ELSE IF construct Case Next Week

23 March, 2000 Simple IF statement IF (logical-expression) then Executes procedure if TRUE Skips procedure if FALSE Ends with END IF TRUE FALSE

23 March, 2000 Examples IF (Discriminant < 0) THEN PRINT *, "Discriminant is", Discriminant PRINT *, ”There are two complex roots & &to this equation” END IF ! Calculate area and perimeter of circle IF (Figure == “C”) THEN Area = Pi*Length**2 Perimeter=2.0*Pi*Length Print *, “For Circle, Area = “, Area, & “ Perimeter = “, Perimeter END IF

23 March, 2000 Logical IF Statement IF (logical-expression) statement No THEN or ENDIF e.g., IF (X>=0) PRINT *, X

23 March, 2000 General IF and ELSE IF (logical- expression) THEN Executes procedure 1 if true ELSE it executes procedure 2 if false Ends with END IF Begin IF Procedure1 Procedure2 THEN ELSE END IF TRUE FALSE

23 March, 2000 ! Check if discriminant is nonnegative. If ! it is, calculate and display the roots. ! Otherwise display the value of the ! Discriminant and a no-real-roots message. IF (Discriminant >= 0) THEN Discriminant = SQRT(Discriminant) Root_1 = (-B + Discriminant) / (2.0 * A) Root_2 = (-B - Discriminant) / (2.0 * A) PRINT *, "The roots are", Root_1, Root_2 ELSE PRINT *, "Discriminant is", Discriminant PRINT *, "There are no real roots" END IF END PROGRAM QuadraticEquations_1