0% found this document useful (0 votes)
4 views32 pages

02 - Conditional Statements

The document is a lecture on C programming focusing on operators and conditional statements. It covers arithmetic operators, increment/decrement operators, assignment operators, relational operators, logical operators, and conditional statements such as if-else structures. Additionally, it includes examples and exercises for practical application of these concepts.

Uploaded by

tasnimulnafis
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)
4 views32 pages

02 - Conditional Statements

The document is a lecture on C programming focusing on operators and conditional statements. It covers arithmetic operators, increment/decrement operators, assignment operators, relational operators, logical operators, and conditional statements such as if-else structures. Additionally, it includes examples and exercises for practical application of these concepts.

Uploaded by

tasnimulnafis
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/ 32

ME 172

Introduction to C Programming
Language
Lecture 2: Operators and Conditional Statements

Courtesy: Dr. Noor Al-Quddus


Dr. Monjur Morshed
M Jamil Hossain
Cyrus Ashok Arupratan Atis
M Aminul Hoque
Partha Kumar Das
kjjSaif Al Afsan Shamim

1
Arithmetic operators (Contd...)
• The data items that operators act upon are called operands
• The operands can be integer quantities, floating-point quantities or characters

The remainder operator (%) requires that both operands be integers and
the second operand be nonzero. Similarly, the division operator (/)
requires that the second operand be nonzero.

Division of one integer quantity by If a division operation is


another always results in a truncated carried out with two floating-
value (i.e., the decimal portion of the point numbers, or with one
value will be dropped). floating-point number and
one integer, the result will be
a floating-point

2
Operators...

Arithmetic operator:

There are five(5) arithmetic operators in C

Operator Name Example

+ Addition a+b
- Subtraction a–b
* Multiplication a*b
/ Division a/b
% Remainder a%b 3
Arithmetic operators (Contd...)

Example:
If a and b are integers
a=10 ; b=3

Expression Value
a+b 13
a-b 7
a*b 30
a /b 3
a%b 1
4
Performance Test 1
 Write a C program that will divide 30000 apples to 52
buyers. Display how many apples each buyer will get and
how many apples will be left (Use of arithmetic operator
is a must, do not do the calculations and then print the
desired output).

Time: 3 minutes!!

5
ANSWER
#include <stdio.h>

void main()
{
int a,b,c,d;
a=30000;
b=52;
c=30000/52;
d=30000%52;
printf("%d and %d",c,d);
}

6
Increment/decrement operator
• ++a/a++ is equivalent to a=a+1
• --a/a-- is equivalent to a=a-1
++m and m++

Prefix operator Postfix operator

The difference for built-in types is:


• ++a first increments the value of a and then returns a value referring to a, so if the value of
a is used then it will be the incremented value.
• a++ first returns a value whose value is a, that is, the old value, and then increments a at
an unspecified time before the next full-expression (i.e., "before the semicolon").

7
Increment/decrement operators(Contd...)

 x=x*a++is equivalent to x=x*a ; a=a+1


 x=x*++a is equivalent to a=a+1 ; x=x*a

 y=y*b-- is equivalent to y=y*b ; b=b-1


 y=y*--b is equivalent to b=b-1 ; y=y*b

8
Increment/decrement Operators(Contd...)

For x=a*++b output:


