Your Only Destination To Empower Your Computer Programing Knowledge. Sky is not so high, You can Touch it if You Try

Control structure in C

This article is about control structure used in C.In previous article basic c program is discuss.But now we will discuss about control structure used in this language.

control structure


Friends,
As the name refers its meaning that a control structure is that which control the flow of execution.The flow of any instruction,Function,etc..There are two types of control structure.
  1. Decision control structure

  2. Counter control structure

Decision control structure


Decision control structure is defined as the control structure which depend upon certain decision.Decision control is also of two types.
  1. Control conditionally

  2. Unconditional control

Control conditionally:


Control conditionally means here that control is depend upon certain fixed condition.
Here we will discuss some of basic decision control conditionally structure which are following:-
  1. if control structure

  2. if-else control structure

  3. if-else-if or if-else ladder or nested if-else

  4. switch case control structure

If control structure


syntex:-
if(condition)
{
statement;
}
in this structure one condition is given.if the condition is satisfied then statement is executed otherwise not.There may be more than one statements.
ex.
if(numbers==33)
{
printf("pass");
}

If-else control structure


syntex:-
if(condition)
{
statement 1;
statement 3;
}
else
{
statement 2;
}
There may be a single or more than one statements.In this structure when condition is true than statement 1 and statement 3 executed,otherwise statement 2 executed.
Ex.
if(number%2==0)
printf("Number is even");
else
printf("Number is odd");
Note:-if there is single statement then we can write without braces.

If-else-if control structure


syntex:-
if(condition 1)
{
statement;
}
else if(condition 2)
{
statement;
}
else
{
statement;
}
In this control structure nested if else is used.There is follow a condition within a condition.
ex:-
if(Number<0)
printf("Number is Negetive");
else if(number%2==0)
printf("number is even");
else
printf("Number is odd");
single statement used so no need to apply braces.

Switch control structure


Switch control structure is also control the flow of execution.in this different cases are made and then matches are found for execution.The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution.
The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed.
syntex:-
switch( expression )
{
case constant-expression1:
statements1;
break;
case constant-expression2:
statements2;
break;
case constant-expression3:
statements3;
break;
default :
statements4;
}
break statement:-By this statement control is out of program.when match is found then break statement brings the control out of program otherwise all cases are tested.default case is for no match found condition which is used to avoid infinite looping which may create error.

Unconditional decision control structure:


This control structure is defined as the control structure in which there is no condition is provided for stopage. Go to statement is example of this.In this level no. is given to statement.
syntex:-
go to 10;
10:statement:
10 is here level number.Go to statement is not much used in programming because of it is uncontrolled.
So friends this is all about decision control structure.I hope You were enjoy it.

No comments:

Post a Comment