We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
Introduction
>» Mostly C program is a set of instructions which are normally executed
sequentially in order , but sometimes we will have situations where we will
have to change the order of execution based on certain conditions or repeat a
group of statements until some conditions are met. Decision making
capability of C language is used in such situation (Control statements)1. Simple IF statement
if(test expression)
{
Statement-block;
}
Statement x;2. The if ... else statement
if(test_expression)
(
True block statement(s)
3
else
{
3
False Block statements) |Nested if... else statement
{M( test condition 1)
t
ifitest condition 2)
t
‘Statement 1;4. The else if ladder
if(condition_expression_One)
Steer :
else if (condition_expression_Two)
‘statement2;
‘ese if (condition_expression_Three)
«statement
else
statement4;switch statement
‘Multiway decision statement
Best used when number of alternatives increases
switch statement tests the value of a given variable against a list of case values and when a
match Is found a block of statement associated with that case Is executed
switch(expression)
{
case label 1 : block1;
break;
case label 2 : block2;
break;
case label n : block n;
break;
default : default block;
break;JUMPS IN LOOPS
Break and continue: break and continue are unconditional control construct.
i, break
This statement is useful to terminate a loop and transfer control out of loop under special
break statement works with while, do....while, for and switch statements. Following pr
diagrammatically represents the working mechanism of break statement.
main()
‘while(condition! ==true)
{
statement];
statement2;
iffcondition2=—tue)
{
)
statement4;
{
sutementS;JUMPS IN LOOPS
Break and continue: break and continue are unconditional control construct.
ii, Continue
Statement is helpful to skip particular set of statements in loop body and to
continue execution from the beginning of loop.
Following syntax clearly depicts how control shifts to beginning of a loop on
finding continue statement
«mo
hlfcontont == ue
‘statoment1;
statement2;
if(condition2= =true)goto statement
Syntax
goto label ;
label : statement;
Forward Jump
goto label
Label:
Statements;
statement5;
Backward Jump
statement;
if(condition)
goto label;
statement2;
statement 3;
statement4;
label: —
Label:
Statements;
goto labcl:
The goto statement
breaks the normal flow of
execution in the program
and takes the control to
statement5, without
executing the
statements 3 and 4.return statement
>
The return statement is used in a function to return value to the calling function.
The return statement can be written anywhere and often inside the function definition.
Still, after the execution of the first return statement, all the other return statements will be terminated
and will be of no use.
‘The reason why we have the two syntaxes for the return statement is that the return statement can be ust
in two ways.
rie is that you can use it to terminate the execution of the function when the function is not returning ar
value.
return;
Second, it is to return a value with function calls, and here, the datatype of expression should be the sam
as the datatype of function.
return expression;
Here the expression can be any variable, number, or expression. All the return examples given below are
valid inC.
return x;
return 0;
return x++;
return(x+y);Loop (Iterative) Statements
Definition of Loop: Is a programming structure used to repeatedly carryout 2
particular instructon/statement uni condition is tue. Each single repetition
lf the lop is known asa eration of the lop.
‘Three important component of any loop are
1. Intitization (example: ct, +0 ete)
2 Test Contin (example: ctr<=500, | I= ete)
3. Updating oop contrel values (example: etrctrot, it)
Loop (Iterative) Statements (Contd...)
Pretertand os tet ope
Cesta en he epi ch se cle rete lms ao Kn et
omar
thew etc ge a he ed fea eh eae tome a pst et ap
Sete
while loop
Its. pre-test loop (also nown as entry controlled loop)
> This lop has following stan:
while (condition)
statement-block;
Inthe syntax ven above while’ fsa key word and conition is at begining of the oop
> Ifthe test conditions true the body of while lop wil be executed.
> After execution ofthe body, the test condition is once agin evaluated and ti tee,
‘executed once again.
> This process irepeated until condition finally becomes false and control comes out
oF the oop.
While loop
wie nin
{ >.
statement lok,
}do.... while loop
do.... while loop: It is a post-test loop (also
called exit controlled loop) it has two keywords
do and while.
The General syntax:
do
{
statement-block;
} while (condition);for loop
It is another pre-test loop (also called entry controlled loop) that provides concise loop
control structure.
It has one keyword for.
One important aspect of for loop is all the three components of a loop (viz. ii
testing condition and updation (increment/decrement))is given in the head of for loop.
General Syntax:
(iti te coin pain)
{ \
Bylo
1
A set of statements will be executed for a set number of times
Flowchart of for loop
for loop (Contd...)
‘The execution of for statement is as follows.
1. In this loop first initialization of control variable is done first, using
assignments such as i=1, count=0. The variable 1 count are called loop control
variable.
2. The value of control variable is tested using the test condition. The test
condition is evaluated. If the condition is true, the body of the loop is executed;
otherwise loop will be terminated. }
3. When the body of the loop is executed, the control transferred back to the for
statement to update the loop variable. And then condition is checked once
again. If condition is true once again body will be executed once again. This
process continues till the value of the control variable fails to satisfy the test
condition.Anested loop means a inside another loop statement. That is why
nested loops are also called “loop inside loops"
We can define any number of loops inside another loop.
Nested for Loop
Nested for loop can be used to control the number of times that a particular set of
statements will be executed.
‘Another outer loop could be used to control the number of times that a whole loop is
repeated
NOTE:
Loops should be property indented so that a reader can easily determine which
statements are contained within each of the statement
[Although this feature will work with any loop such as while,do-while and for, it fs most
commonly used with the for loop, because this is easiest to control.
=>
=
‘ —_p
&
Ses
a
2 |
fortinitialization;condition;increment)
{
Statement of inside loop
1 Statement of outer loop