Friday, September 18, 2020

else-if ladder in c-programming

 else-if ladder :

There is another way of putting if's together when multipath decisions are involved.

A multipath decision is a chain of if's in which the statement associated with each else is an if.

It takes the following general form.

if( condition 1 )

     statement 1 ;

     else if ( condition 2 )

                     statement 2 ;

          else if ( condition 3 )

                      statement 3 ;

             else if ( condition n )

                       statement n ;

                else

                       default statement ;

statement-x

This construct is known as else if ladder.

The conditions are evaluated from the top to bottom.

As soon as a true condition is found , the statement associated with it is executed and control is transferred to the statement-x.

When all the conditions are false , then the final else containning the default statement is executed.

Following figure shows the logic of execution.

Concider the following example 

if ( n==1 )

             printf ("Monday");

else if ( n==2 )

            printf ("Tuesday");

else if ( n==3 )

            printf ("Wednesday");

else if ( n==4 )

            printf("Thursday");

else if ( n==5 )

            printf("Friday");

else if ( n==6 )

           printf("Saturday");

else if ( n==7 )

           printf("sunday");

In above example accordig to the value of n day name will be displayed.


 

No comments:

Post a Comment

Object Paradigm Oriented Programming in C++ / object oriented programming / History of C++/ paradiagram in c++/C devolop, Operating System, Unix operating system, Assembly language, popular in world, Programming language.

  Object Paradigm Oriented Programming  Two programmers, Brian Kernighan and Dennis Ritchie developed a new language called C, in 1972.  C l...