0% found this document useful (0 votes)
22 views50 pages

Lecture 3

This document discusses various operators in C programming language. It describes arithmetic operators like addition, subtraction etc. and their precedence. It also covers relational operators to compare values, logical operators like AND, OR to combine conditions, and assignment operators to assign values to variables. Examples are given to explain the usage and evaluation of expressions containing different types of operators.

Uploaded by

Rinku Rinku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views50 pages

Lecture 3

This document discusses various operators in C programming language. It describes arithmetic operators like addition, subtraction etc. and their precedence. It also covers relational operators to compare values, logical operators like AND, OR to combine conditions, and assignment operators to assign values to variables. Examples are given to explain the usage and evaluation of expressions containing different types of operators.

Uploaded by

Rinku Rinku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

CSE-125

Computer Programming &


Engineering Analysis

Lecture: 3
Control Statements
Avishek Das
Department of CSE
CUET
Introduction
➢An operator is a symbol that tells the computer to perform certain
manipulations.
➢An expression is a sequence of operands and operators that
reduces to a single value.
➢C operators can be classified into a number of categories.
➢Arithmetic operators (+,-,*,/)
➢Relational operators (> , <, <=)
➢Logical operators (&&, ||, !)
➢Assignment operators (=)
➢Increment and decrement operators (++ , --)
➢Conditional operators (?..:)
➢Bitwise operators (&, |, ^, <<, >>)
16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 2
3

Arithmetic operators
➢The arithmetic operators used for numeric calculations. They are two
types:
➢Unary Arithmetic Operators
+x, -y, ++, --, !, ~
➢Binary Arithmetic Operators
Operator meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% modulo division

16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 3


4

