control-statements detailed presentation

gayathripcs 26 views 26 slides Sep 17, 2024
Slide 1
Slide 1 of 26
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
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

about control statements


Slide Content

Objectives of these slides:Objectives of these slides:

to introduce the main kinds of C contrto introduce the main kinds of C contr
ol flowol flow
Control
Statements
ProgrammingProgramming In C In C

Control StructuresControl Structures

There may be situations where the There may be situations where the
programmer requires to alter normal flow of programmer requires to alter normal flow of
execution of program or to perform the execution of program or to perform the
same operation a no. of times.same operation a no. of times.

Various control statements supported by c Various control statements supported by c
are-are-

Decision control statementsDecision control statements

Loop control statementsLoop control statements

Decision Control StatementsDecision Control Statements

Decision control statements alter the normal Decision control statements alter the normal
sequential execution of the statements of the sequential execution of the statements of the
program depending upon the test condition to be program depending upon the test condition to be
carried out at a particular point in program.carried out at a particular point in program.

Decision control statements supported by c are:-Decision control statements supported by c are:-

if statementif statement

if-else statementif-else statement

Else if LadderElse if Ladder

Nested If Nested If

switch statementswitch statement

Decision Control StatementsDecision Control Statements

if statementif statement

Most simple and powerful decision control statement.Most simple and powerful decision control statement.

It executes a statement or block of statements only if the It executes a statement or block of statements only if the
condition is true.condition is true.

Syntax: if (condition)Syntax: if (condition)if (condition)if (condition)
{{ OR OR{{
statement (s);statement (s); statement 1; statement 1;
}} statement 2; statement 2;
Next statement;Next statement;}}
statement 3;statement 3;

In above syntax : if condition is true only then the In above syntax : if condition is true only then the
statements within the block are executed otherwise next statements within the block are executed otherwise next
statement in sequence is executed.statement in sequence is executed.


Flowchart Flowchart
Condition
STOP
False
True
Block of if
Next statement
/* Program to check whether a no. is even */
# include<stdio.h>
# include<conio.h>
void main()
{
int num;
clrscr();
printf(“enter the number”);
scanf(“%d”,&num)
if(num%2==0)
{
printf(“\n Number is even”);
}
printf(“ End of program”);
getch();
}

if – else statementif – else statement

In case of if statement, the block of statements is executed only when the In case of if statement, the block of statements is executed only when the
condition is true otherwise the control is transferred to the next statement condition is true otherwise the control is transferred to the next statement
following if block.following if block.

But if specific statements are to be executed in both cases (either But if specific statements are to be executed in both cases (either
condition is true or false) then if – else statement is used.condition is true or false) then if – else statement is used.

In if – else statement a block of statements are executed if the condition In if – else statement a block of statements are executed if the condition
is true but a different block of statements is executed when the condition is true but a different block of statements is executed when the condition
is false.is false.

Syntax: if (condition)Syntax: if (condition)
{{
statement 1;statement 1;
statement 2;statement 2;
}}
elseelse
{{
statement 3;statement 3;
}}
Test
Condition
Block of if Block of else
Next statement
STOP
False
True

Exercise: WAP to check whether a given no. is even or Exercise: WAP to check whether a given no. is even or
odd?odd?
Nested if – else statementNested if – else statement

When an entire if-else is enclosed within the body of if When an entire if-else is enclosed within the body of if
statement or/and in the body of else statement, it is known statement or/and in the body of else statement, it is known
as nested if-else statement.as nested if-else statement.

The ways of representing nested if –else are-The ways of representing nested if –else are-
if (condition1)
{
if (condition2)
statement 1;
else
statement 2;
}
else
statement 3;
if (condition1)
{
if (condition2)
statement 1;
else
statement 2;
}
else
{
if (condition 3)
statement 3;
else
statement 4;
}
if (condition1)
statement 1;
else
{
if (condition2)
statement 2;
else
statement 3;
}

If- else- if ladderIf- else- if ladder

In a program involving multiple conditions, the nested if else In a program involving multiple conditions, the nested if else
statements makes the program very difficult to write and statements makes the program very difficult to write and
understand if nested more deeply.understand if nested more deeply.

For this ,we use if-else-if ladder.For this ,we use if-else-if ladder.

Syntax: if (condition1)Syntax: if (condition1)
statement1;statement1;
else if(condition2)else if(condition2)
statement2;statement2;
else if(condition3)else if(condition3)
statement 3;statement 3;
elseelse
default statement;default statement;
condition 1
condition 2
condition 3Statement 1
Statement 2
Statement 3
Default statement
Next statement
false
true
false
true
false
true

