Sunday, 20 December 2015

Conditional Branching statements in C Programming

C Supports two types of decision control statements that can alter the flow of a sequence of instructions. These include conditional type branching and unconditional type branching. In this post I will explain about the first one.

The conditional branching statements help to jump from one art of the program to another depending on whether a particular condition is satisfied or not. Generally they are two types of branching statements.

1. Two way selection
    - if statement 
    - if-else statement 
    - if-else-if statement or nested-if
2. Multi way selection
    - switch statement 

Decision Making with if statement

                                                The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two way decision statement and is used in conjunction with an expression. It takes the following form

                      if(test-expression)

It allows the computer to evaluate the expression first and them depending on whether the value of the expression is "true" or "false", it transfer the control to a particular statements. This point of program has two paths to flow, one for the true and the other for the false condition.

Simple if statement 

The general for of a simple if statement is

if(test-expression)
{
statement-block
}
statement-x;
The statement-block may be a single statement or group of statements. If the test expression is true, the statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x. But when a condition true both the statement-block and the statement-x are executed in sequence.

Example: C Program to check equivalence of two numbers using if statement 


void main()
{
int m,n,large;
clrscr();
printf(" \n enter two numbers:");
scanf(" %d %d", &m, &n);
if(m>n)
large=m;
else
large=n;
printf(" \n large number is = %d", large);
return 0;
}

The if-else statement 

                                 The if-else statement is an extension of the simple if statement. The general form is 
                                 
if(test-expression)
{true-block statements
}
else
{
false-block statements 
}
statement-x

If the test-expression is true, then true-block statements immediately following if statement are executed otherwise the false-block statements are executed. In other case, either true-block or false-block will be executed, not both.

Example: C program to find largest of two numbers 
void main()
{
int m,n,large;
clrscr();
printf(" \n enter two numbers:");
scanf(" %d %d", &m, &n);
if(m>n)
large=m;
else
large=n;
printf(" \n large number is = %d", large);
return 0;
}

Nested if statement 

                            C language supports if-else statements to test additional conditions apart from the initial test expression. The if-else construct works in the same way as normal if statement. nested if construct is also know as if-else-if construct. The general for is 

                                       If(test-condition-1)
                                       {
                                              if(test-condition-2)
                                              {
                                                    statement-1;
                                               }
                                              else
                                                {

                                                     statement-2;
                                                 }       

                                        }
                                        else
                                         {

                                            statement-3;
                                        }
                                      statement-x

If the test-condition-1 is false, the statement-3 will be executed; other wise it continues the second test. If the condition-2 is true, the statement-2 will be evaluated and then the control is transferred to the statement-x.

Dangling Else Problem 

                                  The classical problem which encounter when working with nested if statement is that dangling else problem. This will occur when the number of if statements does not match with number of else statements. The only solution to this problem is that, enclose the two statements properly.   

                  In my nest post I will explain multi way selection. If have any question you can feel free is ask as comment in below. Happy programming.!!!!

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top