Decision Control Structure

Decision Control Structures

We made out many programs, but those programs were executed sequentially, means the same order in which they appear in the program. But what when we want to condition our program, that the particular part or block of statements executes only if the condition is true.

Let’s tak a example, I think you all love to watch movies ? And many ah times you prefer to go to cinemas theater, when you plan to watch ah movie in cinema theater, you condition the event that if we are able to get the tickets, we will go to watch movie otherwise not. Also you can condition that or else if we are able to get tickets in black then also we will go to watch 😀

What if we want to condition our program the same way, Can We ? Of course we can. C allows three major decision instructions for this — the if statement, the if-else statement, and the switch statement. A fourth is also available, but less important structure, the conditional operators.

Take a program that we want to print “Number is Positive” if the number is greater than 0(zero) otherwise we want to print “Number is Negative”. There is also a possibility that the number can also be equals to zero, for this there is also a function, else if, to condition effectively.

Remember in the c language non-zero value represents true and zero value represents false. Means if in the if statement the condition evaluate to a non-zero value, whether it is greater than zero or less then or positive or negative, the condition will be assumed to be true and the if part will be execute and if the value is zero the condition assumed to be false and then the else part will be execute.

Syntax :

if there is multiple statements to be executed, if the condition is true then we use a curly brace after the if statement to execute all, as

if(condition)

{

statement 1;

statement 2;

…..

}

Or if there is only one statement to be executed, the braces are optional, we can simply written

if(condition)

statement;

And to condition effectively, we know if the number is not greater to zero, then ofcorse it will less then zero for this we can use the else part the way

if(condition)

{

statements; …..

}

else

{

Statements; …

}

Let’s Understand it by The Program ►►

#include<stdio.h>
#include<conio.h>
 
void main()
{
   int num;
   printf("Enter any Number\n");
   scanf("%d",&num);

   if(num>0)
   printf("Number is Positive");

   else if(num==0)
   printf("Number is zero");

   else
   printf("Number is Negative");
 
   getch();   
}

Here the number entered through keyboard will be check in the if statement that whether it is greater than zero, if it is the following printf(“Number is Positive”); will be execute otherwise the else if condition will be check and if num is equals to zero, the printf(“Number is Zero”); will be execute. If none of the above condition turns to be true then the else part will be execute means the number is negative.

Remember to compare two values, always use == (comparison operator), not the single one = (assignment operator). This is the most basic problems with the learner that they use the assignment operator =, instead of comparison operator == for comparing two values.

One More thing to note here never put a semicolon after the if statement as if(condition) ;, because then the scope of if will be up to the semicolon,  and the further statements will be execute normally, by default through sequential process. And the if statement will be assume as null if.

You can use the single statement if, for only one condition in your program or can use the else part with it for two conditions, or also use multiple if in your program, or else if to check many conditions and execute the only one true condition, it totally depends on you and the program logic.

►►Write Your Program◄◄

 

►► Make a Program to input the float value and then compare them that whether it’s positive or negative.

Nested of if-else

 

â—„â—„Prev

Advertisements

3 thoughts on “Decision Control Structures”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

spreading the power of programming …

%d bloggers like this: