Decision Control Structure / Decision taking in C language / If-Else Statements / Making multiple paths in C language

Continuing from the previous article the precedence of operators would be highly beneficial. In your life you do everything according to the circumstances. You think like if this happens, I would do this, if not then another plan is in your mind. So C language also gives you this facility of making decisions according to circumstances. Up till now we have developed programs whose flow are sequential, means line written first, executes first. This is not the simple case in serious program. For serious programming you have to be able to make decisions, the decision control structures will make you able to decide which part should execute first. The decision control instructions can be implemented in C by using.

●     if statement

●     if-else statement

●     switch case structure

if Statement

Like most of the programming languages C uses if keyword for simple decision. In C languageif tells the compiler what follows is a decision condition, if it is true then execute the following statement otherwise skip that statement. Look at the syntax of if.

if(expression)

statement ;

OR

if(expression)

{

statement1 ;

statement2 ;

}

The text written with red color is mandatory to write. The part follow the if(expression) calledif block. When there is more than one statement in the if block the curl braces are mandatory to write.

Here is a program which illustrates the if statement.

Example

Write a program which takes a number as input if it is greater than 10 then display some text otherwise does nothing.

//  Header Files

#include<stdio.h>

#include<conio.h>

void main(void)

{

clrscr();

int num;

printf(“\nEnter a number”);

scanf(“%d”,&num);

if(num > 10)

printf(“\n You have entered number greater than 10”);

getch();

}

On execution,  if you enter number greater than 10, you will get two messages via printf(),

if number is less than 10  then program does not do anything.

Interesting Thing

We have written relational expression in above program. Interesting thing is that we can even write arithmetic expression as well. A non-zero number is considered as true and only zero is considered as false.

Examples

●     if(3 / 2 + 1 * 3) evaluates to 4

True;

●     if(2 – 5 + 30 % 4) evaluates to – 5 (also a non-zero number)

True;

●     if(5 % 2 / 3 – 2 / 4 ) evaluates to 0

False;

●     if(a = 12) evaluates to 12

True;

●     if(a != 0) evaluates to non-zero number

True;

If-else Statement

if else statement is very similar to if statement, we use if-else statement when we

want to do something also when the expression is false. Let see the syntax of if-else.

if(expression)

{

statement1 ;

statement2 ;

}

else

{

statement1 ;

statement2 ;

}

curl braces are not mandatory for one statement under if or else block.

Example

Write a program which takes a number as input, if it is greater than or equals to 10 then display a message else display another message.

//Header files must be included

void main(void)

{

clrscr();

int num;

printf(“\nEnter a number”);

scanf(“%d”,&num);

if(num >= 10)

printf(“\n You have entered number greater than 10”);

else

printf(“\n Number is less than 10”);

getch();

}

On execution,  if you enter number greater  than or equals to 10, user will get displayed a message via printf() and if the number is less than 10 then program also displays a message, means whether the expression is true or false user will be informed by appropriate message while in case of if statement when the condition is false user is not informed by the message, which may be annoying for user. That is why programmer rarely use if statement.

Important points

●     The statements after if excluding else form ‘if block’ and the part after else form ‘else block’.

●     Always follow the indentation of statements in the if and else block. This will help you to understand your code.

Nested if-else

When we construct the whole if-else in either if or else statement. This is called nesting of ifs.See this program to understand the concept of nested if-else.

//Header files must be included

void main(void)

{

clrscr();

int num;

printf(“\nEnter a number”);

scanf(“%d”,&num);

if(num > 10)

{

if(num == 10)

printf(“\n You have entered 10”);

else

printf(“\n You have entered number greater than 10”);

}

else

{

if(num >100)

printf(“\n Number entered is greater than 100”);

else

printf(“\n Number is less than 10”);

}

getch();

}

Working

●     Whole if-else is nested in first if. First if expression is evaluated.

●      If true then second if expression is evaluated.

●      If first if expression evaluated to false then third if is evaluated.

Alert !

Most students do have this problem while using if-else structure which I am going to discuss. See this program…

void main(void)

{

clrscr();

int num;

printf(“\nEnter a number\t”);

scanf(“%d”,&num);

if(num = 10)

printf(“\n You have entered  10”);

else

printf(“\n Number is not 10”);

getch();

}

Sample Output

Enter a number        10

You have entered    10

Enter a number        12

You have entered    10

Enter a number        23

You have entered    10

Confuse??? You have done wrong while writing expression in if.

You should have used equality operator, but you used assignment operator, which always evaluates to true until and unless you input zero to make the else block execute.

Case Control Structure

When we have only one condition to evaluate and many ways to go, we use case control structure or commonly known as switch case structure. See this example…

Example

Write a program that takes input a character. If it is vowel then display a message and if it is consonant then also display different message.

#include<stdio.h>

#include<conio.h>

void main(void)

{

clrscr();

char ch;

printf(“\nEnter a Character”);

ch = getche();

switch(ch)

{

case ‘a’  :   printf(“\n Vowel %c”,ch);       break;

case ‘e’  :   printf(“\n Vowel %c”,ch);       break;

case ‘i’   :   printf(“\n Vowel %c”,ch);       break;

case ‘o’  :   printf(“\n Vowel %c”,ch);       break;

case ‘u’  :   printf(“\n Vowel %c”,ch);       break;

case ‘A’ :    printf(“\n Vowel %c”,ch);       break;

case ‘E’ :    printf(“\n Vowel %c”,ch);       break;

case ‘I’   :   printf(“\n Vowel %c”,ch);       break;

case ‘O’ :   printf(“\n Vowel %c”,ch);       break;

case ‘U’ :   printf(“\n Vowel %c”,ch);       break;

default  :printf(“\n Consonant %c”,ch);

}//switch case ends

getch();

}//main ends

New Things in Program

●     In this program a character is taken input by another input function available in Turbo C compiler getche(), which is not a standard C input function.

●     It is present in conio.h header file.

●     The difference between getch() andgetche() i‘e’, it means getche() echo the character on screen while getch() does not.

●     Switch control structure starts with switchkeyword.

●      In the braces of switch a variable present whose different cases is to be checked.

●     After every case, break statement is necessary, if it was not used, it would have checked all the cases although the particular case had found.

●     Like else in if-else structure, case control structure has default.

●     Remember after default case the sense of using break is void.

Regards,

Umair Ahmed khan.

http://www.programmingdoctors.wordpress.com

Advertisements
  1. No trackbacks yet.

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

%d bloggers like this: