Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

C Programming - Conditional Statements

Uploaded by

mr.tauhidsoad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C Programming - Conditional Statements

Uploaded by

mr.tauhidsoad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

C PROGRAMMING:

CONDITIONAL STATEMENTS

Prepared By: Ahmed Abdal Shafi Rasel


Dept of Computer Science & Engineering
February 18, 2023
CONTENTS
 Statements and Expressions
 If .. Else Statement

 Switch Case Statement

 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

February 18, 2023


GIVEN NUMBER IS EVEN
#include <stdio.h>
int main(){
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf(“The given number is even.”);
return 0;
}
printf(“The given number is odd.”);

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

February 18, 2023


GIVEN NUMBER IS EVEN
#include <stdio.h>
int main(){
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0)
printf(“The given number is even.”);
else
printf(“The given number is odd.”);

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

February 18, 2023


INTEGERS
<#include <stdio.h>
int main(){
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if(number1 == number2)
printf("Result: %d = %d",number1,number2);
else if (number1 > number2)
printf("Result: %d > %d", number1, number2);
else
printf("Result: %d < %d",number1, number2);
return 0; 14
}
Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
INCREMENT ++ AND DECREMENT --

February 18, 2023


OPERATOR AS PREFIX AND POSTFIX
 In programming, increment ++ operator
increases the value of a variable by 1 and
decrement -- operator decreases the value of a
variable by 1.
 Suppose you use ++ operator as prefix
like: ++var. The value of var is incremented by 1
then, it returns the value.
 Suppose you use ++ operator as postfix
like: var++. The original value of var is returned
first then, var is incremented by 1.
 The same goes with -- operator.
15

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

#include<stdio.h> Hello Friend


int main()
{
int x=1;
if(x--)
{
printf("Hello\t");
}
printf("Friend");
return 0;
} 19

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

February 18, 2023


Code
Output
#include<stdio.h>
int main() Enter two numbers: 15 5
{
int number_1,number_2; Divisible
printf("Enter two numbers :");
scanf("%d%d",&number_1,&number_2);
if(number_1%number_2==0)
{
printf("Divisible");
}
else
{
printf("Not Divisible");
}
return 0; 25

}
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

#include<stdio.h> Enter a number: 2


int main()
{ Two
int x;
printf("Enter a number:");
scanf("%d",&x);
switch(x)
{
case 1: printf("One\n");
break;
case 2 :printf("Two\n");
break;
case 3 :printf("Three\n");
break;
default:printf("Other\n");
}
return 0;
} 30

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

February 18, 2023


# include <stdio.h>
int main() {
char operator; double a,b;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&a, &b);
switch(operator)
{
case '+': printf("%.1lf + %.1lf = %.1lf",a, b, a+ b);
break;
case '-': printf("%.1lf - %.1lf = %.1lf",a, b, a- b);
break;
case '*': printf("%.1lf * %.1lf = %.1lf",a, b, a* b);
break;
case '/': printf("%.1lf / %.1lf = %.1lf",a, b, a/ b);
break;
default: printf("Error! operator is not correct"); }
return 0;
}
37

Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
C PROGRAM TO CHECK LEAP YEAR

February 18, 2023


#include <stdio.h>
int main(){
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0) {
if( year%100 == 0) {
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year); }
else
printf("%d is a leap year.", year ); }
else
printf("%d is not a leap year.", year);
return 0;} 38

Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
C PROGRAM TO CHECK ALPHABET

February 18, 2023


#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);

if( (c>='a' && c<='z') || (c>='A' && c<='Z'))


printf("%c is an alphabet.",c);
else
printf("%c is not an alphabet.",c);
return 0;
}
39

Prepared By: Ahmed Abdal Shafi Rasel, Department of Computer Science & Engineering
C PROGRAM TO CHECK VOWEL OR CONSONANT

February 18, 2023


#include <stdio.h>
int main()
{
char c; int isLowercaseVowel, isUppercaseVowel;

printf("Enter an alphabet: ");


scanf("%c",&c);

isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');


isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

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%

HRA: House Rent Allowance


MA: Medical Allowance
41

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

You might also like