0% found this document useful (0 votes)
11 views30 pages

Slide 4 - If-Else

The document introduces C's program control statements, which manage the flow of execution through sequence, selection, and repetition. It details various decision-making statements such as if, switch, and conditional operators, explaining their syntax and usage. Additionally, it provides examples and problems to illustrate the concepts of if statements, else statements, and the switch statement.

Uploaded by

ridowan2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views30 pages

Slide 4 - If-Else

The document introduces C's program control statements, which manage the flow of execution through sequence, selection, and repetition. It details various decision-making statements such as if, switch, and conditional operators, explaining their syntax and usage. Additionally, it provides examples and problems to illustrate the concepts of if statements, else statements, and the switch statement.

Uploaded by

ridowan2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Thanks to

Nakib Hayat Chowdhury

Introducing C’s Program Control


Statements

1
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
BECOME FAMILIAR WITH THE if
• In its simplest form, the if 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.

4
Different forms of if statement

1. Simple if statement
2. if....else statement
3. Nested if......else statement
4. else if ladder
If….
• Simplest form of if for single statement
if(expression)
statement;
• For multiple statements
if(expression)
{
statement 1;
statement 2;
……
statement n;
}
6
If….
• The expression may be any valid
C expression.

• If the expression evaluated as


true, the statement will be
executed.

• If it does not - the statement is


bypassed and the line of code
following the if is executed.

7
Old Problem!
• Draw the flowchart of a program that reads two numbers and
print “First” if first number is greater than second.
• The program

Home Task
1. Write a program that will reads a number from terminal and
determine whether this number is ‘odd’ or ‘even’ and print
the message- NUMBER IS EVEN or NUMBER IS ODD
2. Write a program to determine whether a given number is
divisible by 7 and 3 or not.

8
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
int a=10,
a=10, b=20;
b=20;
if(
if( 010) )
printf(“This
printf(“This line
line will
will print.”);
print.”);

Output
Output
This line will print.

Nothing!
12
What will happen?

int a=10, b=20;


printf(“%d”, a<=b);

Output
1

13
ADD THE else!
• We can add else statement to the if.
• The if…else statement is an extension of the simple if
statement.

if(test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
Statement x
14
ADD THE else!

Which number is greater?


15
else if Ladder!

Which number is greater? 16


if…else Ladder!

17
Nested if…else

18
Nested if…else

19
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.

20
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.

21
Conditional Operators

exp1 ? exp2 : exp3


The Conditional Operator
• the conditional operator (? : )

• A conditional expression is written in the form

expression 1 ? expression 2 : expression 3

True or False? True False

23
The Conditional Operator

(a + b) >= 13 ? a = 100 : a = 1000

True or False?

True
24
The Conditional Operator

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

True or False?

False
25
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!

You might also like