LESSON 3 Expressions, Statements, & Operators. STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Conditionals – if - else Dr. Sajib Datta
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
Expressions and statements Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
JavaScript, Third Edition
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
Operators and Expressions
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Decision Structures and Boolean Logic
2440: 211 Interactive Web Programming Expressions & Operators.
Computer Science Selection Structures.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
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.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
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.
Operators & Expressions
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 Type Called bool  Bool has only two possible values: True and False.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Operators & Expressions
Operators And Expressions
University of Central Florida COP 3330 Object Oriented Programming
Sequence, Selection, Iteration The IF Statement
University of Central Florida COP 3330 Object Oriented Programming
JavaScript: Control Statements I
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Chapter 8 JavaScript: Control Statements, Part 2
Summary Two basic concepts: variables and assignments Basic types:
Code Refresher Test #1 Topics:
Expressions.
Chapter 4: Expression and Operator
Using C++ Arithmetic Operators and Control Structures
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

LESSON 3 Expressions, Statements, & Operators

STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null statement). All statements end with a semicolon Even the null statement, which is just the semicolon and nothing else.

BLOCK & COMPOUND STATEMENTS Block Any place you can put a single statement, you can put a compound statement A block Begins with an opening brace({) Ends with a closing brace (}). Although every statement in the block must end with a semicolon, the block itself does not end with a semicolon.

EXPRESSIONS Expression Anything that evaluates to a value. Expressions are said to return a value. All expressions are statement.

Operators Operator => requires an action to be taken. Arithmetic Operator Logical operator Relational operator

OperatorSymbolExample ArithmeticOperationResultant Addition Subtraction Multiplication*8.0 * Division/9.0 / 4.0 9/ Modulo division%9 % 41 Relational Equal to=5 = 7FALSE Less than<5 < 7TRUE Greater than>5 > 7FALSE Less than or equal to<=5<=7 6<=6 TRUE Greater than or equal to>=5 >= 7FALSE Equal to= 5 = = 7FALSE Not equal to!=5 != 7TRUE  Used for conditional statement, e.g. selection & looping statements. Logical Not !NOT TRUEFALSE And&&TRUE AND TRUETRUE Or||TRUE OR FALSETRUE  Used for joining few conditional statements together.  Output/result is in Boolean type (True/False) Higher priority

NOT A ! A TF FT ANDOR ABA && BABA || B TTTTTT TFFTFT FTFFTT FFFFFF

Rules of (arithmetic) operator precedence Operator(s)Operation(s)Order of evaluation (precedence) ( )ParenthesesEvaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level”, they are evaluated left to right. *, /, or %Multiplication, Division, Modulus Evaluated second. If they are several, evaluated left to right. + or -Addition, SubtractionEvaluated last. If there are several, they are evaluated left to right.

Example

Compound Statements/ Operators Assignment operatorsExampleExpression += x+=yx = x +y - = x - =yx = x – y *= x *= yx = x * y / = x /= yx = x / y %= x %= yx = x % y

Increment and Decrement operators OperatorExpressionExplanation ++++aIncrement a by 1 then use the new value of a in the expression in which a resides. ++a++Use current value of a in the expression in which a resides, then increment a by bDecrement a by 1 then use the new value of b in the expression in which b resides. --b--Use current value of b then increment b by 1.

PostFix Expression Postfix increment: a++  a = a + 1 Postfix decrement: a--  a = a – 1 Example: a = 10; a++; a = 10; a--; b = 5; a = 10; c = b + a++; b = 5; a = 10; c = b + a--; x = 10; z = x ++; z = x--;

Example

Prefix Expressions Prefix Expression Prefix expression: ++a  a = a + 1 Prefix expression: --a  a = a –1 Example: a = 10; ++a; a = 10; --a; b = 5; a = 10; c = b + ++a; b = 5; a = 10; c = b + --a; x = 10; z = ++x ; z = --x;

Example

ASCII Table

Example

Hierarchy of Operations Order of OperationsOperand Data TypeResultant Data Type ( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy. 1. Functions Mathematical Operators 2. *, /, %Numeric 3. +, -Numeric Relational Operators 4. ==,, =, !=Numeric or String or Character Logical Logical Operators (nonzero is TRUE, zero is FALSE) 5. NOT, !Logical 6. AND, &&Logical 7. OR, ||Logical Assignment Operator 8. =Numeric or Character or Logical

Additional Exercise Assume that: x = 26 y = 4 z = 7 Decide if the returned value will be 1 (true) or 0 (false) x=6 == x%y*3-y/z !(x+y<z) Decide if the return result, and changes to each variables y*++z-x y*z+++x