Introduction to Computing
(CS 1109/1110)
http://jatinga.iitg.ac.in/~asahu/cs1109/
Relational Expression and
Floating point
A. Sahu
Dept of Comp. Sc. & Engg.
Indian Institute of Technology Guwahati 1
Outline
• Relational Expression and Evaluation
• Floating Point
• Floating Point Density
• Operations
• Type Casting
2
Algebra: BEDMAS/PEDMAS Rule
• B-E-DM-AS or P-E-DM-AS or B-O-DM-AS
• B/P : Bracket or Parenthesis ( )
– In C, only ( ) used for expression
– Curly braces {}, and square bracket [] used for
some other purpose.
– Again [] may involves in expression as in the form
of array access
• E : Exponentiation or Order (O)
• DM: Division and Multiplication
• AS : Addition and Subtraction
3
BEDMAS equivalent in C
Arithmetic Operators Precedence Rule
Operator(s) Precedence & Associativity
() Evaluated first. If nested
(embedded), innermost first.
* / % Evaluated second. If there are
several, evaluated left to right.
+ - Evaluated third. If there are
several, evaluated left to right.
= Evaluated last, right to left.
Assignment Operators
= += -= *= /= %=
Statement Equivalent Statement
a = a+2 ; a += 2 ;
a = a-3 ; a -= 3 ;
a = a*2 ; a *= 2 ;
a = a/4 ; a /= 4 ;
a = a%2 ; a %= 2 ;
b = b+(c+2); b += c + 2 ;
d =d*(e-5); d *= e - 5 ;
Practice with Assignment Operators
int i = 1, j = 2, k = 3, m = 4 ;
Expression Value
i += j + k
j *= k = m + 5
k -= m /= j * 2
Practice with Assignment Operators
int i = 1, j = 2, k = 3, m = 4 ;
Expression Value
i += j + k i=6
j *= k = m + 5 k=9, j=18
k -= m /= j * 2 m=1, k=2
Relational Expressions and
Evaluation
8
Relational Operators
Operator Meaning
< Less than
> Greater than
<= less than or equal to
>= greater than or equal to
== is equal to
!= is not equal to
Relational expressions evaluate to the integer
values 1 (true) or 0 (false).
All of these operators are called binary operators
because they take two expressions as operands.
Practice with Relational Expressions
int a = 1, b = 2, c = 3 ;
Expression Value Expression Value
a < c (a + b)>= c
b <= c (a + b)== c
c <= a a != b
a > b (a + b)!= c
b >= c
Practice with Relational Expressions
int a = 1, b = 2, c = 3 ;
Expression Value Expression Value
a < c T (a + b)>= c T
b <= c T (a + b)== c T
c <= a F a != b T
a > b F (a + b)!= c F
b >= c F
Arithmetic Expressions: True or False
• Arithmetic expressions evaluate to numeric
values.
• An arithmetic expression that has a value of
zero is false.
• An arithmetic expression that has a value
other than zero is true.
Practice with Arithmetic Expressions
int a = 1, b = 2, c = 3 ;
float x = 3.33, y = 6.66 ;
Expression Numeric Value True/False
a + b
b-2*a
c– b–a
c–a
y–x
y-2*x
Practice with Arithmetic Expressions
int a = 1, b = 2, c = 3 ;
float x = 3.33, y = 6.66 ;
Expression Numeric Value True/False
a + b 3 T
b-2*a 0 F
c– b–a 0 F
c–a 2 T
y–x 3.33 T
y-2*x 0.0 F
Structured Programming
• All programs can be written in terms of only
three control structures
– Sequence, selection and repetition
• The sequence structure
– Unless otherwise directed, the statements are
executed in the order in which they are written.
• The selection structure
– Used to choose among alternative courses of
action.
• The repetition structure
– Allows an action to be repeated while some
condition remains true.
Selection: the if statement
if ( condition )
{
statement(s) /* body of the if
statement */
}
The braces are not required if the body contains only a
single statement.
However, they are a good idea and are required by the
C Coding Standards.
Examples
if ( age >= 18 )
{
printf(“Vote!\n”) ;
}
if ( value == 0 )
{
printf(“You entered a zero.\n”) ;
printf (“Please try again.\n”) ;
}
Good Programming Practice
• Always place braces around the body of an if
statement.
• Advantages:
– Easier to read
– Will not forget to add the braces if you go back
and add a second statement to the body
– Less likely to make a semantic error
• Indent the body of the if statement 3 to 4
spaces -- be consistent!
Selection: the if-else statement
if ( condition )
{
statement(s)/*if clause */
}
else
{
statement(s) /*else clause */
}
Example
if ( age >= 18 )
{
printf(“Vote!\n”) ;
}
else
{
printf(“Maybe next time!\n”) ;
}
Nesting of if-else Statements
if ( condition1 )
{
statement(s)
}
else if ( condition2 )
{
statement(s)
}
. . . /* more else clauses may be here */
else
{
statement(s) /* the default case */
}
Good Example : 2 if 2 else
if ( value == 0 )
{
printf(“Value you entered was 0\n”);
}
else if ( value < 0 )
{
printf(“%d is negative.\n”, value) ;
}
else
{
printf(“%d is positive.\n”, value) ;
}
Bad Example : 2 if 1 else
if ( n > 0 )
if ( a > b )
z=a; if ( n > 0 )
else {
z=b; if (a> b)
z=a;
if ( n > 0 ) }
if ( a > b ) else
z=a; z=b;
else
z=b;
Indentation will not ensure result :
else match with closest if
Code of Red box behaves like Code of Green box
Gotcha! = versus ==
int a = 2 ;
if ( a = 1 )/* semantic(logic) Err! */
{
printf (“a is one\n”) ;
}
else if (a == 2 )
{
printf (“a is two\n”) ;
} else {
printf (“a is %d\n”, a) ;
}
Gotcha ..
• The statement if (a = 1) is syntactically correct, so
no error message will be produced. (Some compilers
will produce a warning.) However, a semantic (logic)
error will occur.
• An assignment expression has a value -- the value
being assigned. In this case the value being assigned
is 1, which is true.
• If the value being assigned was 0, then the expression
would evaluate to 0, which is false.
• This is a VERY common error. So, if your if-else
structure always executes the same, look for this
typographical error.
Logical Operators
• So far we have seen only simple conditions.
if ( count > 10 ) . . .
• Sometimes we need to test multiple conditions in
order to make a decision.
• Logical operators are used for combining simple
conditions to make complex conditions.
&& is AND if ( x > 5 && y < 6 )
|| is OR if ( z == 0 || x > 10 )
! is NOT if (! (bob > 42) )
Example Use of &&
if ( age < 1 && gender == ‘m’)
{
printf (“Infant boy\n”) ;
}
Truth Table for &&
Expression1 Expression2 Expression1 && Expression2
0 0 0
0 nonzero 0
nonzero 0 0
nonzero nonzero 1
Exp1 && Exp2 && … && Expn will evaluate to 1 (true) only
if ALL subconditions are true.
Truth Table for &&
Expression1 Expression2 Expression1 && Expression2
F F F
F T F
T F F
T T T
Exp1 && Exp2 && … && Expn will evaluate to 1 (true) only
if ALL subconditions are true.
Example Use of ||
if (grade==‘E’ || grade==‘F’)
{
printf(“See you next semester!\n”) ;
}
Truth Table for ||
Expression1 Expression2 Expression1 || Expression2
0 0 0
0 nonzero 1
nonzero 0 1
nonzero nonzero 1
Exp1 && Exp2 && … && Expn will evaluate to 1 (true) if
only ONE subcondition is true.
Truth Table for II
Expression1 Expression2 Expression1 II Expression2
F F F
F T T
T F T
T T T
Exp1 || Exp2 || … || Expn will evaluate to 1 (true) only if
any one subconditions are true.
Truth Table for !
Expression ! Expression
0 1
nonzero 0
E !E
T F
F T
Operator Precedence and Associativity
Precedence Associativity
() left to right/inside-out
++ -- ! + (unary) - (unary) (type) right to left
* / % left to right
+ (addition) - (subtraction) left to right
< <= > >= left ot right
== != left to right
&& left to right
|| left to right
= += -= *= /= %= right to left
, (comma) right to left
Floating Point Numbers
35
Floating Point Numbers
• Need to floating point number
• Number representation : IEEE 754
• Floating point range
• Floating point density
–Accuracy
• Arithmetic and Logical Operation on
FP
• Conversions and type casting in C
36
Need to go beyond integers
complex
• integer 7
• rational 5/8 real
• real √3 rational
• complex 2-3i integer
Extremely large and small values:
distance pluto - sun = 5.9 1012 m
mass of electron = 9.1 x 10-28 gm
Representing fractions
• Integer pairs (for rational numbers)
5 8 = 5/8
Strings with explicit decimal point
- 2 4 7 . 0 9
Implicit point at a fixed position
010011010110001011
Floating point implicit point
fraction x base power
Numbers with binary point
101.11 = 1x22 + 0x21 + 1x20 + . +1x2-1 + 1x2-2
= 4 + 1 + .+ 0.5 + 0.25 = 5.7510
0.6 = 0.10011001100110011001.....
.6 x 2 = 1 + .2
.2 x 2 = 0 + .4
.4 x 2 = 0 + .8
.8 x 2 = 1 + .6
Numeric Data Type
• char, short, int, long int
– char : 8 bit number (1 byte=1B)
– short: 16 bit number (2 byte)
– int : 32 bit number (4B)
– long int : 64 bit number (8B)
• float, double, long double
– float : 32 bit number (4B)
– double : 64 bit number (8B)
– long double : 128 bit number (16B)
40
Numeric Data Type
unsigned char
char
unsigned short
short
Unsigned int
int
41
Number in integer and log scale
• unsigned char C;
– 8 bit, 256 number
– Start from 0, 1, 2, 3, 4, …, 255 //Unit spaced
• Int A
– 32 bit numbers
– Start from 0, 1, 2, ,,,231
– Negative side: -1, -2, -3, …., -231 //Unit spaced
• Float f
– 32 bit
– Log scale
42
Number in integer and log scale
• Log Scale
– 10-5 10-4 10-3 10-2 10-1 100 101 102 103 104 105 106
• Number of integers :
– Between 100 and 101 = 9
– Between 101 and 102 = 90
– Between 102 and 103 = 900
– Between 103 and 104 = 9000
– Between 104 and 105 = 90000
– Between 105 and 106 = 900000
43
Numeric Data Type
• char, short, int, long int
– We have : Signed and unsigned version
– char (8 bit)
• char : -128 to 127, we have +0 and -0 ☺ ☺ Fun
• unsigned char: 0 to 255
– int : -231 to 231-1
– unsigned int : 0 to 232-1
• float, double, long double
– For fractional, real number data
– All these numbered are signed and get stored in
different format
44
Sign bit Numeric Data Type
Exponent Mantissa
float
Exponent Mantiss-1
Mantissa-2
double
45
Thanks
46