• Write the following program: 210
& for x=a*b++ output:
#include<stdio.h>
200
void main()
{
int a=10,b=20,x;
x=a*++b;
printf("\n The value of x is: %d”,x);
}

Replace the line x=a*++b with x=a*b++


9
Assignment Operator
Shorthand Notation
Operator Description Example
+= Add AND assignment operator C += A is equivalent to
C=C+A

-= Subtract AND assignment operator C -= A is equivalent to


C=C-A

*= Multiply AND assignment operator C *= A is equivalent to


C=C*A

/= Divide AND assignment operator C /= A is equivalent to


C =C /A

10
Relational Operators (Contd..)
Also called Comparison operators
It performs tests on their operands. They return he
Boolean value . Such as:
• 1 if the statement is successful (true)
• 0 otherwise
Example Name Result

a == b Equal TRUE if a is equal to b.


a != b Not Equal TRUE if a is not equal to b.
a<b less than TRUE if a is strictly less than b.
a>b greater than TRUE if a is strictly greater than b.
a<=b less than or equal to TRUE if a is less than or equal to b.
a>=b greater than or equal to TRUE if a is greater than or equal to b.
11
LOGICAL Operators

Example Name Result


!a Not TRUE if a is not TRUE.
a && b And TRUE if both a and b are TRUE.
a || b Or TRUE if either a or b is TRUE.

a b a && b a || b
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1

12
The ? : operator

General form:
Conditional expression ? Expression 1 : Expression2

Example:
if (x<0)
flag = 0;
else
flag = 1;

The above statement can be written as


flag = (x<0) ? 0 : 1 ;
13
Math.h (header file)
Most of the mathematical functions are placed in math.h header
Some are specified in the stdlib.h header
Some common mathematical functions:
Function Name Description
exp(x) returns e raised to the given power (ex)
sqrt(x) computes square root (√x)
cos(x) computes cosine (cos(x))
pow(x,y) raises a number to the given power (xy) [pow(x.y)]
sinh(x) computes hyperbolic sine (sinh(x))
erf(x) error function
And so on……. tan(x), abs(x), log10(x)….etc

The outputs of the functions are of the double format.


14
Math.h header file
Math Constants:
Constant Name Description
M_E The base of natural logarithms (e).
M_LOG2E The base-2 logarithm of e.
M_PI 3.141593

M_SQRT2 The positive square root of 2.


15
M_SQRT1_2 The positive square root of 1/2.
Practice Example
#include<stdio.h>
output:
#include<math.h>
-1.000000
int main()
{
double pi;
pi=M_PI; //sets pi = 3.1416
double sum;
sum=cos(pi);
//here in cos(x) , x is radian value, so input should be radian
printf(“%lf”,sum);
return 0;
}
16
# Practice

• Write a program that takes two numbers as input.


• Find the square root of the first number and the resulting
output will be the radius of a cylinder.
• Raise the second input number to a power of 5. The
resulting output will be the height of the cylinder.
• Find the volume of the cylinder by using the saved value of
pi in the header file.

• Remember to use the math.h file.

17
ANSWER
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{ int a,b;
double r,h,pi,V;
pi= M_PI;
printf(“Enter the first number: \n"); scanf("%d",&a);
printf(“Enter the second number: \n"); scanf("%d",&b);
r= sqrt(a);
h= pow(b,5);
V=pi*r*r*h;
printf("The volume is %lf",V);
return 0; }

18
The getch() function
The getch( ) function reads a single character the instant it’s typed
without waiting for ENTER.
get means it gets something i.e. it’s an input function
ch means it gets a character

The getche() function


The getche( ) function also reads a single character the instant it’s
typed without waiting for ENTER and also echoes it.
get means it gets something i.e. it’s an input function
ch means it gets a character
e means it echoes the character to the screen when
you type it.
19
Example
#include <stdio.h>
void main(void)
{
char test;
printf(“Type any character: ”);
test = getch();
printf(“\nThe character you typed was:
%c”,test);
}

Replace getch() with getche()


20
Conditional Statements

21
22

The if statement

General form:

if (condition)
{
statement;
}

Conditions:
1. Using relational or conditional operators
2. Using logical operators
23

Multiple statements within if

General form:
if (condition)
{
statement 1;
statement 2;
-----------;
statement n;
}
24

Example of if statement
Write the following program with multiple statements

int i; If you type 1, Output:


printf(“Enter an Integer: ”); Enter an Integer: 1
scanf(“%d”,&i); You typed 1
if (i==1) End of Statement
End of the program
{
printf(“\n You typed 1”);
printf(“\n End of statement”);
}
printf(“\n End of the program”);
If you type any other no.,
except1 .Output:
Enter an Integer: 3
End of the program
25
General form

if-else if-else if-else


if (condition) if (condition)
{ {
statement 1; statement 1;
statement 2; statement 2;
} }

else else if (condition)


{ {
statement 1; statement 1;
statement 2; statement 2;
} }

else
{
statement 1;
Note: else is optional statement 2;
}
26

Example of if-else statement


Write the following program with multiple statements

int i;
printf(“Enter an Integer: ”);
scanf(“%d”,&i);
if (i==1)
{
printf(“\n You typed 1”);
}
else
{
printf(“\n You did not type 1”);
}
printf(“\n End of the program”);
27

Example of if- else if- else statement

int num;
printf(“Enter an Integer: ”);
scanf(“%d”,&num);
if (num < 0)
{
printf(“\n the number is less than zero”);
}
else if(num == 0)
{
printf(“\n the number is equal to zero”);
}
else
{
printf(“\n the number is greater than zero”);
}
28

Nested if-else statements


General form Another form

if (condition) if (condition)
{ {
statement; if (condition)
} {
statement;
else }
{ else
if (condition) {
{ statement;
statement; }
} }

else else
{ {
statement; statement;
}
} }
# Class Performance 2

Write a C program to show grade of a student.

Number Grade
Less than 40 F
Between 40 and 70 A-
Between 70 and 80 A
Over or equal to 80 A+

Take the number obtained as input and print the


grade as output. If the number is below zero or
above 100 show an error message.

29
ANSWER
void main()
{ int x;
printf("Enter the number obtained by the student: ");
scanf("%d",&x);
if (x>=0 && x<40)
{ printf("The grade of the student is F"); }
else if(x>=40 && x<70)
{ printf(" The grade of the student is A-"); }
else if(x>=70 && x<80)
{ printf(" The grade of the student is A"); }
else if(x>=80 && x<100)
{ printf(" The grade of the student is A+"); }
else
{ printf(“The number is invalid"); }}
30
Thank you

Everything has its beginning. But it doesn't start at "one."

-Metal Gear Solid 4

31
Assignments:
1. Write a C program to find the smallest of 3 integers taken as
input using nested if-else statement .

2. Write a C program to find the roots of a Quadratic Equation


ax2+bx+c =0, that will take coefficients a, b, c as input and
find the roots as output.. Use nested if-else statement.

32

You might also like