0% found this document useful (0 votes)
2 views7 pages

Ppsc Cdt7 Summary

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Department of CSE, U24CS104: Programming for Problem Solving

KITSW with C
1CSE-2 AY:2024-25
CDT7- LECTURE SUMMARY
CDT7 Arithmetic expressions
Topics Covered
Motivation These topics are used to understand how to declare and use
(Why you Arithmetic expressions, precedence of operators and associativity
(students)
should learn
these topics?)
Lecture Learning Outcomes (LLOs):After completion of this lecture, you should be
able to…
LLO1 Understand the declaration ofArithmetic expressions.
On topic 1
CDT7– Lecture Summary – Key Takeaways
C Expressions
An expression is a formula in which operands are linked to each other by the use of
operators to compute a value. An operand can be a function reference, a variable,
an array element or a constant.
Let's see an example:
a-b;
In the above expression, minus character (-) is an operator, and a, and b are the
two operands.
There are four types of expressions exist in C:
o Arithmetic expressions
o Relational expressions
o Logical expressions
o Conditional expressions
Each type of expression takes certain types of operands and uses a specific set of
operators. Evaluation of a particular expression produces a specific value.
For example:
x = 9/2 + a-b;
The entire above line is a statement, not an expression. The portion after the equal
is an expression.

Prepared by: Dr. P. Vijay Kumar, Dept of CSE, KITSW


Page 1 of 7
Department of CSE, U24CS104: Programming for Problem Solving
KITSW with C
1CSE-2 AY:2024-25
CDT7- LECTURE SUMMARY

Arithmetic Expressions
An arithmetic expression is an expression that consists of operands and arithmetic
operators. An arithmetic expression computes a value of type int, float or double.
When an expression contains only integral operands, then it is known as pure
integer expression when it contains only real operands, it is known as pure real
expression, and when it contains both integral and real operands, it is known as
mixed mode expression.
Evaluation of Arithmetic Expressions
The expressions are evaluated by performing one operation at a time. The
precedence and associativity of operators decide the order of the evaluation of
individual operations.
When individual operations are performed, the following cases can be
happened:
o When both the operands are of type integer, then arithmetic will be
performed, and the result of the operation would be an integer value. For
example, 3/2 will yield 1 not 1.5 as the fractional part is ignored.
o When both the operands are of type float, then arithmetic will be performed,
and the result of the operation would be a real value. For example, 2.0/2.0
will yield 1.0, not 1.
o If one operand is of type integer and another operand is of type real, then the
mixed arithmetic will be performed. In this case, the first operand is
converted into a real operand, and then arithmetic is performed to produce
the real value. For example, 6/2.0 will yield 3.0 as the first value of 6 is
converted into 6.0 and then arithmetic is performed to produce 3.0.
Let's understand through an example.
6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)

Evaluation of expression Description of each operation

6*2/( 2+1 * 2/3 +6) +8 * (8/4) An expression is given.

Prepared by: Dr. P. Vijay Kumar, Dept of CSE, KITSW


Page 2 of 7
Department of CSE, U24CS104: Programming for Problem Solving
KITSW with C
1CSE-2 AY:2024-25
CDT7- LECTURE SUMMARY
6*2/(2+2/3 + 6) + 8 * (8/4) 2 is multiplied by 1, giving value 2.

6*2/(2+0+6) + 8 * (8/4) 2 is divided by 3, giving value 0.

6*2/ 8+ 8 * (8/4) 2 is added to 6, giving value 8.

6*2/8 + 8 * 2 8 is divided by 4, giving value 2.

12/8 +8 * 2 6 is multiplied by 2, giving value 12.

1+8*2 12 is divided by 8, giving value 1.

1 + 16 8 is multiplied by 2, giving value 16.

17 1 is added to 16, giving value 17.

Relational Expressions
o A relational expression is an expression used to compare two operands.
o It is a condition which is used to decide whether the action should be taken or
not.
o In relational expressions, a numeric value cannot be compared with the string
value.
o The result of the relational expression can be either zero or non-zero value.
Here, the zero value is equivalent to a false and non-zero value is equivalent
to true.

