Skip to main content

How to Use Conditional Instructions.

How to Use Conditional Instructions. 

In C programs too, we must be able to perform a different set of actions depending on the circumstances. C has two major decision making instructions – the if-else statements and the switch statements.

In this article, we learn about if-else statements, multiple if-else statements and nested if-else statements.

a)   The if-else statements:- 

C uses the keywords if and else to implement the decision control instruction. the general form of this statement looks like this:

            if(this condition is true)

                    statements1;

            else

                    statements2;

the condition following the keyword if is always enclosed within a pair of parentheses. if the condition is true, then statement1 is executed. if the condition is not true, then statement2 has executed .as a general rule, we express a condition using 'relational' operators in C. its allow us to compare to values to see. in the following image shows how they look and how they evaluated in C.


b)      Multiple statements within if-else:-

it may be happen that is a program we want more then one statement to be executed when the expression following if is satisfied. if such multiple statements are to be executed, then they must be placed within a pair of braces, as illustrated in the following image.

Note:-in the if side statements not including else called if block, similarly else side statements called else block. else is exactly below the if or if block.

c)       Nested if-else:-

it is perfectly alright if we write another if-else construct within either the if block or the else block. this is called 'nesting' of ifs or nested if-else, which are shown in the below example

#include<stdio.h>
int main()
{
    int i ;
    printf("Enter either 1 or 2");
    scanf("%d",&i);
if(i==1)
    printf("welcome !\n");
else
{
    if(i==2)
        printf("get out\n");
    else
        printf("wow !\n");
}
return 0;
}


note that the second if-else construct is nested in the first else block. if the condition in the first if is fall, then the condition in the second if is checked. if it is false as well, then the second else is executed.

Comments

Popular posts from this blog

How To Know Best Tools For Social Media Marketing Part 2

  Best Tools For Social Media Marketing Part 2 Today we will tell you about social media marketing tools Part 2. in this article remaining 6 tools, which are helping in a social media campaign. Unsplash: - It is very important to have amazing images while having high-quality textual content. Unsplash provides a database of high-resolution, great images that you can use with your posts, the fact that a good image has its own unique significance cannot be denied. Each image is properly tagged so that you can find the most appropriate image for your post. Marin: - If your agency runs a social media advertising campaign (campaign), then this social media management tool will be useful for you. Marin offers a range of features that promise to increase your ROI by up to 30% compared to native equipment. One of its cool features is called auto-split which helps your audience in the micro-segment. Nimble: - As your customer list grows, a powerful CRM is needed to automate data manag...