Branching(simple if,if else,nested if,else if,switch case )


         
                                          Branching
                                       (simple if,if else,nested if,else if,switch case )




In c language decision making is so important .Program should be able to do logical(bool or True \False) decisions.
“IF statement” and “switch statement” are the most popular conditional statements used in C.These statements are used in most of program.
Branch is the term given to the code executed in sequence as a result of change in the program’s flow; the program’s flow can be changed by conditional statements in that program.

                                                      IF Statement
IF Statement:-“If statement” is the selection statement used to select set of action depending on the given conditions . Therefore programmers can use this statement to control the flow of their program. If the program’s condition matches “if statement” condition then the code will be executed otherwise it will be ignored and the rest of the program will be executed.The if statement gives you the ability to test an expression(code), then select a statement or a group of statements to be executed if the expression evaluates to true. If you have the else part of the statement, you can execute another statement (or statements) if the expression evaluates to false.


  • Simple If
  • If else
  • Nested if
  • Else if
Simple if :- 

Syntax:-
    if(bool condition)\\if true statement will work if the condition is false the program ignores the statements
    \\statements;
Note:-If you are using more than one statement .You have to put curly braces.Like shown below

Syntax:- 
  
if(bool condition)\\if true statement will work if the condition is false the program ignores the statements
{
\\statement 1;
\\statement 2;
\\statement 3;
}
If else :- Else is used for other than if condition.the same applies to the else statement, so if you have only one statement to execute with the else statement, you can write it like this:

Syntax:- 


if(bool condition)
\\statement;
else
\\statement; 

But if you have multiple statements in your else part, you can write it as:

Syntax:- 

 
if(bool condition)
{
\\statement 1;
\\statement 2;
\\statement 3;
\\so on.... }
else{
\\statement 1;
\\statement 2;
\\statement 3;
\\so on......
} 
Nested if:-We can nest if statements as much as we want.

Syntax:- 

if(bool condition)
{ \\statement;

 if (bool condition)
{ \\statement;
if (bool condition)
     { \\statement;
        if (bool condition)
          { \\statement;
         }
          else{ \\statement;
              } 
      }
  } 
}
else{
\\statement ;
}

Example:-C program to find grade on the basis of percentage.
#include<stdio.h>
void main()
{float per;
printf("enter your percentage");
scanf("%f",&per);
if(per>=80)
{  printf("a+");  }
else{
        if(per>=75)
        {   printf("a");   }
        else{      if(per>=60)
               {   printf("b+");  }
               else{   if(per>=50)
                   {   printf("b");}
                       else{   printf("fail"); }
                   }
            }
    }
getch();
}
Else if:- We can combine else with if in one statement to execute more than one test on the expression. which must be written like "elseif"most of the people do common mistake you musn't be written like"ifelse".As you already know, if tests the expression, and if it evaluates to false, the else block will be executed. Sometimes this is not what we need, however. We may need to be very specific with the else part. For example, take a look at the following code:-

Example:-C program to find grade on the basis of percentage.

#include<stdio.h>
void main()
{float per;
printf("enter your percentage");
scanf("%f",&per);
if(per>=80)
{
;printf("a+");
}
elseif{per>=75&&per<80)
{
  printf("a");
}
elseif{per>=60&&per<75)
{
printf("b+");
}
elseif{per>=50&&per<60)
{
printf("b");
}
elseif{
printf("fail");
} 
getch();
}
                                              Switch Case 
Switch case statements are a substitute lon if statements that compare a variable to several "integer" values are simply values that can be expressed as an integer ,such as the value if a char. 

The switch statement is similar to multiple else if statements, but it accepts only expressions that will evaluate to constant values. The if/else structure is appropriate for small groups of Boolean expressions, but it would be very boring to write 20 else if statements to test for some values: else if(x ==5) do something else if(x == 6) do another thing, and so on. This is the syntax of a switch statement:

Syntax:- 

switch(variable)
{
case value1:
       \\statement;
         break;
case value2:
      \\statement;
default:
         \\statement;
} 

Example:-C program which accept any no. from user 1 to 7 .if number is equals to 1 than print "sunday",if number is equals to 2 than print "monday" and so on.if number is out of range than print "invalid value".

#include<stdio.h>

#include<conio.h>

void main()

{

int n;

clrscr();

printf(“Enter any number 1 and 7: to show day  “);

scanf(“%d”,&n);

switch(n)

{

case 1:

printf(“Sunday”);

break;

case 2:

printf(“Monday”);

break;

case 3:

printf(“Tuesday”);

break;

case 4:

printf(“Wednesday”);

break;

case 5:

printf(“Thursday”);

break;

case 6:

printf(“Friday”);

break;

case 7:

printf(“Saturday”);

break;

default:

printf(“invalid value”);

}

getch();

} 
                                       

Comments

Popular posts from this blog