Switch statementSwitch statement

Switch is a multi-way decision making statement which selects Switch is a multi-way decision making statement which selects
one of the several alternatives based on the value of single one of the several alternatives based on the value of single
variable or expression.variable or expression.

It is mainly used to replace multiple if-else-if statement.It is mainly used to replace multiple if-else-if statement.

The if-else-if statement causes performance degradation as The if-else-if statement causes performance degradation as
several conditions need to be evaluated before a particular several conditions need to be evaluated before a particular
condition is satisfied.condition is satisfied.

Syntax: Syntax: switch (expression)switch (expression)
{{
case constant1 : statement (s); [break;]case constant1 : statement (s); [break;]
case constant2 : statement (s); [break;]case constant2 : statement (s); [break;]
………………………………………………………………………… ..
default: statement (s)default: statement (s)
}}

Break statementBreak statement

Break statement terminates the execution of Break statement terminates the execution of
the loop in which it is defined.the loop in which it is defined.

The control is transferred immediately to the The control is transferred immediately to the
next executable statement after the loop.next executable statement after the loop.

It is mostly used to exit early from the loop It is mostly used to exit early from the loop
by skipping the remaining statements of by skipping the remaining statements of
loop or switch control structures.loop or switch control structures.

Syntax: break;Syntax: break;

Looping StructuresLooping Structures

When we want to repeat a group of statements a no. When we want to repeat a group of statements a no.
of times, loops are used.of times, loops are used.

These loops are executed until the condition is true.These loops are executed until the condition is true.

When condition becomes false, control terminates When condition becomes false, control terminates
the loop and moves on to next instruction the loop and moves on to next instruction
immediately after the loop.immediately after the loop.

Various looping structures are-Various looping structures are-

whilewhile

do – whiledo – while

forfor

LOOPING STATEMENTSLOOPING STATEMENTS

Loop is divided into two parts:Loop is divided into two parts:

Body of the loopBody of the loop

Control of loopControl of loop

Mainly control of loop is divided into two Mainly control of loop is divided into two
parts:parts:

Entry Control loop (while, for)Entry Control loop (while, for)

Exit Control loop (do-while)Exit Control loop (do-while)

while statementwhile statement

While loop is used to execute set of statements as long as While loop is used to execute set of statements as long as
condition evaluates to true.condition evaluates to true.

It is mostly used in those cases where the programmer It is mostly used in those cases where the programmer
doesn’t know in advance how many times the loop will be doesn’t know in advance how many times the loop will be
executed.executed.

Syntax: while (condition)Syntax: while (condition)
{{
Statement 1 ;Statement 1 ;
Statement 2 ;Statement 2 ;
}}
condition
statement
Statement after while loop
true

do- whiledo- while

do-while is similar to while except that its test do-while is similar to while except that its test
condition is evaluated at the end of the loop instead at condition is evaluated at the end of the loop instead at
the beginning as in case of while loop.the beginning as in case of while loop.

So, in do-while the body of the loop always executes So, in do-while the body of the loop always executes
at least once even if the test condition evaluates to at least once even if the test condition evaluates to
false during the first iteration.false during the first iteration.

Syntax: doSyntax: do
{{
statement 1;statement 1;
statement 2;statement 2;
}while (condition);}while (condition);
statement;statement;
Body of loop
Test condition
Next statement
true
false

for loopfor loop

Most versatile and popular of three loop structures.Most versatile and popular of three loop structures.

Is used in those situations when a programmer Is used in those situations when a programmer
knows in advance the number of times a statement knows in advance the number of times a statement
or block will be executed.or block will be executed.

It contains loop control elements all at one place It contains loop control elements all at one place
while in other loops they are scattered over the while in other loops they are scattered over the
program and are difficult to understand.program and are difficult to understand.

Syntax:- Syntax:-
for (initialization; condition; increment/decrement)for (initialization; condition; increment/decrement)
{{
Statement( s);Statement( s);
}}

The The forfor is a sort of is a sort of whilewhile
for (expr1; expr2; expr3)for (expr1; expr2; expr3)
statement; statement;
is equivalent to:is equivalent to:
expr1;expr1;
while (expr2) {while (expr2) {
statement; statement;
expr3; expr3;
}}

