Expression ISYS 350.

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

Arithmetic Calculations
Java Planning our Programs Flowcharts Arithmetic Operators.
CS150 Introduction to Computer Science 1
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Operators and Expressions
Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the.
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.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
LESSON 6 – Arithmetic Operators
ORDER OF OPERATIONS x 2 Evaluate the following arithmetic expression: x 2 Each student interpreted the problem differently, resulting in.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Expression and Decision Structure ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators.
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.
Order of Operations - rules for arithmetic and algebra that describe what sequence to follow to evaluate an expression involving more than one operation.
Divide. Evaluate power – 3 = – 3 EXAMPLE – 3 = 3 2 – – 3 = 6 – 3 Multiply. Evaluate expressions Multiply and divide from.
1-2 Order of Operations and Evaluating Expressions.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
Notes 2.1 Order of Operations PEMDAS If an expression has only numbers and symbols, it is a numerical expression. If it has a variable it is a variable.
What is 10-5? Correct! Click on the picture below to answer another question!
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Choose a category. You will be given the answer. You must give the correct question. Click to begin.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
CSE 220 – C Programming Expressions.
Expression ISYS 350.
Lecture 3 Java Operators.
Side Effect Operators Changing the value of variables
University of Central Florida COP 3330 Object Oriented Programming
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
Math & Exponents.
University of Central Florida COP 3330 Object Oriented Programming
Nahla Abuel-ola / WIT.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Expression ISYS 350.
By: Muhammad Zidny Naf’an
Learning About the Loop Structure (continued)
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Work with Data and Decision Structure
Expression ISYS 350.
Expression ISYS 350.
Conversion Check your class notes and given examples at class.
You replace it with its simplest name
Data Types and Expressions
Loops ISYS 350.
Expression ISYS 350.
Operators In Java Programming By Rajanikanth B.
Chapter 4: Expression and Operator
OBJECT ORIENTED PROGRAMMING I LECTURE 4 PART 2 GEORGE KOUTSOGIANNAKIS
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Multiply and divide Expressions
Operator and Expression
Operator King Saud University
AB form1 Multiplication textBox1 12 textBox2 34 textBox3
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Order of Operations  + - X.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Expression ISYS 350

Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators Operator Name of the operator Description + Addition Adds two numbers - Subtraction Subtracts one number from another * Multiplication Multiplies one number by another / Division Divides one number by another and gives the quotient % Modulus Divides one number by another and gives the remainder Other calculations: Use Math class’s methods. Example: Math.Pow(x, y) double x = 3.0, y = 2.0, z; z=Math.Pow(x,y);

Order of Evaluation Operator Multiples 1 ( ) Inner to outer, left to right 2. Power left to right 3. *, / left to right 4. +, - left to right

Formula to Expression

Increment/Decrement Operators ++ Increment Adds 1 to the operand (x = x + 1). -- Decrement Subtracts 1 from the operand (x = x - 1). int x = 14; int y = 8; Int z = 10; int result7 = --y; // result7 = 7 int result8 = ++x; // result8 = 15, x = 15 ++z; // z= 11 double a = 8.5; double b = 3.4; double result15 = --a; // result15 = 7.5 double result16 = ++b; // result16 = 4.4

Compound (Shortcut) Operators += Adding the operand to the starting value of the variable. -= Subtracting the operand from the starting value of the variable. *= Multiplying the operand by the starting value of the variable. /= Dividing the operand by the starting value of the variable. %= Remainder after dividing the right operand by the value in the variable.

Decrement/Increment by 1 private void button1_Click(object sender, EventArgs e) { int myInt; myInt = int.Parse(textBox1.Text); // myInt-=1; // myInt = --myInt; --myInt; textBox1.Text = myInt.ToString(); } private void button2_Click(object sender, EventArgs e) //myInt += 1; //myInt = ++myInt; ++myInt; textBox1.Text = myInt.ToString(); }

Decrement/Increment by any Step Value private void button1_Click(object sender, EventArgs e) { int stepValue; stepValue = int.Parse(textBox2.Text); int myInt; myInt = int.Parse(textBox1.Text); myInt -= stepValue; // myInt = myInt - stepValue; textBox1.Text = myInt.ToString(); } private void button2_Click(object sender, EventArgs e) myInt += stepValue; // myInt = myInt + stepValue;

Prefix/Postfix Increment/Decrement Operators int a = 5; int b = 5; int y = ++a; // a = 6, y = 6 int z = b++; // b = 6, z = 5 •When you use an increment or decrement operator as a prefix to a variable, the variable is incremented or decremented and then the result is assigned. •When you use an increment or decrement operator as a postfix to a variable, the result is assigned and then the variable is incremented or decremented.

Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: int Counter=0; Need to increase the counter by 1 when the button is clicked: Counter = Counter + 1; // Or Counter+=1; // Or ++Counter; Question: Where to declare this variable?

Example Incorrect: private void button1_Click(object sender, EventArgs e) { int Counter = 0; Counter = Counter + 1; textBox1.Text = Counter.ToString(); } Correct: int Counter = 0; private void button1_Click(object sender, EventArgs e) { Counter = Counter + 1; textBox1.Text = Counter.ToString(); }

Variable Scope The scope of a variable determines its visibility to the rest of a program. Procedural-level scope: declared in a procedure and visible only in the procedure. Class-level scope: declared in a class but outside any procedure; visible to all procedures in the class.