Wednesday, 10 September 2014

Control Structures in C:

Control Structures in C:
Normally statements in C program are executed sequentially i.e in the order in which they are written. This is called sequential execution. We can transfer the control point to a desired location is possible with control structures. C allows many kinds of control statements.
I. Conditional(decision ) control structures
· if
· if –else
· if-elseif – else
· nested if
II. Jumping(branching) control structures
· goto
· break
· return
· continue
III. Loop(iterative) control structures
· for loop
· while loop
· do - while loop
IV. Multi way conditional(case) control structures
· switch- case
Conditional Control Structures:
If: if is also called as conditional statement in if statements get executed only when the condition is true, it can terminate when the condition becomes false.
Syntax:
if (condition)
{
        ………..
        …statements…
        ………….
}

if –else: in if-else conditional control statement, statements in if block gets executed only when the condition is true and statements in else block gets executed only when the condition is false.

Syntax:
if (condition)
{
            statements
}
else
{
             statements
}

if –else if-else: in this statement statements in loops gets executed when the corresponding conditions are true. Statements in final else block gets executed 
when all other conditions are false. We can use any no. of else-if blocks between if and else.

Syntax:
if (condition1)
{
       statements
}
else if(condition2)
{
         statements
}
else if(condition3)
{
        statements
}
….
else
{
statements
}

nested if : writing the if statement with in another if is called as nested if. Inner if is processed only when outer if condition is true. Hence statements of inner if gets executed when outer if and inner if conditions are true.

Syntax:
if (condition1)
{
if(condition2)
{
statements
}
else
{
statements
}
}
else
   statements

II. Jumping Control Structures:

goto :
goto os a kind of branching structure. It moves the control (moves
forward or back)to label. goto is condition dependent.
In above goto forward statements2 gets omitted because after executing
statements1 goto moves the control to label statements.
In goto back after executing statement1, statements2 repeated more times
depends on condition(goto transfers to label, if condition is true only).

Syntax1 : goto back syntax: goto forward
    ……..
…statements1..
   ..
label:
   ..statements2
    ..
if(condition)
         goto label;
..
EXAMPLE:
i=0;
abc:
printf(“hello”);
i++;
if(i<10)
goto abc;
……statemnts1..
if(condition)
goto label;
..
.. statemnts2..
label:
…….
…..
EXAMPLE:
i= -3;
if(x>0)
goto abc;
x=-x;
abc:
printf(“%d”,x);

Break :
Break quits the corresponding iterative loop statements, break is valid
only for loop statements and switch case .
……..
……..
return :
return is used in functions. It moves the control to calling function from
the called function module. Return is also return values to the point where the
function is called.
Ex: main( )
{
int a=10,b=20;
printf(“%d”,sum(x,y);
getch( );
}
int sum(int x, int y)
{
return x+y;
}
Iterative (loop) control statements :

for loop :
in for loop statements get executed as long as condition is true. For loop
contains three expressions, expression1 includes initializing the variables and
expression 2 includes increment or decrement of initialized variable.

Syntax:
for( expression1; condition; expression2)
{
statements;
}
example :
for(i=1;i<=10;i++)
printf(“hello”); prints hello 10 times.
….
….
break;
…..
…..
EXAMPLE:

for(i=1;i<10;i++)
{
printf(“hello”);
if(i==5)
break;
}
Prints 5 times only

While loop :
In while loop statements get executed as long as condition is true. In while it checks the condition first and executes the statements later. So minimum no. of execution times of statements is 0.

Syntax:

While(condition)
{
statements;
}

EXAMPLE:

i=1;
while(i<=10)
{
printf(“hello”); prints hello 10 times.
i++;
}
do while loop:
In do while loop statements get executed as long as condition is true. In do while it executes the statements first and checks the condition later. So minimum no. of execution times of statements is 1.
Syntax:
do
{
….
statements;
….
}while(condition);

EXAMPLE:

i=1;
do
{
printf(“hello”); prints hello 10 times.
i++;
} while(i<=10);

multi-way conditional control statement:

switch case:
switch case is similar ti else if. In switch case statements get executed when corresponding case constants are true only(which have been mentioned match with the value of variable in switch). Control point comes to default when all the cases are false.
startcondition statementstruefalseendstartstatements conditiontruefalseend

Syntax:
Switch(variable/expression)
{
case constant1:statements1;
break:
case constant2: statements2;
break:
case constant3: statements3;
break:
default: statements;
break:
}
EXAMPLES:

program to perform arithmetic operators using switch case.
main( )
{
char op;
int a=10,b=5;
clrscr( );
printf(“enter your operator +,-,*,/,%”);
scanf(“%c”,&op);
switch(op)
{
case ‘+’: printf(“sum=%d”,a+b);
break;
case ‘-’: printf(“difference=%d”,a-b);
break;
case ‘*’: printf(“mul=%d”,a*b);
break;
case ‘/’: printf(“div=%d”,a/b);
break;
case ‘%’: printf(“rim=%d”,a%b);
break;
default: printf(“your option is wrong”;
break;
}
getch( );
}
start
Case 1 Statements 1
true
end
Case 2
default
false
false
true
Statements 2
Default statements

No comments:

Post a Comment