Control statement

Statement

A statement is a combination of variables ,operators,operands,conditions or functions which define a useful entity and tells compiler to perform a function.A control statement is a statement which restricts or allow the compiler to execute the particular block of code.With the help of control statements you can execute the code recursively instead of by default sequential execution.
Types of control statements
  1. Sequential statement
  2. Conditional statement
  3. Iterative statement
  4. Unconditional jump statements

Sequential statement: 

All the statements defined till now  are the sequential statement ,the order of statements written matters example you want to print your name ,it matters whether you have written your first name first or last name first. The flow of control goes sequentially from the starting of main function to the end of main function.

Conditional statement :

These are those statements which are based on certain conditions that is if this is true perform certain task.The control here also works sequentially by it may skip a certain block of code when condition is false.The conditional statements used are 

  1. if
  2. if..else
  3. if ..else leader
  4. switch
if :-If indicates that if particular condition inside the if is true the block defined in if is executed.Its prototype is

if(condition)
{
     ...//set of statements
}
condition can be an assignment ,logical statement or a relational expression.
example if date is 2 Oct then print it is National holiday

if..else:- It defines two block of statements .If condition is true the if block is executed ,otherwise the else block is executed.Its prototype is

if (condition)
{
  ...//set of statements     (if block)
}
else
{
   ..//set of statements (else block)
}

example:-To print whether number is odd or even.If modulus of number with 2 is zero then it is even else print it is odd.

if..else leader:It defines as many blocks as per requirement.If ist condition is not true the control goes to next condition and moves on so on unless and until a condition is true.If else block is defined and all if conditions are false then else block is executed.Whenever the control finds the condition is true it halts the execution of next statements.The prototype is:


if (condition)
{
  ...//set of statements     (if block)
}
else if(condition)
{
   ..//set of statements (if block)
}
else if (condition)
{
  ...//set of statements     (if block)
}
else
{
   ..//set of statements (else block)
}

example:finding greater among three digits

switch: It is somewhat similar to if else leader in functionality but it greatly differs in the syntax.It consist of cases .A default case is also there as similar to else part.It only differs in functionality from if else leader in the aspect that if break is not explicitly mentioned in each case ,all the cases will be sequentially executed by the compiler.The prototype is:

int a;
switch(a)
{
       case 1:
       {
                   ....//set of statements
                   break;
        }
         case 2:
       {
                   ....//set of statements
                   break;
        }
        default:
       {
                   ....//set of statements
                   break;
        }
}
here the variable passed to the switch case is integer,character but cannot be string.

Iterative Statement

Iterative statements are those statements which are recursively calling themselves unless and until the condition is evaluated to false.A task which needs a lot of human effort in typing ,can be done efficiently with the help of program.Assume you want to print numbers upto thousand ,sequentially you have to type printf and then numbers but with the help of iterative statements we place a condition and try to print numbers until the number displayed on screen is thousand.Following are the iterative statements used:
  1. for loop
  2. while loop
  3. do..while
for loop: Its syntax consist of 3 parameters ,variable initialization,checking condition and expression.It works from the variables initial value up till conditional value ,modifying the variable according to the expression defined.For loop in C(Turbo C) doesn't allow to declare and initialize the variable inside the loop ,It only allows initialization inside loop.The prototype is

variable declaration
for(initialization,conditional expression,expression)
{
    ...//statements to execute;
}

example:
int i;
for(i=0;i<=1000;i++)
{
       printf(" %d \t",i);
}
it prints 0 to 1000 with a tab (0            1             2...) \t is an escape sequence .

while loop: It is also an "entry control " loop.A condition is defined in the while loop ,unless and until the condition is true the statements in the while loop are executed .The prototype is:

while(condition)
{
      ...//statements
}

example: printing 0 to 1000 

int i=0;
while(i<=1000)
{
         printf("%d \t",i);
         i++;
}

do while loop: It is also an "exit control " loop.The control once goes inside the block and the block is executed atleast once.If the while condition is false the loop is terminated ,else the loop continues.The prototype is

do
{
      ...//statements
}while(condition);

example: printing 0 to 1000 

int i=0;
do
{
         printf("%d \t",i);
         i++;
}while(i<=1000);

Unconditional statements

These statements are condition less and the control of the program goes to the particular location defined by the label.This is not recommended in the programming because our program might stuck at some point.The goto statement is used:
syntax :goto a
            ..//set of statements
           a: printf("STATEMENTS SKIPPED");

0 comments:

Post a Comment