C language control statements

sumanAggarwal3 1,103 views 16 slides Feb 16, 2016
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

C language control statements


Slide Content

Decision Making and Branching in C
SUMAN AGGARWAL
ASSISTANT PROFESSOR
ADVANCED EDUCATIONAL INSTITUTE
www.advanced.edu.in

In programming the order of execution of instructions may have to be changed depending on
certain conditions. This involves a kind of decision making to see whether a particular condition
has occurred or not and then direct the computer to execute certain instructions accordingly
C language posses such decision making capabalities by supporting the following ststement:
1. if statement
2. Nesting of if else Statement
3. switch statement
4. goto statement
www.advanced.edu.in

Decision making with if statement
The if statement is a powerful decision making statement and is used to control the flow of
execution of statements.
The syntax is
if (expression)
The expression is evaluated and depending on whether the value of the expression is true (non
zero) or false (zero) it transfers the control to a particular statement
www.advanced.edu.in

The general form of simple if
statement is:
if (test expression)
{
statement block;
}
statement n;
www.advanced.edu.in

Small Description
www.advanced.edu.in
Syntax is:
if (test expression)
{
statement block;
}
else
{
statement block;
}
statement n;

Nesting of if....else statement:
www.advanced.edu.in
When a series of conditions are to be checked, we may have to use more than one if... else
statement in the nested form.
if (test condition 1)
{
if (test condition 2)
{
statement block 1;
}
else
{
statement block 2;
} statement m;
}
else
{
if (test condition 3)
{
statement block 3;
}
else
{
statement block 4
} statement n;
}
statement x; -

Description of if....else statement:
If the test condition 1 is true then, test condition 2 is checked and if it is true, then the statement
block 1 will be executed and the control will be transferred to statement m and it will executed
and then statement x will be executed.
If the test condition 1 is true but test condition 2 is false, statement block 2 will be executed and
the control is transferred to statement m and it will be executed and then statement x will be
executed.
If the test condition 1 is false, then test condition 3 is checked and if it is true, statement block 3
will be executed, then control is transferred to statement n and it will be executed and then
statement x will be executed.
If the test condition 1 is false and test condition 3 is also false, statement block 4 will be
executed, then the control is transferred to statement n and it will be executed and then statement
x is executed.
-
www.advanced.edu.in

Switch Statement
www.advanced.edu.in
The switch structure is a multiple-selection structure that
allows even more complicated decision statements than a two-
way if/else structure allows.
It chooses one of the "cases" depending on the result of the
control expression.
Only variables with the INT or CHAR data types may be used in
the control expressions (i.e. parentheses) of switch statements.
Single quotes must be used around CHAR variables
Single quotes are NOT used around the integer value

Switch Syntax
www.advanced.edu.in
switch (expression)
{
case value 1 :
statement block 1;
break;
case value 2:
statement block 2;
break;
:
:
default:
default block;
}
statement n;

Flowchart Switch Statement
www.advanced.edu.in

Description Switch Statement
www.advanced.edu.in
The expression is an integer expression or characters. Value 1, value 2, ...... are constants , constant
expressions (evaluable to an integral constant) or character and are known as case labels. Each of these
values should be unique within a switch statement, statement block 1, statement block 2, ........ are
statement list and may contain 0 or more statements. There is no need to put braces between these
blocks. The case labels end with a colon (:).
When the switch is executed, the value of the expression is successively compared against the values
value 1, value 2, ...... If a case is found whose value matches with the value of the expression, then the
block of statement that follows that case are executed. The break statement at the end of each block,
signals the end of a particular case and causes an exit from the switch statement transferring the control
to the statement n following the switch block. The default is an optional case. When present, it will be
executed if the value of the expression does not match with any of the case values and then the statement
n will be executed. If not present no action takes place if all matches fails and the control goes to
statement n.

The goto statement
www.advanced.edu.in
This statement is used to branch unconditionally from one point to another in the
program. This statement goto requires a label to locate the place where the branch is to
be made. A label is any valid identifier and must be followed by a colon. The label is
placed immediately before the statement where the control is to be transferred.
Different ways of using goto statement are given below:
Syntax:
goto label;

Forward jump
www.advanced.edu.in
•Forward jump: In this the position of the label is after the goto statement.
goto label;
:
:
label:
statement n;
Example:
goto read;
n = 5 * 4;
:
:
read:
scanf ("%d", &code);
•:
:

Backward jump
www.advanced.edu.in
Backward jump: In this, the position of the label is before the goto statement
label:
statement n;
: goto label;
Example:
. . read:
scanf ("%d", &code);
:
:
goto read;
n = 5 * 4;
:
:

Conclusion
www.advanced.edu.in
The C language programs presented until now follows a sequential form of execution of
statements. Many times it is required to alter the flow of the sequence of instructions. C language
provides statements that can alter the flow of a sequence of instructions. These statements are
called control statements. These statements help to jump from one part of the program to another.
The control transfer may be conditional or unconditional.

Suman Aggarwal
Assistant Professor
Advanced Educational Institutions
◦ Advanced Educational Institutions,
◦70 km Milestone,
◦Delhi-Mathura Road, Dist. Palwal, Haryana-121105
◦ +91–1275–398400, 302222
Contact Email Id: [email protected]
Website: http://www.advanced.edu.in
www.advanced.edu.in
“THANK YOU”