Sunday, December 30, 2012

C Program Control Structure : Explain If else with Example, Switch statement with Example

Control Structure in C Program ( if else, if ... else if ... else , switch case, conditional operator)
 
As it name suggest it used to control the flow of program execution. Lets learn by example,
If you want to purchase any Bike then you have to select any one options out of the list of possibilities. 1) Price of the Bike 2) Features / Style of Bike 3) Average km. 4) nearest shop / where to buy. So here you have to find out the answer of the above questions, some many be compulsory while some can be optional.  Depending on the requirement you have to choose one final options and have to Buy Bike.

The control structure of the C Language works on The Same Way !

Lets See  How many control structure provided by the C Language.
1) If... else
2) If... else if ... else..
3) if ... else if....
4) switch () (Switch statement)
5) ? : (Conditional Operator)


Basically there are only three control structure is provided by C Language 1) If...else.... 2) switch case 3) ? :  conditional control

1) If... else
If... else conditional structure is used to select one options out of two options, with reference to above example of Bike, If you want to purchase bike whose price is less than 20,000/-, Then you need to use this conditional structure. Let's check out with C Program to Demonstrate If.. else structure.

C Program If else Example :

#include <stdio.h>
#include <conio.h>

void main()
{
    int bikeprice1=10000;
    int bikeprice2=30000;
    if(bikeprice1 < 20000 )
    {
        printf("\nYou Can Purchase Bike 1 ");
    }
    else
    {
        printf("\nYou Can not Purchase Bike 1 because of Price Limit");
    }

    if(bikeprice2 < 20000 )
    {
        printf("\nYou Can Purchase Bike 2 ");
    }
    else
    {
        printf("\nYou Can not Purchase Bike 2 because of Price Limit");
    }

}

Some Assumption while writing my C Programs :
Here I have assumed Bike 1 cost is : 10000 and Bike 2 Cost is : 30000

Before Moving to Output :
* Can you guess what's should be the output of the above program !
* Think twice and write into the your book and compare with our output.

Lets Check out Output of the Above Program:
You Can Purchase Bike 1
You Can not Purchase Bike 2 because of Price Limit

Now Lets move to the next conditional structure : 2) If... else if ... else..

2) If... else if ... else..
if ... else if ... else is similar to previous one but it has more than one options from it you want to select one way, Lets take example for the same. If you have three ways to go for a specific location then you need to select any one options out of them, so at that time you need to go with the same option.

Lets understand with one example that is also more important for C Language.
Write a program to find that number is is positive, negative or zero. Here in program we have three answers while number is only one so we have to use 2) if... else if ... else conditional structure to solve this problem.

Lets understand with the example :

#include <stdio.h>
#include <conio.h>
void main()
{
    int no=5;
    if(no == 0 )
    {
        printf("Number is Zero");
    }
    else if(no > 0)
    {
        printf("Number is Positive");
    }
    else
    {
        printf("Number is Negative");
    }
}

Output of the program:
Number is positive

Extra Notes for Writing above Programs:
Above Programs can be written in 3 ways, If you want more clarification about this post your comments or sends message to us.

3) if ... else if....
Now Lets move to next Control structure 3) if ... else if....Its similar to 2) if...else if... else... but here last else condition is not present, I think you can do it for following types of questions.

Write a program to find out that Number is positive or Negative (Here i have omit the zero option)

4) Switch statement : Will be available soon

5) ? : (Conditional Operator) : Will be available soon


Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

1 comment:

  1. thank you for sharing nice article in your blog
    visit
    web tutorial programming
    https://www.welookups.com

    ReplyDelete