C Programming - Conditional Statements
C Programming - Conditional Statements
CONDITIONAL STATEMENTS
Conditional Operator
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
EXPRESSION & STATEMENTS
An expression is the combination of functions,
operators, commas, variables, identifiers etc.
Example: x=0, x++
An expression, which returns only two value
either TRUE or FALSE, is known as Boolean
expression.
An expression becomes a statement when it is
followed by a semicolon.
Example: x=0; x++; printf(…..);
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
CONDITIONAL STATEMENTS
Conditional statement is a feature of
programming language which allows it to
perform actions depending upon some conditions
provided by the programmer.
If the given condition is true then the set of
statements are executed otherwise body is
skipped.
Two types of Conditional Statements in C are:
if … else statement
switch case statement
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
IF STATEMENT
The if statement gives the user the choice of
executing a block of statements if the expression
is evaluated to true or skipping it if the
expression is evaluated to false.
The expression must be of Boolean type
expression.
Syntax:
if(expression)
{
statements;
}
5
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
FLOWCHART OF IF STATEMENT
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
EXAMPLE CODE: CHECKS WHETHER A
return 0;
}
7
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
NESTED IF STATEMENT
Using of one if statement within another if
statement is known as nested if statement.
Syntax
if(expression)
{
statements;
if(expression)
{
statements;
}
statements;
}
8
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
IF .. ELSE STATEMENT
It is known as double blocked conditional statements.
It means, it has TRUE part as well as FALSE part.
If the given condition is true then the TRUE part is
executed otherwise FALSE part is executed.
The expression inside if-else statement expects a
value. If the value is nonzero then it is true. If the
value is 0 then it is false.
Syntax:
if(expression)
{
statement1;
}
else
{
statement2;
} 9
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
FLOWCHART OF IF .. ELSE STATEMENT
10
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
EXAMPLE CODE: CHECKS WHETHER A
return 0;
}
11
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
IF-ELSE IF- ELSE STATEMENT
The if-else if- else statement in C is generally used
when we need to compare more than one condition.
The else block is executed if all the preceding
conditions failed.
When we want to specify condition with else part then
we have to use ladder of
if statement.
If-else if- else checks its expression sequentially.
When it found one expression is true then it will not
check other expressions within that if-else if-else
block.
12
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
IF-ELSE IF- ELSE STATEMENT
Syntax
if(expression1)
{
statement1;
}
else if(expression2)
{
statement2;
}
else if(expression3)
{
statement3;
}...
else
{
statement n;
}
13
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
EXAMPLE CODE: COMPARING TWO
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
PREFIX & POSTFIX ++
Code Output
#include <stdio.h> 5
int main() 7
{
int var=5;
printf("%d\n",var++);
printf("%d",++var);
return 0;
}
16
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
PREFIX & POSTFIX --
Code Output
#include <stdio.h> 4
int main() 4
{
int var=5;
printf("%d\n",--var);
printf("%d",var--);
return 0;
}
17
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: IF .. ELSE
Code Output
#include<stdio.h>
int main() 3
{
int x,y=0,a=2,b=3;
if(y>0)
{
x=a;
}
else
{
x=b;
}
printf("%d",x);
return 0;
}
18
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: IF .. ELSE
Code Output
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: IF .. ELSE
Code Output
#include<stdio.h> Hello
int main()
{
int x=1;
if(x--)
{
printf("Hello\t");
}
if(x++)
{
printf("Everyone");
}
return 0;
}
20
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: IF .. ELSE
Code Output
#include<stdio.h> 11
int main()
{
int x=2;
if(x--)
printf("1\t");
else if(x--)
printf("2\t");
else if(x--)
printf("3\t");
else
printf("None\t");
printf("%d",x);
return 0;
} 21
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: IF .. ELSE
Code Output
#include<stdio.h> 1 3 4 -3
int main()
{
int x=1;
if(x--)
printf("1\t");
if(x--)
printf("2\t");
else if(x--)
printf("3\t");
if(x--)
printf("4\t");
printf("%d",x);
return 0;
} 22
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: IF .. ELSE
Code Output
#include<stdio.h>
int main() Nothing
{
int x=0,y=1;
if(x=0)
{
if(y==2)
{
printf("Hello\t");
}
else
{
printf("Welcome\t");
}
}
printf("Nothing");
return 0;
} 23
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: IF .. ELSE
Code Output
#include<stdio.h> Hello
int main()
{ Something
int x=0;
if(x==0)
{
printf("Hello\n");
}
printf("Something\n");
return 0;
}
24
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
EXAMPLE CODES: IF .. ELSE
}
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
FINDING LARGEST OF THREE VALUES
#include <stdio.h>
int main(){
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf("%.2f is the largest number.", n1);
if( n2>=n1 && n2>=n3 )
printf("%.2f is the largest number.", n2);
if( n3>=n1 && n3>=n2 )
printf("%.2f is the largest number.", n3);
return 0;
26
}
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
SWITCH CASE
Switch case is a multiple or multiway branching decision making
statement.
Switch case checks the value of an expression against a case values, if
condition matches the case values then the control is transferred
to that point.
Syntax
switch (expression)
{
case constexp1: statements;
break;
case constexp2: statements;
break;
...
case constexpN : statements;
break;
default : statements;
}
Break statement causes the execution to jump out of the switch to the
statement immediately following the switch.
27
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
SWITCH STATEMENT FLOWCHART
28
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
NESTED SWITCH CASE
We can use switch statement within a switch
statement which is called nested switch.
29
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
EXAMPLE CODES: SWITCH CASE
Code Output
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
EXAMPLE CODES: SWITCH CASE
Code Output
#include<stdio.h>
int main()
Enter a character: e
{ Vowel
char c;
printf("Enter a character:");
scanf("%c",&c);
switch(c)
{
case 'a':printf("Vowel\n"); break;
case 'e':printf("Vowel\n"); break;
case 'i':printf("Vowel\n"); break;
case 'o':printf("Vowel\n"); break;
case 'u':printf("Vowel\n"); break;
default :printf("Consonent");
}
return 0;
} 31
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science &
Engineering
February 18, 2023
CONDITIONAL OPERATOR
The conditional operator takes three operands, so it is a
ternary operator.
The conditional operator is the only ternary operator
available in C.
Syntax: expression ? statement1 : statement2
If the condition (expression before ?) evaluates to be true,
the first statement -- the statement after the question mark
will get executed.
Otherwise, the second statement -- the statement after the
colon gets executed.
32
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
CONDITIONAL OPERATOR
33
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
CONDITIONAL OPERATOR EXAMPLE
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
(num % 2 == 0)? printf(“Even") : printf(“Odd");
return 0;
}
34
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
CONDITIONAL OPERATOR EXAMPLE
#include<stdio.h>
int main()
{
float num1, num2, min;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
min = (num1 < num2) ? num1 : num2;
printf("Minimum = %.2f", min);
return 0;
}
35
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 19, 2023
CONDITIONAL OPERATOR EXAMPLE
#include<stdio.h>
int main()
{
int item;
printf("Enter the number of items: ");
scanf("%d",&item);
printf("You have %d item%s.", item, (item==1)?"":"s");
return 0;
}
36
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
SIMPLE CALCULATOR USING SWITCH CASE
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
C PROGRAM TO CHECK LEAP YEAR
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
C PROGRAM TO CHECK ALPHABET
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
C PROGRAM TO CHECK VOWEL OR CONSONANT
if (isLowercaseVowel || isUppercaseVowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0; 40
}
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
ASSIGNMENT PROBLEMS:
Calculate the Grade of a given mark of a student
using if .. else statements.
Write a program to input the salary of a person
and calculate the HRA and DA according
to the following conditions:
Salary HRA MA
5000-10000 10% 5%
10001-15000 15% 8%
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
ASSIGNMENT PROBLEMS:
Write a program to input the sales made by a
salesman and calculate the commission according
to the following conditions:
Sales Commission
1-10000 4%
10001-20000 5%
20001-30000 6%
>30000 7%
Write a program to input a number (1 to 7) and
print the weekday name according to the given
number.
42
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
REFERENCES
C Programming: A Modern Approach by K.N.
King, 2nd Edition.
C: The Complete Reference by Herbert Schildt,
4th Edition
C Programming Language by Kernighan and
Ritchie, 2nd Edition
Beginning C: From Novice to Professional, by
Ivor Horton, 4th Edition
https://devdocs.io/c/
https://en.cppreference.com/w/c/language
43
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
February 18, 2023
THANK YOU
44
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering