Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the.

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
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
Compound Operators 03/07/11. More Operators, First Section 4.4.
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.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
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.
C# Introduction ISYS 350. Visual Studio 2012 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
C# Introduction ISYS 350. Visual Studio 2013 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Operators. Today’s Learning Goals … Understand the purpose of JavaScript operators Explore the mathematical operators Find out about the assignment operators.
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.
Divide. Evaluate power – 3 = – 3 EXAMPLE – 3 = 3 2 – – 3 = 6 – 3 Multiply. Evaluate expressions Multiply and divide from.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
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.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
C# Introduction ISYS 350. Visual Studio 2012 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
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.
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Nahla Abuel-ola / WIT.
Expression ISYS 350.
By: Muhammad Zidny Naf’an
Learning About the Loop Structure (continued)
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Structure of a C Program
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
Work with Data and Decision Structure
Expression ISYS 350.
Expression ISYS 350.
Operator Overloading.
Conversion Check your class notes and given examples at class.
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Loops ISYS 350.
Expression ISYS 350.
Operators In Java Programming By Rajanikanth B.
Chapter 4: Expression and Operator
Expression ISYS 350.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Operator King Saud University
HNDIT11034 More Operators.
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.
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Presentation transcript:

Expression ISYS 350

Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the operatorDescription +AdditionAdds two numbers -SubtractionSubtracts one number from another *MultiplicationMultiplies one number by another /DivisionDivides one number by another and gives the quotient %ModulusDivides 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 OperatorMultiples 1 ( )Inner to outer, left to right 2. Powerleft to right 3. *, /left to right 4. +, -left to right

Formula to Expression

Increment/Decrement Operators ++IncrementAdds 1 to the operand (x = x + 1). -- DecrementSubtracts 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 Operator += 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) { int myInt; myInt = int.Parse(textBox1.Text); //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) { int stepValue; stepValue = int.Parse(textBox2.Text); int myInt; myInt = int.Parse(textBox1.Text); myInt += stepValue; // myInt = myInt + stepValue; textBox1.Text = myInt.ToString(); }

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 int Counter = 0; private void button1_Click(object sender, EventArgs e) { Counter = Counter + 1; textBox1.Text = Counter.ToString(); } private void button1_Click(object sender, EventArgs e) { int Counter = 0; Counter = Counter + 1; textBox1.Text = Counter.ToString(); } Incorrect: Correct:

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.