Various other ways of writing same for loopsVarious other ways of writing same for loops
i = 1i = 1
for (; i<=15;i ++)for (; i<=15;i ++)
{{
…………....
}}
for (i=1; ;i++)
{
………
if (i>15)
break;
……
}
for (i=1;i<=15;)
{
………….
i++;
}

Some ExamplesSome Examples
for(i = 7; i <=77; i += 7)for(i = 7; i <=77; i += 7)
statement; statement;
for(i = 20; i >= 2; i -= 2)for(i = 20; i >= 2; i -= 2)
statement; statement;
for(j = 10; j > 20; j++)for(j = 10; j > 20; j++)
statement; statement;
for(j = 10; j > 0; j--)for(j = 10; j > 0; j--)
statement;statement;

IncrementingIncrementing

Add 1 to c by writing:Add 1 to c by writing:
c = c + 1;c = c + 1;
Also:Also: c += 1;c += 1;
Also:Also: c++;c++;
Also:Also: ++c;++c;
Incrementing and DecrementingIncrementing and Decrementing

/* Preincrementing and /* Preincrementing and popostincrementing */ stincrementing */

#include <stdio.h>#include <stdio.h>
int main()int main()
{{
int c; int c;
c = 5; c = 5;
printf("%d\n", c); printf("%d\n", c);
printf("%d\n",c++); printf("%d\n",c++); /*post/*post increment*/increment*/
printf("%d\n\n", c); printf("%d\n\n", c);
::
continued

c = 5;c = 5;
printf("%d\n", c); printf("%d\n", c);
printf("%d\n",++c); /*pre-increme printf("%d\n",++c); /*pre-increme
nt*/nt*/
printf("%d\n", c); printf("%d\n", c);
return 0; return 0;
}}
Output:Output:
55
55
66
55
66
66

DecrementingDecrementing

Take 1 from c by writing:Take 1 from c by writing:
c = c - 1;c = c - 1;
Also:Also: c -= 1;c -= 1;
Also:Also: c--;c--;
Also:Also: --c;--c;

Continue statementContinue statement

Like break ,continue statement also skips the remaining Like break ,continue statement also skips the remaining
statements of the body of the loop where it is defined but statements of the body of the loop where it is defined but
instead of terminating the loop, the control is transferred to the instead of terminating the loop, the control is transferred to the
beginning of the loop for next iteration.beginning of the loop for next iteration.

The loop continues until the test condition of the loop become The loop continues until the test condition of the loop become
false.false.

Syntax: continue;Syntax: continue;

E.g. E.g. for (m=1;m<=3;m++)for (m=1;m<=3;m++)
{{
for (n=1;n<=2;n++)for (n=1;n<=2;n++)
{{
if (m==n)if (m==n)
continue;continue;
printf(“ m=%d n=%d”);printf(“ m=%d n=%d”);
}}
}}
Output:
12
21
31
32

goto Statementgoto Statement

An unconditional control statement that causes the control to An unconditional control statement that causes the control to
jump to a different location in the program without checking jump to a different location in the program without checking
any condition.any condition.

It is normally used to alter the normal sequence of program It is normally used to alter the normal sequence of program
execution by transferring control to some other part of the execution by transferring control to some other part of the
program.program.

So it is also called jump statement.So it is also called jump statement.

Syntax: goto label;Syntax: goto label;

Label represents an identifier which is used to label the Label represents an identifier which is used to label the
destination statement to which the control should be destination statement to which the control should be
transferred.transferred.
label : statement;label : statement;

The goto statement causes the control to be shifted either in The goto statement causes the control to be shifted either in
forward direction or in a backward direction .forward direction or in a backward direction .

exit() functionexit() function

C provides a run time library function exit() which C provides a run time library function exit() which
when encountered in a program causes the program to when encountered in a program causes the program to
terminating without executing any statement terminating without executing any statement
following it.following it.

Syntax: Syntax: exit(status);exit(status);
Status is an integer variable or constant.Status is an integer variable or constant.

If the status is 0,then program normally terminates If the status is 0,then program normally terminates
without any errors.without any errors.

A non-zero status indicates abnormal termination of A non-zero status indicates abnormal termination of
the program.the program.

The exit() function is defined in the process.h header The exit() function is defined in the process.h header
file.file.

Difference b/w exit() & breakDifference b/w exit() & break

Exit()Exit() is used to transfer the control is used to transfer the control
completely out of the program whereas completely out of the program whereas
breakbreak is used to transfer the control out is used to transfer the control out
of the loop or switch statement.of the loop or switch statement.
Tags