Arithmetic and Logical Expressions
Arithmetic and Logical Expressions
PROGRAMS USING ARITHMETIC 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;}
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.
Assignment Statements in C
x = 20;x is assigned the value 20
y = 30; y is assigned the value 30
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)
/* 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
/* 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
/* 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;}
/* 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;}
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.
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
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
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
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
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
declare and initialize
• These two things can be done in one step.•• int fmile = 0;
/* 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;
}
/* 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,
/* 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
Accuracy of results
(1 /7.0) * 7.0 = ?
Accuracy of results
(1 /7.0) * 7.0 = ?Do not be surprised if computer does not print the result of the expression as 1.
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
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
The MODULUS operator
The modulus oprator is used to find the remainder part in an integer division.
a = 38b = 7a/b = 5a % b = 3
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
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
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
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
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
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.
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
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)
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
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
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.
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
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
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
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
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
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