Relational Description
Expression

x%2 = = 0 This condition is used to check whether the x is an even


number or not. The relational expression results in value
1 if x is an even number otherwise results in value 0.

a!=b It is used to check whether a is not equal to b. This


relational expression results in 1 if a is not equal to b
otherwise 0.

a+b = = x+y It is used to check whether the expression "a+b" is equal


to the expression "x+y".

a>=9 It is used to check whether the value of a is greater than

Prepared by: Dr. P. Vijay Kumar, Dept of CSE, KITSW


Page 3 of 7
Department of CSE, U24CS104: Programming for Problem Solving
KITSW with C
1CSE-2 AY:2024-25
CDT7- LECTURE SUMMARY
or equal to 9.

Let's see a simple example:


#include <stdio.h>
int main()
{
int x=4;
if(x%2==0)
{
printf("The number x is even");
}
else
printf("The number x is not even");
return 0;
}
Output

Logical Expressions
o A logical expression is an expression that computes either a zero or non-zero
value.
o It is a complex test condition to take a decision.
Let's see some example of the logical expressions.

Logical Description
Expressions

( x> 4 ) && ( x It is a test condition to check whether the x is greater than 4


<6) and x is less than 6. The result of the condition is true only
when both the conditions are true.

x > 10 || y It is a test condition used to check whether x is greater than


<11 10 or y is less than 11. The result of the test condition is true
if either of the conditions holds true value.

! ( x> 10 ) && It is a test condition used to check whether x is not greater


(y==2) than 10 and y is equal to 2. The result of the condition is true

Prepared by: Dr. P. Vijay Kumar, Dept of CSE, KITSW


Page 4 of 7
Department of CSE, U24CS104: Programming for Problem Solving
KITSW with C
1CSE-2 AY:2024-25
CDT7- LECTURE SUMMARY
if both the conditions are true.

Let's see a simple program of "&&" operator.


#include <stdio.h>
int main()
{
int x = 4;
int y = 10;
if ( (x <10) && (y>5))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Output

Let's see a simple example of "| |" operator


#include <stdio.h>
int main()
{
int x = 4;
int y = 9;
if ( (x <6) || (y>10))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Output

Prepared by: Dr. P. Vijay Kumar, Dept of CSE, KITSW


Page 5 of 7
Department of CSE, U24CS104: Programming for Problem Solving
KITSW with C
1CSE-2 AY:2024-25
CDT7- LECTURE SUMMARY

Conditional Expressions
o A conditional expression is an expression that returns 1 if the condition is
true otherwise 0.
o A conditional operator is also known as a ternary operator.
The Syntax of Conditional operator
Suppose exp1, exp2 and exp3 are three expressions.
exp1 ? exp2 : exp3
The above expression is a conditional expression which is evaluated on the basis of
the value of the exp1 expression. If the condition of the expression exp1 holds true,
then the final conditional expression is represented by exp2 otherwise represented
by exp3.
Let's understand through a simple example.
#include<stdio.h>
#include<string.h>
int main()
{
int age = 25;
char status;
status = (age>22) ? 'M': 'U';
if(status == 'M')
printf("Married");
else
printf("Unmarried");
return 0;
}
Output

1. LLP1(on LLO1):
Explain arithmetic expression and how an arithmetic expression is evaluated in C?
Additional LLPs: Practice problems for homework
2. LLP2.1(on LLO2):

Prepared by: Dr. P. Vijay Kumar, Dept of CSE, KITSW


Page 6 of 7
Department of CSE, U24CS104: Programming for Problem Solving
KITSW with C
1CSE-2 AY:2024-25
CDT7- LECTURE SUMMARY
Evaluate the following expression in C by applying operator precedence and
associativity.
i. X = (23 + 12 – 18) – (12* 3-6 %2)
ii. Y = (12 / 4 % 2 – 8 + 16 * 6)

Prepared by: Dr. P. Vijay Kumar, Dept of CSE, KITSW


Page 7 of 7

You might also like