0% found this document useful (0 votes)
6 views40 pages

Conditional Statement

The document provides an overview of control statements in programming, focusing on conditional statements such as if, switch, and conditional operators. It explains the different forms of the if statement, including simple, nested, and else if ladder structures, and illustrates their usage with examples. Additionally, it discusses the switch statement for multiple alternatives and highlights the differences between if and switch statements.

Uploaded by

mdismailbd018
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)
6 views40 pages

Conditional Statement

The document provides an overview of control statements in programming, focusing on conditional statements such as if, switch, and conditional operators. It explains the different forms of the if statement, including simple, nested, and else if ladder structures, and illustrates their usage with examples. Additionally, it discusses the switch statement for multiple alternatives and highlights the differences between if and switch statements.

Uploaded by

mdismailbd018
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

Conditional Statement

Samira Akter
Lecturer, Dept. of CSE
Presidency University
Email: samiraa@[Link]
Program Control Statements
•Control statements control the flow of execution in a
program or function.
•There are three kinds of execution flow:
– Sequence:
•the execution of the program is sequential.
– Selection:
•A control structure which chooses alternative to execute.
– Repetition:
•A control structure which repeats a group of statements.

2
Decision Making Statement
•if statement
•switch statement
•Conditional operator statement
•goto statement

They are also known as control statements.


3
Different forms of if statement
1. Simple if statement
2. if....else statement
3. Nested if......else statement
4. else if ladder
BECOME FAMILIAR WITH THE if
• I n i t s s i m p l e s t fo r m , t h e i f
statement allows our program to
conditionally execute a statement.
• Its operation is governed by the
outcome of a conditional test
evaluates to either true or false.

5
If Statement
If….
• The expression may be any valid
C expression.
• If the expression evaluated as
tru e, th e statemen t w ill be
executed.
• If it does not - the statement is
bypassed and the line of code
following the if is executed.

7
If Statement
if(Conditions){
------------------
-----------------
}
if....
•In C, an expression is true if it evaluates to any
nonzero value.
•If it evaluate to zero, it is false.

9
What will happen?
int a=10, b=20;
if(a < b)
printf(“This line will print.”);
Output
This line will print.

10
What will happen?
int a=10, b=20;
if( a > b)
printf(“This line will print.”);
Output
Nothing!

11
What will happen?
int a=10, b=20;
printf(“%d”, a<=b);
Output
1

12
ADD THE else!
•We can add else statement to the if.
•The if…else statement is an extension of the
if(test expression)
simple if statement.
{
True-block statement(s)
}
else
{
False-block statement(s)
} 13
If Statement
if(Conditions){
------------------
-----------------
}
else{
---------------
-----------------
}
ADD THE else!

Which number is greater? 15


Program that will decide whether a number is
positive or not.

How do you understand if a number is


positive or negative?
Program that will decide whether a number is
positive or not.

Positive if Number>0
Negative if Number <0
Program that will decide whether a number is
positive or not.
Remember!!!
•The result/value produce by the relational
and logical operators is either 0 or 1.
•It produce 1 for true
•And 0 for false.

19
Program that will decide whether a number is
positive or not or neither positive nor negative.
If Statement
if(Conditions){
------------------
}
else if(Conditions){
---------------
}
else{
-----------------
}
if…else Ladder!

Which number is 22
if…else Ladder!

23
Program that will decide whether a number is
positive or not.
Nested if…else

25
Nested if…else

26
Program to find the greatest digit from three
digits
Program to find the greatest digit from three
digits

a,b,c
a bigger: a>b & a>c
b bigger: b>a & b>c
Else c is bigger
Program to find the greatest digit from three
digits
#include <stdio.h>
int main()
{
int dig1, dig2, dig3;
printf("Enter three numbers: ");
scanf("%d%d%d", &dig1, &dig2, &dig3);
if(dig1 > dig2){
if(dig1 > dig3){
printf("dig1 is the maximum");
}
else{
printf("dig3 is the maximum");
}
}
else
Nested if…else
Problem!
A commercial bank has introduced an incentive policy
of giving bonus to all its deposit holders. The policy is
as follows: A bonus of 2 per cent of the balance held
on 31st December is given to every one, inspective of
their balance, and 5 per cent is given to female
account holders if their balance is more than Tk.
50,000.
30
Conditional Operators

exp1 ? exp2 : exp3


The Conditional Operator
• the conditional operator (? : )

• A conditional expression is written


expression 1 ? expression 2 : in the
form expression 3
True or False? True False

32
The Conditional Operator

(a + b) >= 13 ? a = 100 : a =
1000
True or False?
True
33
The Conditional Operator

i = (a+b)<13 ? 100 : 1000


True or False?
False
34
SELECT ALTERNATIVE WITH
THE switch STATEMENT
• If is good for choosing between two
alternatives
• When several alternatives are needed we
should use switch statement.
• switch is C’s multiple selection statement.
• Use to select one of several alternative paths in
program execution
How it works?
A value is successively
tested against a list of
integer or character
constants.
When the match is
found, the statement
sequence associated
with that match is
executed.
Statement sequence
are not blocks, not
use curly braces
Example -
switch
if vs switch
• switch can only test for equality,
where the if conditional expression
can be of any type
• switch will work with only int or char
types. We can’t use float or others.
Nested switch!
goto Statement
•Guess what I am going to say!

We will learn
later!

You might also like