Top Banner
Arithmetic and Logical Expressions
43

July_Lecture 2 _Arithmetic and Logical Expressions

Jan 12, 2016

Download

Documents

zcscasc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: July_Lecture 2 _Arithmetic and Logical Expressions

Arithmetic and Logical Expressions

Page 2: July_Lecture 2 _Arithmetic and Logical Expressions

PROGRAMS USING ARITHMETIC EXPRESSIONS

Page 3: July_Lecture 2 _Arithmetic and Logical Expressions

/* My 3rd Program – printing values*/

#include <stdio.h>int main(void) {int x, y, sum; x = 20; y = 30; sum = x+y;

printf("The result is %d \n”, sum );

return 0;}

Page 4: July_Lecture 2 _Arithmetic and Logical Expressions

The syntax of an assignment statement is that• L.H.S. of equal sign contains a variable• R.H.S. contains an arithmetic expression• it is followed by a semicolon.

Execution:1) The value of the arithmetic expression on the right is

determined using the current values of the variables inthis expression.

2) This value is then stored in the variable on L.H.S.

Page 5: July_Lecture 2 _Arithmetic and Logical Expressions

Assignment Statements in C

x = 20;x is assigned the value 20

y = 30; y is assigned the value 30

Page 6: July_Lecture 2 _Arithmetic and Logical Expressions

Assignment Statements in C

x = 20;x is assigned the value 20

y = 30; y is assigned the value 30

sum = x+y;sum is assigned the value of x+y

x = x + 5;x is assigned the value of x+5 (25 in this case)

Page 7: July_Lecture 2 _Arithmetic and Logical Expressions

/* My 3rd Program – printing values*/

#include <stdio.h>int main(void) {int x, y, sum; // declare type of variablesx = 20; y = 30; sum = x+y;

printf("The result is %d\n", sum);

return 0;}

The result is 50

Page 8: July_Lecture 2 _Arithmetic and Logical Expressions

/* My 4th Program */

#include <stdio.h>int main(void) {int x, y, sum; x = 20; y = 30; sum = x+y;

printf("The sum of %d and %d is %d\n", x, y, sum);

return 0;}

The sum of 20 and 30 is 50

Page 9: July_Lecture 2 _Arithmetic and Logical Expressions

/* My 5th Program – getting values from the user */

#include <stdio.h>int main(void) {int x, y, sum; scanf( “%d %d” ,&x, &y) ;sum = x+y;printf("The sum of %d and %d is %d\n”, x,

y, sum);

return 0;}

Page 10: July_Lecture 2 _Arithmetic and Logical Expressions

/* A better way to get values from the user */

#include <stdio.h>int main(void) {int x, y, sum; scanf( “x= %d, y= %d” ,&x, &y) ;sum = x+y;printf("The sum of %d and %d is %d\n”, x,

y, sum);

return 0;}

Page 11: July_Lecture 2 _Arithmetic and Logical Expressions

Your first lab session

• Learn about basic UNIX commands • Learn about basic editor commands• Run a small C program to print your name and IIT experience.

Page 12: July_Lecture 2 _Arithmetic and Logical Expressions

Running a program

• 1) Type in your program– If there are errors, Edit your program

• 2) Compile your program– If compilation errors, go to step 1

• 3) Execute your program– If execution errors, go to step 1

• 4) If you are not satisfied with your results,– Go to step 1

Page 13: July_Lecture 2 _Arithmetic and Logical Expressions

To convert your program into machine understandable  form , use a compiler, like gcc.

Let the program be saved in My.c

> gcc My.c>

If there are no errors, then you can run the compiled  program using

> a.out

Page 14: July_Lecture 2 _Arithmetic and Logical Expressions

A different way to Compile and Run

If there are many programs, then each program can be    compiled separately into its own executable form

Let there be two programs test1.c & test2.c

Compiling first program> gcc –o test1.out test1.c 

Compiling second program> gcc –o test2.out test2.c 

Now, any program can be run by their executables.

To run the first program > test1.out

Page 15: July_Lecture 2 _Arithmetic and Logical Expressions

Declaring variables

• int a;• int year, month, group_number;• int y, m, day;  • int a, b, c, d (Not preferred)• int  Num_feet_in_mile; (too long)• int f_mile

Page 16: July_Lecture 2 _Arithmetic and Logical Expressions

initializing variables

• A good practice is to initialize the variables to zero if no other values have been specified,

• Otherwise, the computer will assign a garbage value to this variable.

• int  fmile;• fmile = 0;

0

Page 17: July_Lecture 2 _Arithmetic and Logical Expressions

