0% found this document useful (0 votes)
2 views

if-else

The document discusses program control statements, which manage the flow of execution in programming through sequence, selection, and repetition. It details various decision-making statements, particularly the 'if' statement and its forms, as well as the 'switch' statement for multiple selections. Additionally, it covers the use of conditional operators and provides examples and problems related to these concepts.

Uploaded by

Zayed Oyshik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

if-else

The document discusses program control statements, which manage the flow of execution in programming through sequence, selection, and repetition. It details various decision-making statements, particularly the 'if' statement and its forms, as well as the 'switch' statement for multiple selections. Additionally, it covers the use of conditional operators and provides examples and problems related to these concepts.

Uploaded by

Zayed Oyshik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

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. 1
Decision Making Statement
• if statement
• switch statement
• Conditional operator statement
• goto statement

They are also known as control statements.


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

3
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;
}
5
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.

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

7
if....
• In C, an expression is true if it evaluates to any
nonzero value.

• If it evaluate to zero, it is false.

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

Output

This line will print.

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

Output

Nothing!
10
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!
11
What will happen?

int a=10, b=20;


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

Output

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

• Should Tani buy this?


• Should they buy this?

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
if…else 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

We will discuss it in slide 5!


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!
goto Statement
• Guess what I am going to say!

We will learn
later!

You might also like