CH - 2-Control Structure in C

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Chapter-2 Control Structure in ‘C’

 Topics:-

• Simple Statements
• Decision Making Statements
• Looping Statements
• Nesting of Control Structures
• Break and Continue Statement
• goto statement
 Decision Making Statements
Decision making can be done in C using various types of statements are as
following:

1. if statement
• The general syntax of simple if statement as follows :
Syntax :
if(condition)
{
Statement block;
}
statement-x;
• In above syntax the statement block may be a single statement of a group
of statements.
• Flowchart :

True False
Condition

Statement block Statement-x

The simple if statement is executed in following order :


• First the condition is checked.
• If the condition is true then the statement block is executed and then
statement-x.
• If the condition is false then only statement-x is executed.
Example :
if(a>b)
{
printf(“a is maximum”);
}
2. if.....else Statement
• The general syntax of if….else statement as follows:
• Syntax :
if(condition)
{
True block statement(s);
}
else
{
False block statement(s);
}
statement-X;
• Flowchart :

True False
True Block Condition False Block

Statement-X

The if….else statement is executed in following order :


• First the condition is checked.
• If the condition is true then the true block statement is executed and then
statement-X is executed.
• If the condition is false then the false block statement is executed and then
statement-X is executed.
Example :
if(a>b)
{
printf(“a is maximum”);
}
else
{
printf(“b is maximum”);
}
3. Nested if...else Statement
• Nesting of if…..else statement is used to check more than one conditions.
Syntax:
if(condition1)
{
if(condition2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-X;
Flowchart

False Condition1 True

False True
Condition2

Statement-3 Statement-2 Statement-1

Statement-X
The Nested if….else statement is executed in following order :

• First the condition1 is checked.


• If the condition1 is true then condition2 is checked.
• If condition2 is true then statement-1 is executed and then statement-X is
executed.
• But if condition2 is false then statement-2 is executed and then statement-X is
executed.
• If the condition1 is false then the statement-3 is executed and then statement-X is
executed.
Example
if(account_no > 5)
{
if(balance > 5000)
{
bonus = 0.05*balance;
}
else
{
bonus = 0.02*balance;
}
}
else
{
bonus = 0.02*balance;
}
balance = balance + bonus;
4. if….else if ladder
Syntax :
if(condition1)
{
statement-1;
}
else if(condition2)
{
statement-2;
}
.
.
.
else if(condition N)
{
statement-N;
}
else
{
Default statement;
}
statement-X;
Flowchart :
True False
Condition1

True False
Condition2

True
statement-2 False
Condition N
statement-1

statement-N Default

statement-X
The if….else if ladder is executed in the following order :
• First condition1 is executed.
• If the condition1 is true then the statement-1 is executed and then statement-X is
executed.
• If the condition1 is false then condition2 is checked.
• If condition2 is true than statement-2 is executed and then statement-X is
executed.
• This process is repeated until all the condition is checked.
• If all the condition becomes false then the default statement is executed and then
statement-X is executed.
Example :
To find maximum number for four given number.

if(a>b && a>c && a>d)


{
printf(“a is maximum”);
}
else if(b>a && b>c && b>d)
{
printf(“b is maximum”);
}
else if(c>a && c>b && c>d)
{
printf(“c is maximum”);
}
else
{
printf(“d is maximum”);
}
5. switch….case statement
• The switch case statement is also known as multi way decision statement.
• The switch statement test the value of a given variable against a list of case values
and when a match is found, a block of statements associated with that case is
executed.
Syntax :
Switch(expression)
{
Case value1:
block1;
break;
Case value2:
block2;
beak;
……………………..
……………………..
Default :
Default block;
break;
}
Statement-X;
Flowchart :
Expression

Value1
block1

Value2
block2

No match
Default
Block

Statement-X
The order of execution :
• The value of the expression is compared against the values value-1,value-2,
…….
• If value of the expression is match with value-1 then block1 is executed and
then statement-X is executed.
• If the value of the expression is match with value2 then block2 is executed
and then statement-X is executed.
• If the value of the expression is not match with any value then Default Block
is executed then statement-X is executed.
• Rules for switch statement :

1. The switch expression must be an integral type.


2. Case labels must be constant or constant expressions.
3. Case labels must be unique. No two labels can have the same value.
4. Case label must end with colon (:).
5. The break statement transfers the control out of the switch statement.
6. The break statement is optional. That is, two or more case labels may be
long to the same statements.
7. The default label is optional. If present, it will be executed when the
expression does not match any case label.
8. There can be at most one default label.
9. The default may be placed anywhere but usually placed at the end.
10. It is permitted to nested switch statements.
Example :
switch(choise)
{
case 1:
c=a+b;
break;
case 2:
c=a-b;
break;
case 3:
c=a*b;
break;
case 4:
c=a/b;
break;
default :
printf(“wrong choise entered”);
break;
}
6. goto statement
• goto statement is used to branch unconditionally from one point to another
point in the program.
• With goto statement, label is always used to identify the point where the
branch is to be made.
• A label is any valid variable name, and it must followed by colon.
• A label is placed immediately before the statement where the control is to be
transferred.
 There are two types of goto statements :

1. Forward jump : If a label is after the statement goto label; then some
statements will be skipped and control is transfer to that label.

goto label;
……………
……………
label:
statement;
2. Backward jump : If a label is before the statement goto label; then a loop
will be formed and some statements will be executed repeatedly.

Label :
Statement;
…………….
…………….
goto label;

• The following example asks user to input number until user enter the positive
number.

X: printf(“enter number”);
scanf(“%d”,&n);
if(n<0)
goto X;
else
printf(“%d”,n);
 Conditional (Ternary) operator
• This operator is useful for making two way decisions.
• It is a combination of ? and : .
• It has three operands.
• It is also known as ternary operator.

Syntax :
Conditional expression ? expression1 : expression2

• In this syntax first the conditional expression is checked.


• If the it is true then expression1 is executed.
• If it is false then expression2 is executed.
Example :
if(x<0)
{
flag=0;
}
else
{
flag=1;
}

Can be written as :

flag = ( x < 0 ) ? 0 : 1;
 What is loop?
• A loop is a group of statements that are executed until some condition
satisfied.
• The loop consists two segments:
• Body of the loop: This segment consists single or group of statements.
• Control statements: The control statement consists the condition.
1. Entry controlled loop
• If the condition is checked before body of loop then it is known as entry
controlled loop.

False
Condition

True
Body Of Loop

• As shown in flowchart, First condition is checked.


• If the condition is true then body of loop is executed.
• After execution of body of loop the condition is checked again and this
process is continue until condition becomes false.
• If the condition is false then body of loop is not executed.
2. Exit controlled loop
• If the condition is checked after body of loop then it is known as exit
controlled loop.

Body Of Loop

True
Condition

False

• As shown in flowchart, First the body of loop is executed.


• Then the condition is checked.
• If the condition is true then body of loop is executed again and this
process is repeated until condition becomes false.
• If the condition is false then body of loop is not executed.
 There are basically three type of loop in C language as following:

1. while loop

• A while loop is entry controlled loop since in while loop first condition
is checked.
Syntax:

while(condition)
{
body of loop;
}
Flowchart:

False
Condition

True
Body Of Loop

• As shown in flowchart, First condition is checked.


• If the condition is true then body of loop is executed.
• After execution of body of loop the condition is checked again and this
process is continue until condition becomes false.
• If the condition is false then body of loop is not executed.
• In while loop if the condition becomes false at the first time then body
of loop will never executed.
Example:

i=1;
while(i<=10)
{
printf(“%d”,i);
i++;
}
2. do….. while loop
• A while loop is exit controlled loop since in do….while loop first body of
loop is executed.

Syntax:

do
{
body of loop;
}
while(condition);
 Flowchart:

Body Of Loop

True
Condition

False

• As shown in flowchart, First the body of loop is executed.


• Then the condition is checked.
• If the condition is true then body of loop is executed again and this
process is repeated until condition becomes false.
• If the condition is false then body of loop is not executed.
• In do….while loop body of loop will executes at least once even if the
condition is false at the first time.
Example:

i=1;
do
{
printf(“%d”,i);
i++;
}
while(i<=10);
3. for loop
• for loop is an entry controlled loop since in for loop first condition is checked.

Syntax:

for ( initialization; condition; increment or decrement )


{
body of loop;
}
 Flowchart:
Initialization

Condition False
True
Body of loop

Increment/
Decrement

• As shown above flowchart, In the Initialization first initialize the variable.


• Then the condition is checked.
• If the condition is true then body of loop is executed.
• After the completion of execution of body of loop, the value of variable is
increment or decrement.
• After increment or decrement the value of variable, the value of the
variable is checked using condition. This process is repeated until the
condition becomes false.
• If the condition is false then body of loop is not executed.
Example:
for(i=1;i<=10;i++)
{
printf(“%d”,i);
}
 Additional features of for loop
1. More than one initialization is allowed in for loop.
Ex:
j=1;
for(i=0;i<=10;i++) can be written as for(j=1,i=0;i<=10;i++)
2. More than one increment or decrement is allowed
in for loop.
Ex:
for(j=1,i=0;i<=10;i++,j++)
3. More than one condition is allowed in for loop.
Ex:
for(j=0,i=0;i<=10,j<=15;i++,j++)
4. One or more part of the for loop can be omitted.
Ex:
i=0;
for(;i<=10;)
{
printf(“%d”,i);
i++;
}
5. Nested for loop is allowed.
Ex:
for(i=0;i<=10;i++)
{
for(j=1;j<=15;j++)
{
printf(“%d”, i+j);
}
printf(“\n”);
}
 break statement
• The break statement is used to terminate the loop.
• When the break statement is encounter in the loop, the loop is exited
and the control is transfer out of that loop.
• The break statement can be used in only with while loop, do….while
loop and for loop

Example:
for(i=1;i<=10;i++)
{
printf(“%d”,i);
if(i==5)
break;
}

Output : 1 2 3 4 5
 Syntax
 continue statement
• The continue statement causes the loop to skip the statements
followed by it in the loop and goes to the iteration (step).

• The continue statement does not terminate the loop but it only skips
the statements followed by it.

• The continue statement can be used only with for loop, while loop and
do….while loop.
 Syntax
Example:

for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf(“%d”,i);
}

Output

1 2 3 4 6 7 8 9 10
Happy Reading!!!

You might also like