declare and initialize

• These two things can be done in one step.•• int fmile = 0;

Page 18: July_Lecture 2 _Arithmetic and Logical Expressions

/* Computes the number of feet in a mile */

#include <stdio.h>int main(void) {int fmile=0, ymile;int fyard;

ymile = 1760;fyard = 3;fmile = ymile * fyard;printf(“ 1 Mile = %d Feet.\n", fmile);return 0;

}

Page 19: July_Lecture 2 _Arithmetic and Logical Expressions

/* Division operation in C */

#include <stdio.h>int main(void) {int b=10, d=4, m;float c, r;m =b/d;c = b/d;

printf(“m= %d, c= %f, \n", m,c);return 0;

}

m= 2, c= 2.0,

Page 20: July_Lecture 2 _Arithmetic and Logical Expressions

/* Division operation in C */

#include <stdio.h>int main(void) {int b=10, d=4, m;float c, r;m =b/d;c = b/d;r = b*1.0/ d;

printf(“m= %d, c= %f, r= %f \n", m,c,r);return 0;

}

m= 2, c= 2.0, r=2.5

Page 21: July_Lecture 2 _Arithmetic and Logical Expressions

Accuracy of results

(1 /7.0) * 7.0 = ?

Page 22: July_Lecture 2 _Arithmetic and Logical Expressions

Accuracy of results

(1 /7.0) * 7.0 = ?Do not be surprised if computer does not print the result of the expression as 1.

Page 23: July_Lecture 2 _Arithmetic and Logical Expressions

Accuracy of results

(1 /7.0) * 7.0 = ?Do not be surprised if computer does not print the result of the expression as 1.

1/7.0 = 0.14285714285714285….(0.14285714285714285….) * 7.0 = 1.0

Page 24: July_Lecture 2 _Arithmetic and Logical Expressions

Accuracy of results

(1 /7.0) * 7.0 = ?Do not be surprised if computer does not print the result of the expression as 1.

1/7.0 = 0.14285714285714285….(0.14285714285714285….) * 7.0 = 1.0

(0.14286) * 7.0 = 1.00002

Computer has finite precision. floating-point values are rounded to the number of significant digits permissible

Page 25: July_Lecture 2 _Arithmetic and Logical Expressions

The MODULUS operator

The modulus oprator is used to find the remainder part in an integer division.

a = 38b = 7a/b = 5a % b = 3

Page 26: July_Lecture 2 _Arithmetic and Logical Expressions

26

Operator Precedence

• In decreasing order of priority1. Parentheses ::  ( )2. Unary minus ::  –53. Multiplication, Division, and Modulus4. Addition and Subtraction

• For operators of the same priority, evaluation is from left to right as they appear

• Parenthesis may be used to change the precedence of operator evaluation

Page 27: July_Lecture 2 _Arithmetic and Logical Expressions

27

Examples of Arithmetic expressions

a + b * c – d / e       a + (b * c) – (d / e)

a *  b + d % e – f   (a *  b) + (d % e) – f

a – b + c + d           (((a – b) + c) + d)

x * y * z                  ((x * y) * z)

b / d * c      (b / d) * c

Page 28: July_Lecture 2 _Arithmetic and Logical Expressions

28

assigning one value to multiple variables

• Several variables can be assigned the same value using multiple assignment operators

a = b = c = 5;speed = flow = 0.0;

• Easy to understand if you remember that – the assignment expression has a value– Multiple assignment operators are right‐to‐left associative

Page 29: July_Lecture 2 _Arithmetic and Logical Expressions

29

“Types” of values on LHS and RHS of arithmetic expressions

• Usually should be the same• If not, the type of the RHS value will be internally converted to the type of the LHS value, and then assigned to it

• Example:float a;a = 2*3;

RHS type is int and the value is 6LHS type  is float, so stores it as 6.0

Page 30: July_Lecture 2 _Arithmetic and Logical Expressions

LOGICAL  EXPRESSIONS

Page 31: July_Lecture 2 _Arithmetic and Logical Expressions

31

Logical Expressions

• Uses relational and logical operators in addition to arithmetic ones

• Informally, specifies a condition which can be true or false

• Evaluates to value 0 or 1– 0 implies the condition is false– 1 implies the condition is true

Page 32: July_Lecture 2 _Arithmetic and Logical Expressions

32

Logical StatementsIf count is less than 100 then it is True

If count exactly equals 100 then it is True

If count is not equal to 100 then it is True

If marks are 80 or more but less than 90 then it is true 

if balance is more than 5000 OR transactions are more than 25 then it is true.

