1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS 1541 -- Introduction to Computing for the Physical Sciences.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by.
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
CPSC 171 Introduction to Computer Science Boolean Logic, Gates, & Circuits.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
 In studying digital integrated circuits, one must start with the simplest group of circuit, the SSIs or Small Scale Integrated Circuits. Since these.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
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.
Islamic University Of Gaza, Nael Aburas Data Storage Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas
CPS120: Introduction to Computer Science Operations Lecture 9.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
CSC Programming for Science Lecture 8: Character Functions.
Control statements Mostafa Abdallah
Operators.
1 CS161 Introduction to Computer Science Topic #6.
CPS120: Introduction to Computer Science Decision Making in Programs.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Computer Science 210 Computer Organization
Section 7.1 Logical Operators
Control Statements: Part 2
EGR 2261 Unit 4 Control Structures I: Selection
Data Types, Identifiers, and Expressions
The Selection Structure
CS005 Introduction to Programming
Logical Operators & Truth Tables.
Data Types, Identifiers, and Expressions
Computers & Programming Languages
Relational Operators Operator Meaning < Less than > Greater than
Introduction to MATLAB
Computer Science 210 Computer Organization
Chapter-3 Operators.
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Introduction to Programming – 4 Operators
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Relational Operators.
Truth tables Mrs. Palmer.
Life is Full of Alternatives
Life is Full of Alternatives
Introduction to MATLAB
Selection—Making Decisions
Topics discussed in this section:
Chap 7. Advanced Control Statements in Java
Boolean Expressions September 1, 2019 ICS102: The course.
Presentation transcript:

1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences

Constants 2 Recall that variables are used to store values that might change Constants are values that cannot be changed at any time. Some constants that are pre-defined in MATLAB are: Constant namesMeaning/value pi π or … i j inf ∞ EECS Introduction to Computing for the Physical Sciences

Constants 3 What will be the final answer of the following expression? EECS Introduction to Computing for the Physical Sciences >> 2 * pi pi ans =

Random numbers 4 Several built-in functions in MATLAB to generate random numbers such as: The simplest built-in random function is “ rand ” EECS Introduction to Computing for the Physical Sciences rand - generate a random number between 0 and 1. randi(max) - generate a random integer: 1 ≤ x ≤ max. randi([min,max]) - generate a random integer: min ≤ x ≤ max. returns an n -by- n matrix of pseudorandom normal values

Random numbers 5 Example: Note that there is no input argument required for the “ rand ” function >> rand ans = Since “ rand ” returns a random real number between 0 and 1, how do we generate a random integer greater than or equal to 0 but less than 10 (i.e. 0 ≤ x < 10)? EECS Introduction to Computing for the Physical Sciences

Rounding functions 6 Rounding functions: fix - Round towards zero. floor - Round towards minus infinity. ceil - Round towards plus infinity. round - Round towards nearest integer. EECS Introduction to Computing for the Physical Sciences Example: >> fix(3.1415) ans = 3 Example: >> floor( ) ans = - 4

7 Rounding functions EECS Introduction to Computing for the Physical Sciences Example: >> ceil(3.1415) ans = 4 Example: >> round( ) ans = - 3 Rounding functions: fix - Round towards zero. floor - Round towards minus infinity. ceil - Round towards plus infinity. round - Round towards nearest integer.

Random numbers 8 Recall: how do we generate a random integer greater than or equal to 0 but less than 10 (i.e. 0 ≤ x < 10)? >> fix(rand*10) One method: We can combine the “ fix ” and “ rand ” functions rand*10 gives a random number between 0 and 10 fix rounds “down” the random number to an integer EECS Introduction to Computing for the Physical Sciences

Relational Expressions 9 Expressions that are conceptually either true or false are called relational expressions, or Boolean or logical expressions “true” is represented by the logical value 1, and “false” is represented by the logical value 0 EECS Introduction to Computing for the Physical Sciences

Relational Expressions: relational operators 10 The relational operators in MATLAB are: Operatorname > greater than >= greater than or equal to < less than <= less than or equal to == equal to ~= not equal to EECS Introduction to Computing for the Physical Sciences

11 Example: Relational Expressions: relational operators Example: >> 10 < ans = EECS Introduction to Computing for the Physical Sciences >> (10 < 8) - 5 ans = -5 0

12 Example: Relational Expressions: relational operators >> 9 > 8 > 7 > 6 ans = EECS Introduction to Computing for the Physical Sciences 0

Relational Expressions 13 Comparing characters (e.g. a, b, c) is also possible. Characters are compared using their ASCII equivalent value EECS Introduction to Computing for the Physical Sciences Example: >> ‘a’ < ‘d’ ans = 1

Relational Expressions: logical operators 14 The logical operators in MATLAB are: Operatorname || or && and ~ not The “or” logical operator will output a true value if either or both of the operands are true. The “and” logical operator will output a true value only if both of the operands are true. EECS Introduction to Computing for the Physical Sciences

Relational Expressions: logical operators 15 The || and && operators in MATLAB are also known as short- circuit operators. This means that if the result of the expression can be determined from the first part, then the second part will not even be evaluated. EECS Introduction to Computing for the Physical Sciences

16 Example: >> 3 8 ans = Relational Expressions: logical operators Example: >> 10 < && 3 < 8 ans = 1 0 EECS Introduction to Computing for the Physical Sciences

17 Example: >> (10 8 ans = Relational Expressions: logical operators Example: >> ‘b’ < ‘c’ - 1 && 3 < 8 ans = 1 0 EECS Introduction to Computing for the Physical Sciences

Relational Expressions: logical operators 18 Summary: Truth Table for logical operators: xy~xx || yx && y True FalseTrue False TrueFalse false truefalseFalse EECS Introduction to Computing for the Physical Sciences

19 One of the popular built-in functions in MATLAB is the “ plot ” function: Built-in Functions: Plotting functions >> help plot Plotting 2-D or 3-D graphs is a powerful function provided in MATLAB. EECS Introduction to Computing for the Physical Sciences We will look into more details about plotting graphs in MATLAB in Chapter 3 and Lab #2.