Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Decision Making and Branching using if statement , switch statement , conditional operator and goto

Posted By: Adela Fischer     Category: C Programming     Views: 9324

This article explains about decision making and branching is performed using if statement, switch statement, conditional operator and goto.

-  The natural sequential execution of a C prog
-  Changing order of execution is a need
-  Repeating a group of statement until specified conditions are met is also a need
-  The Control or Decision Making statements
-  If, switch, Conditional Operator statement, goto statement

if Statement

  • if (condition) {statement block}
  • if (condition) {true statement block}  else {false statement block}
  • Nested if is possible
  • if (condition1) {statement block}    else if (condition2) {statement block2}    else if (condition3) {statement block3} …


Switch Statement

It’s a multi-way decision making statement
switch (expression)
{
   case value1:block1
break;
   case value2:block2
break; 
   …..
   default: blockn
 }

Example

 index = marks/10
 switch(index)
{ case 10:
           case 9:
  case 8:
  case 7: printf (“Distinction”)
  case 6: printf (“First Class”)
  case 5: printf (“Second Class”)
           case 4: printf (“Third Class”)
   default: printf (“Fail”    }

Conditional Operator and goto

  •  if (x<0) flag = 0; else flag = 1; is equivalent to flag = (x<0) ? 0 : 1
  • Nested operations are possible
  • goto Initiate_it; is a statement directs control to  label Initiate_it:
  • It’s unconditional branching
  • It’s only advised to be be used in specific conditions

  
Share: 



Adela Fischer
Adela Fischer author of Decision Making and Branching using if statement , switch statement , conditional operator and goto is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!