Showing posts with label Decision Control Statements. Show all posts
Showing posts with label Decision Control Statements. Show all posts

Friday, July 10, 2020

Decision Control Statements, Break, Continue, Exit, Goto

Decision control structures (also called selection statements)allow to choose the set-of-instruction for execution depending upon an expression truth value.

The main decision control statements are:
  1. if-statement
  2. if-else Statement
  3. Conditional Operator
  4. Switch Case

1. if-statement:

  Syntax:
   if(expression i.e condition)
     statement i.e action;
2. if-else Statement:
  Syntax:
   if(expression)
      statement1;
   else
      statement2;
2.1 Nested if-else Statement:
  Type 1:
  Syntax:

   if(expression 1)
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   else
      body-of-else;
   }
  Type 2:
  Syntax:

   if(expression 1)
      body-of-if;
   else
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   }
  Type 3:
  Syntax:

   if(expression 1)
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   else
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   }
2.2 if-else-if ladder or if-else-if or Nested if:
  Syntax:
   if(expression)
      statement 1;
   else if(expression 2)
      statement 2;
   else if(expression 3)
      statement 3;
     .......
   else
      statement n;
3. Conditional operator:
The conditional operators ? and : are called ternary operators since they take three arguments.
      Syntax:

        expression 1 ? expression 2 : expression 3

      Example:

        y=(x<3 ? 1 : 7);
4. Switch case statement:
C provide a multiple-branch selection statement known as switch.
      Syntax:

switch(expression)
{
case constant 1:statement sequence 1;
break:
case constant 2:statement sequence 2;
break:
.......
.......
case constant n-1:statement sequence n-1;
break;
[default: statement sequence n];
}
Break statement:
Braek statment is used to terminate the execution of a statement block.The next statement block will be executed.
Keyword: break;
When a break ststement is executed in a loop, the repetition of the loop will be terminated.
Continue Statement:
A continue statement is used to transfer the control to the begnning of a statement block.
Keyword: continue;
When a continue statement is executed in loop, the execution is transfer to the beginning of the loop.
exit() :
An exit() function is used to terminate the execution of a C program permanently.
Keyword: exit();
It is a built-in function and is used/called with necessary argument as exit(0);
goto and label:
A goto statement can transfer the program control anywhere in the program.The target point of a goto statement is marked by a label.
Syntax:
goto label;
.....
label: