0% found this document useful (0 votes)
20 views38 pages

3.control Statements

Uploaded by

Mushfik Ahmed
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)
20 views38 pages

3.control Statements

Uploaded by

Mushfik Ahmed
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/ 38

Control Statements

Mahit Kumar Paul


Lecturer, Dept. of CSE
RUET, Rajshahi-6204
[email protected]

07/07/2024 1
Control Statements
• Enable to specify the flow of program control; i.e. the
order in which the instructions in a program must be
executed.
• Flow of control can be of basic three types:

• Sequential
• Selection
• Repetition

07/07/2024 2
Control Statements…
• Sequential - Default mode. Sequential execution of
code statements. i.e. One line after another.
Example

07/07/2024 3
Control Statements…
• Selection - Used for decisions making, branching. i.e.
choosing between two or more alternative paths.
• if
• if…else
• switch

• Repetition - Used for looping, i.e. repeating a piece of


code multiple times in a row.
• for
• while
• do…while
07/07/2024 4
‘if’ Statement
Syntax Flow Diagram

if(expression) {
//statement(s)
}

07/07/2024 5
‘if’ Statement…
Example

Output: a is bigger than b


This is next instruction following 'if'
07/07/2024 6
‘if…else’ Statement
• An if statement can be followed by an
optional else statement, which executes when the
expression of if portion is false.

• C programming language assumes any non-zero and


non-null values as true, and if it is either zero or null,
then it is assumed as false value.

07/07/2024 7
‘if…else’ Statement…

Syntax Flow Diagram

if(expression) {
//statement(s)
}
else{
//statement(s)
}

07/07/2024 8
‘if…else’ Statement…
Example

Output: a is greater than 20


value of a is : 100
07/07/2024 9
‘if…else’ Statement…
Example

Output: a is less than 20


value of a is : 100
07/07/2024 10
‘if…else’ Statement…
Example

Output: a is greater than 20


value of a is : 100
07/07/2024 11
‘if…else’ Statement…
Example

Output: a is greater than 20


value of a is : 100
07/07/2024 12
‘if…else’ Statement…
Example

Output: a is less than 20


value of a is : 100
07/07/2024 13
‘if…else if…else’ Statement
• An if statement can be followed by an optional else
if...else statement.
• It is very useful to test various conditions using single
if...else if statement.
• When using if...else if...else statements, there are few
points to keep in mind −
 An if can have zero or one else's and it must
come after the last else if's.
 An if can have zero to many else if's and they
must come before the else.
 Once an else if succeeds, none of the remaining
07/07/2024 else if's or else's will be tested. 14
‘if…else if…else’ Statement…
Syntax
if(expression) {
//statement(s)
}
else if(expression){
//statement(s)
}

else
//statement(s)

07/07/2024 15
‘if…else if…else’ Statement…
Example

Output: Value of a is 20
07/07/2024 Exact value of a is: 20 16
‘if…else if…else’ Statement…
Example

Output: None of the values is matching


07/07/2024 Exact value of a is: 50 17
Nested ‘if’ Statement
• Means you can use one if or else if statement inside
another if or else if statement(s).

07/07/2024 18
Nested ‘if’ Statement…
Syntax

if(expression) {
//statement(s)
if(expression) {
//statement(s)
}
}
07/07/2024 19
Nested ‘if’ Statement…
Note: You can nest else if...else in the similar way as if
statements.

07/07/2024 20
Nested ‘if’ Statement…
Example

Output: y = 6400
07/07/2024 21
Nested ‘if’ Statement…
Example

Output:
Greater than -50
07/07/2024 22
‘switch’ Statement
• switch case statements are a substitute for long if…else
if statements that compare a variable to several integral
values.
• The switch statement is a multi-way branch statement. It
provides an easy way to dispatch execution to different
parts of code based on the value of the expression.
• switch is a control statement that allows a value to
change control of execution.

07/07/2024 23
‘switch’ Statement…
Syntax Flow Diagram

switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
……….
case p: // code to be executed if n = p;
default: // code to be executed if n
// doesn't match any cases
//default is optional
}
07/07/2024 24
‘switch’ Statement…
Example

Output:
n = 311
07/07/2024 25
‘switch’ Statement…
Notes:
• The expression used in switch must be integral type ( int, char and
enum). Any other type of expression is not allowed.
Example

Output:

07/07/2024 26
‘switch’ Statement…
Notes:
• The expression used in switch must be integral type ( int, char and
enum). Any other type of expression is not allowed.
Example

Output:
p = 599
07/07/2024 27
‘switch’ Statement…
Notes:
• All the statements following a matching case execute until a
break statement is reached.
Example

Output:
m = 300
n = 311

07/07/2024 28
‘switch’ Statement…
Notes:
• The default block can be placed anywhere. The position of default
doesn’t matter, it is still executed if no match found.
Example

Output:
No cases matched

07/07/2024 29
‘switch’ Statement…
Notes:
• The default statement is optional. Even if the switch case statement
do not have a default statement, it would run without any problem.
Example

Output:

07/07/2024 30
‘switch’ Statement…
Notes:
• The statements written above cases are never executed. After the switch statement,
the control transfers to the matching case, the statements written before case are not
executed.
Example

Output:
n = 311

07/07/2024 31
‘switch’ Statement…
Notes:
• Two case labels cannot have same value.

Example

Output:

07/07/2024 32
‘switch’ Statement…
Notes:
• Any number of case statements can be used within a
switch.

• Each case is followed by the value to be compared to and


a colon (:).

07/07/2024 33
Nested ‘switch’ Statement…
• It is possible to have a switch as a part of the statement
sequence of an outer switch.

• Even if the case constants of the inner and outer switch


contain common values, no conflicts will arise.

07/07/2024 34
Nested ‘switch’ Statement…
Syntax
switch(x1) {
case 1:
printf("This case 1 is part of outer switch" );
switch(x2) {
case 1:
printf("This case 1 is part of inner switch" );
break;
case 2: /* case code */
}
break;
case 2: /* case code */
}
07/07/2024 35
Nested ‘switch’ Statement…
Example

07/07/2024 36
Nested ‘switch’ Statement…
Output

This is part of outer switch


This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200

07/07/2024 37
THANKS

07/07/2024 38

You might also like