Search PPTs

Tuesday, August 6, 2013

PPT On Arithmetic Operators


Download

 Arithmetic Operators Presentation Transcript:
1.OBJECTIVES
In this Lecture you will learn:
To use arithmetic operators.
The precedence of arithmetic operators.
To write decision-making statements.
To use relational and equality operators.

2.Arithmetic
Arithmetic calculations used in most programs
Usage
* for multiplication
/ for division
% for remainder
+, -
Integer division truncates remainder
7 / 5 evaluates to 1
Remainder operator % returns the remainder
7 % 5 evaluates to 2

3.Operator precedence
Some arithmetic operators act before others (i.e., multiplication before addition)
Use parenthesis when needed

4.Precedence of arithmetic operators.

5.Good Programming Practice
Using parentheses for complex arithmetic expressions, even when the parentheses are not necessary, can make the arithmetic expressions easier to read.

6.Solve Out Following Expression ? Step by Step

7.Decision Making: Equality and Relational Operators
Condition
Expression can be either true or false
if statement
Simple version in this section, more detail later
If a condition is true, then the body of the if statement executed
Conditions in if statements can be formed using equality or relational operators (next slide)

8.Decision Making: Equality and Relational Operators
Line 6: begins class Comparison declaration
Line 12: declares Scanner variable input and assigns it a Scanner that inputs data from the standard input
Lines 14-15: declare int variables
Lines 17-18: prompt the user to enter the first integer and input the value
Lines 20-21: prompt the user to enter the second integer and input the value

9.if statement to test for equality using (==)
If variables equal (condition true)
Line 24 executes
If variables not equal, statement skipped
No semicolon at the end of line 23
Empty statement
No task is performed
Lines 26-27, 29-30, 32-33, 35-36 and 38-39
Compare number1 and number2 with the operators !=, <, >, <= and >=, respectively

10.Common Programming Error
Forgetting the left and/or right parentheses for the condition in an if statement is a syntax error—the parentheses are required.

11.Confusing the equality operator, ==, with the assignment operator, =, can cause a logic error or a syntax error. The equality operator should be read as “is equal to,” and the assignment operator should be read as “gets” or “gets the value of.” To avoid confusion, some people read the equality operator as “double equals” or “equals equals.”

12.It is a syntax error if the operators ==, !=, >= and <= contain spaces between their symbols, as in = =, ! =, > = and < =, respectively.

13.Indent an if statement’s body to make it stand out and to enhance program readability.

14.Place only one statement per line in a program. This format enhances program readability.

15.Placing a semicolon immediately after the right parenthesis of the condition in an if statement is normally a logic error.

No comments:

Related Posts Plugin for WordPress, Blogger...

Blog Archive