02 - Conditional Statements
02 - Conditional Statements
Introduction to C Programming
Language
Lecture 2: Operators and Conditional Statements
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.
2
Operators...
Arithmetic operator:
+ 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++
7
Increment/decrement operators(Contd...)
8
Increment/decrement Operators(Contd...)
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 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;
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
21
22
The if statement
General form:
if (condition)
{
statement;
}
Conditions:
1. Using relational or conditional operators
2. Using logical operators
23
General form:
if (condition)
{
statement 1;
statement 2;
-----------;
statement n;
}
24
Example of if statement
Write the following program with multiple statements
else
{
statement 1;
Note: else is optional statement 2;
}
26
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
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
if (condition) if (condition)
{ {
statement; if (condition)
} {
statement;
else }
{ else
if (condition) {
{ statement;
statement; }
} }
else else
{ {
statement; statement;
}
} }
# Class Performance 2
Number Grade
Less than 40 F
Between 40 and 70 A-
Between 70 and 80 A
Over or equal to 80 A+
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
31
Assignments:
1. Write a C program to find the smallest of 3 integers taken as
input using nested if-else statement .
32