CH - 2-Control Structure in C
CH - 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
True False
True Block Condition False Block
Statement-X
False True
Condition2
Statement-X
The Nested if….else statement is executed in following order :
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.
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. 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
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
Body Of Loop
True
Condition
False
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
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
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:
Condition False
True
Body of loop
Increment/
Decrement
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!!!