Page 33: July_Lecture 2 _Arithmetic and Logical Expressions

33

Relational Operators

• Used  to compare two quantities.

< is less than

>  is greater than

<= is less than or equal to

>= is greater than or equal to

== is equal to

!= is not equal to 

Page 34: July_Lecture 2 _Arithmetic and Logical Expressions

34

Examples

10 > 20  is false, so value is 025 < 35.5 is true, so value is 112 > (7 + 5) is false, so value is 032 != 21         is true, so value is 1

• When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compareda +  b > c   – d    is the same as   (a+b) > (c+d)

Page 35: July_Lecture 2 _Arithmetic and Logical Expressions

35

Logical Expressions(count < 100)

If count is less than 100 then it is True

(count == 100)

If count exactly equals 100 then it is True

(count != 100)

If count is not equal to 100 then it is True

(count <= 100)

If count is up to 100 then it is True

Page 36: July_Lecture 2 _Arithmetic and Logical Expressions

36

Logical Operators

– Logical AND (&&)• Evalutes to 1 if both the operands are non‐zero

– Logical OR (||)• Result is true if at least one of the operands is non‐zero

X Y X && Y X | | Y0 0 0 00 non-0 0 non-0

non-0 0 0 non-0non-0 non-0 non-0 non-0

Page 37: July_Lecture 2 _Arithmetic and Logical Expressions

37

Logical Expressions((math+phys+chem)/3 >= 60)

if average is equal to or greater than 60 then it is true

((marks >= 80) && (marks < 90))

If marks are 80 or more but less than 90 then it is true 

((balance > 5000) | | (no_of_trans > 25))

if balance is more than 5000 OR transactions are more than 25 then it is true.

Page 38: July_Lecture 2 _Arithmetic and Logical Expressions

38

negation operator 

• Unary negation operator (!)– Single operand– Value is 0 if operand is non‐zero– Value is 1 if operand is 0– example:

a = 80,  b =70(a+b >130)  evaluates to True if sum a+b exceeds 130!(a+b >130)  evaluates to true if a+b is less than or equal to 130

Page 39: July_Lecture 2 _Arithmetic and Logical Expressions

39

Example

• (4 > 3) && (100 != 200)– 4 > 3 is true, so value 1– 100 != 200 is true so value 1– Both operands  are 1 , so final value 1 (True)

• (!10) && (10 + 20 != 200)– first operand10 is non‐0, so value !10 is 0– second operand  30 != 200 is true so value 1– Since both operands are NOT 1 so final value 0

• (!10) || (10 + 20 != 200)– Same as above, but since one of the operand has value non‐0, so final value 1

Page 40: July_Lecture 2 _Arithmetic and Logical Expressions

40

Example

• (4 > 3) && (100 != 200)– 4 > 3 is true, so value 1– 100 != 200 is true so value 1– Both operands  are 1 , so final value 1 (True)

• (!10) && (10 + 20 != 200)– first operand10 is non‐0, so value !10 is 0– second operand  30 != 200 is true so value 1– Since both operands are NOT 1 so final value 0

• (!10) || (10 + 20 != 200)– Same as above, but since one of the operand has value non‐0, so final value 1

Page 41: July_Lecture 2 _Arithmetic and Logical Expressions

41

Example

• (4 > 3) && (100 != 200)– 4 > 3 is true, so value 1– 100 != 200 is true so value 1– Both operands  are 1 , so final value 1 (True)

• (!10) && (10 + 20 != 200)– first operand10 is non‐0, so value !10 is 0– second operand  30 != 200 is true so value 1– Since both operands are NOT 1 so final value 0

• (!10) || (10 + 20 != 200)– Same as above, but since one of the operand has value non‐0, so final value 1

Page 42: July_Lecture 2 _Arithmetic and Logical Expressions

42

Be careful with mixed arithmetic and logical assignments !

• a = 3 && b = 4No parenthesis, so need to look at precedence and associativity

“=“ has higher precedence than &&b=4 is an assignment expression, evaluates to 4 a = 3 is an assignment expression, evaluates to 3 Both operands of && are non‐0, so final value of the logical expression is 1

• Note that changing to b = 0 would have made the final value 0

Page 43: July_Lecture 2 _Arithmetic and Logical Expressions

43

Be careful with mixed arithmetic and logical assignments !

• a = 3 && b = 4No parenthesis, so need to look at precedence and associativity

= has higher precedence than &&b=4 is an assignment expression, evaluates to 4 a = 3 is an assignment expression, evaluates to 3 Both operands of && are non‐0, so final value of the logical expression is 1

• Note that changing to b = 0 would have made the final value 0