Branching


C language executes program statements in a sequence. Sometimes we need to alter the flow of sequence of statements. This is possible using Branching statements offered by C language. They are also known as control statements. Programmer can jump from one part of the program to other with the help of such statements. C language Branching Statements are given below:

1. if statement
2. if…else statement
3. nested if statement
4. switch statement


Let’s learn branching statement by executing following programs.


Programs based on if statement

Programs based on if…else statement

Programs based on nested if statement

Programs based on switch statement



Sample Programs




#include<stdio.h>
void main()
{
 int number;

 printf("Enter number:");
 scanf("%d",&number);

 if(number%2 == 0)
  printf("Entered number is even.");
 else
  printf("Entered number is odd.");
}
/* Output
Enter number:5
Entered number is odd.
*/





#include<stdio.h>
void main ()
{
 int number;
 printf("Enter your number:");
 scanf("%d",&number);
 switch(number)
 {
  case 1 :printf("One");
             break;
  case 2 :printf("Two");
             break;
  case 3 :printf("Three");
             break;
  case 4 :printf("Four");
             break;
  case 5 :printf("Five");
             break;
  default :printf("Invalid number\n" );
 }
}
/*Output
Enter your number:5
Five
*/




#include<stdio.h>
void main()
{
  int number;

  printf("Enter number:");
  scanf("%d",&number);

  if(number > 0)
      printf("Number is positive.");
  else
      printf("Number is not positive.");
}
/*Output
Enter number:11
Number is positive.
*/


5 comments:

  1. Nice blog, keep sharing all type of program which is useful for student,if you want to more information about c language then visit c training institute in pune

    ReplyDelete
  2. Replies
    1. Contact me with your offer through Contact us page of the blog.

      Delete
  3. Nice Post Keep Writing These Type of Post.
    You can visit my new blog if you want to know about Compiler And Its Requirment For C Or C++

    ReplyDelete