Example
#include<stdio.h>
int main()
{
int months, days ;
printf("Enter days: \n”) ;
scanf("%d”, &days) ;
months = days / 30 ;
days = days % 30 ;
printf("Months =%d Days= %d\n", months, days);
}
16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 4
5

Operator’s Precedence
Note:
➢The precedence of arithmetic operators
1. Brackets ()
2. Unary ++ or --
3. * / %
4. + -
36 / 12 * 4
10 + 23 % 4 * 5
(10 + 23) % 4 * 5

16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 5


6

Arithmetic expressions
➢An arithmetic expression is a combination of variables, constants,
and operators.
➢For example,
a*b-c → a*b-c
(m+n)(x+y) → (m+n)*(x+y)
ax2+bx+c → a*x*x+b*x+c

16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 6


7

ARITHMETIC EXPRESSIONS
➢ Integer Arithmetic: When both operands are integers
for example if a=17 and b=4

Expression Result
a+b 21
a-b 13
a*b 68
a/b 4
a%b 1

16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 7


8

ARITHMETIC EXPRESSIONS
➢Real Arithmetic: When both operands are float types
for example if a=12.4 and b=3.1

Expression Result
a+b 15.5
a-b 9.3
a*b 38.44
a/b 4.0
a%b NA

16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 8


9

Arithmetic expressions
➢Mixed-mode Arithmetic: When one operand is integers
and another is real .for example if a=12 and b=2.5

Expression Result
a+b 14.5
a-b 9.5
a*b 30.0
a/b 4.8

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 9


Relational Operators
➢ The relational operators in C are : used to compare values of two
expressions depending on their relations.

Operator Meaning
< less that
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 10


Relational Operators
➢A relational expression yields a value of 1 or 0.

➢5 < 6 TRUE 1
➢4.5<10 TRUE 1
➢-34 + 8 > 23 - 5 FALSE 0
➢4.5<-10 FALSE 0
➢5==0 FALSE 0
➢if a=3, b=2, c =1; then a > b > c is ?

➢Arithmetic operators have higher priority over relational


operators.
➢ The associativity of relational operators is left → right
17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 11
Relational Operators
➢The relational operators >, >=, < and <= have the same
precedence. Just below them in precedence are the equality
operators: = = and !=.
➢Relational operators have lower precedence than arithmetic
operators , so an expression like i < lim -1 is taken as i < (lim -1).

➢3 >= 2 = = -4 < 0

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 12


Logical operators
➢C has the following three logical operators
➢ && meaning logical AND ( binary operator )
➢ || meaning logical OR ( binary operator )
➢ ! meaning logical NOT ( unary operator )

➢Expressions connected by && or || are evaluated left to right,


and evaluation stops as soon as the truth or falsehood of the
result is known.

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 13


Logical operators
➢The logical operators && and|| are used when we want to test
more than one condition and make decisions. An example is :
a>b && x==10

➢ An expression of this kind, which combines two or more


relations expressions, is termed as a logical expression or
compound relational expression.

➢The logical expression given above is TRUE only if a>b is true and
x==10 is true. If either (or both) of them are false, the expression
is false.

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 14


Logical operators

op-1 op2 op-1&&op-2 op-1 || op-2 !op-1


Non-zero Non-zero 1 1 0
Non-zero 0 0 1 0
0 Non-zero 0 1 1
0 0 0 0 1

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 15


Logical operators
➢The precedence of && is higher than that of ||, and both are
lower than relational operators, so
3 < 5 && -3 < -5 || 3 > 2

➢char ch; to decide whether ch is an uppercase,


ch >= ‘A’ && ch <= ‘Z’

➢to decide whether a year is leap year,


(year % 4 == 0 && year % 100 != 0) || year % 400 == 0

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 16


Assignment operators
➢Assignment operators are used to assign the result of an
expression to a variable.
➢C has a set of ‘shorthand’ assignment operators of the form
v op= exp;
where v is a variable, exp is an expression and op is a C binary
arithmetic operator. The operator op= is known as the shorthand
assignment operator.
➢The assignment statement v op= exp; is equivalent to v = v op
(exp) ;
➢For example, x += y + 1; This is same as the statement x = x + ( y
+1);

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 17


Assignment operators

Statement with simple Statement with shorthand


assignment operator operator

a = a+1 a += 1

a = a-1 a -= 1

a = a * (n+1) a *= n+1

a = a / (n+1) a /= n+1

a=a%b a %= b

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 18


Assignment operators
➢The use of shorthand assignment operators has three advantages:

➢What appears on the left-hand side need not be repeated and therefore
it becomes easier to write.

➢The statement is more concise and easier to read.

➢ The statement is more efficient.

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 19


Increment and decrement operators
➢C provides two unusual operators for incrementing and decrementing
variables.

➢The increment operator ++ adds 1 to its operand, while the decrement


operator -- subtracts 1.

➢The unusual aspect is that ++ and -- may be used either as prefix


operators (before the variable, as in ++n), or postfix operators (after
the variable: n++).

➢In both cases, the effect is to increment n. But the expression ++n
increments n before its value is used, while n++ increments n after its
value has been used.
17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 20
Increment and decrement operators
➢ Prefix Increment/Decrement:
The statement y = ++x means first increment the value of x by 1, then assign
the value to y. this statement belongs to the following two statement:

#include<stdio.h> Output
main()
{
int x=8;
printf(“x=%d\n”,x); x=8
printf(“x=%d\n”,++x); x=9
printf(“x=%d\n”,x); x=9
printf(“x=%d\n”,--x); x=8
printf(“x=%d”,x); x=8
}

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 21


Increment and decrement operators
➢ Postfix Increment/Decrement:
The statement y = x-- means first the value of x is assign to y and then x is
decremented. this statement belongs to the following two statement:

#include<stdio.h> Output
main()
{
int x=8;
x=8
printf(“x=%d\n”,x);
printf(“x=%d\n”,x--);
x=8
printf(“x=%d\n”,x); x=7
printf(“x=%d\n”,x++); x=7
printf(“x=%d”,x); x=8
}

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 22


Increment and decrement operators
➢for example, if n is 5, then
x = n++;
is equivalent to
x = n; n = n + 1;
➢But
x = ++n;
is equivalent to
n = n + 1; x = n;

➢The increment and decrement operators can only be applied to


variables, an expression like (i+j)++ is illegal.
17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 23
Increment and decrement operators
➢The increment and decrement operators can be used in complex
statements. Example:
m=n++ -j +10;

➢Consider the expression


m = - n++ ;
➢The precedence of ++ and -- operators are the same as those of
unary + and -
➢The associatively of them is “right to left”.
m = - n++; is equivalent to m = - (n++)

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 24


Increment and decrement operators
➢suppose,
int a, b, c ; a = b = c = 1;
➢After execution the following statements, what are the values of
the expression and variables.
(1) a>b && b>c++;
(2) a-- || c++;
(3) !a && b++;
(4) ++a && ++b && ++c;
(5) ++a && --b && ++c;
17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 25
Control Structures
➢ Control structure refers to the way in which the programmer
specifies the order of executing the statements. Three approach -

➢ Sequential – all the statements are executed in the same order as it is


written.

➢ Selectional - based on some conditions, different set of statements are


executed.

➢ Iterational (Repetition) - certain statements are executed repeat.

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 26


Selectional Control Structures
➢ There are two selectional control structures
➢ If-else statement
➢ Switch statement

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 27


Simple if Statement
➢ A specific condition (value or expression) is tested.
➢ If the condition is true, a set of statements are executed.
➢ If the condition is false, the statements are not executed and the
program control goes to the next statement that immediately
follows if block.

Syntax: True Set of


if (condition) condition Statement(s)
{
Statement(s); False
}
Next Statement; Next Statement

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 28


if-else Statement
➢ In simple ‘if’ statement, when the condition is false, there is no
alternate set of statements.

Syntax:
if (condition) True
Statement set-1
{ condition

Statement set-1;
} False
else
{ Statement set-2
Statement set-2;
}
Next Statement;
Next Statement

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 29


if-else-if Statement Syntax:
if (condition-1)
➢ When one condition is false, it checks for {
Statement set-1;
the next condition and so on. }
else if (condition-2)
➢ When all the conditions are false the ‘else’ {
block is executed. Statement set-2;
}
………………………………
else if (condition-n)
{
Statement set-n;
}
else
{
Statement set-x;
}

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 30


Nested if-else Statement
Syntax:
➢ When one condition is false, it checks for
the next condition and so on. if (condition-1)
{
if (condition-2)
➢ When all the conditions are false the ‘else’
{
block is executed. Statement set-2;
}
else
{
Statement set-n;
}
}
else
{
Statement set-x;
}

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 31


Nested if-else Statement

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 32


Example - 1

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 33


Example - 2
Odd/Even number

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 34


Example - 3

16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 35


Example - 4
Find the largest number

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 36


Example - 5
Leap year checking

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 37


Example - 5
Leap year checking

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 38


Example - 6
Double if statement

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 39


Example - 7
Nested if statement

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 40


Conditional operator

➢A ternary operator pair “? : ” is available in C to construct


conditional expressions of the form
expr1 ? expr2 : expr3

➢the expression, expr1 is evaluated first. If it is non-zero (true),


then
➢the expression, expr2 is evaluated, and that is the value of the
conditional expression.
➢Otherwise, expr3 is evaluated, and that is the value.

➢Only one of expr2 and expr3 is evaluated.


17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 41
Example-1
➢Consider an example two find the largest number between two numbers.

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 42


Example-1

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 43


Example – 2 using Conditional Operator

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 44


Switch case Statement
➢ The ‘switch’ statement is a type of decision control structure that selects a
choice from the set of available choices.
➢ It is very similar to ‘if’ statement.
➢ But ‘switch’ statement cannot replace ‘if’ statement in all situations.
Syntax:
Switch(integer variable or integer expression or character variable) {
case integer or character constant-1 :
Statement(s); break;
case integer or character constant-2 :
Statement(s); break;
……………
case integer or character constant-n :
Statement(s); break;
default:
Statement(s); break;
}

17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 45


Rules for switch
➢ The switch expression must be an integral type(int, char and
enum).

➢ Case labels must be constants or constant expressions.


➢ Case labels must be unique.
➢ Case labels must end with colon.

➢ The break statement transfers the control out of the switch


statement.
➢ The break statement is optional.

➢ The default label is optional.


➢ It is permitted to nest switch statements.
Example
int i = 2;

switch(i){
case 1:
printf(“ONE”);
break;
case 2: Output:
printf(“TWO”);
break; TWO
case 3:
printf(“THREE”);
break;
default:
printf(“INVALID”);
break;
}
Example
switch (departmentCode){
case 4 :
printf(“CSE”);
break;
case 2 :
printf(“EEE”);
break;
case 8 :
printf(“ETE”);
break;
case 7 :
printf(“PME”);
break;
case 11:
print(“BME”);
}
Example

int iNum = 2;
switch(iNum) {
case 1.5:
printf(“ONE AND HALF”); Case 1.5: this is invalid
because the values in
break; case statements must be
case 2: Integers or characters
printf(“TWO”);
break;
case ‘A’ :
printf(“A character”);
}
Example
#include<stdio.h>
int main()
{
char ch;
printf("Enter the vowel:");
scanf("%c", &ch);
switch(ch) {
case 'a' : printf("Vowel");
break;
case 'e' : printf("Vowel");
break;
case 'i' : printf("Vowel");
break;
case 'o' : printf("Vowel");
break;
case 'u' : printf ("Vowel");
break;
default : printf("Not a vowel");
}
return 0;
}

You might also like