Friday, November 8, 2013

Decision Making Statements in C

Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

C programming language assumes any non-zero and non-null values as true and if it is either zero or null then it is assumed as false value. C programming language provides following types of decision making statements.

if...else statement

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax:
if(boolean_expression)
{
statement(s;
}
else
{
statement(s);
}

If the boolean expression evaluates to true then the if block of code will be executed otherwise else block of code will be executed.
C programming language assumes any non-zero and non-null values as true and if it is either zero or null then it is assumed as false value.

Example:
#include <stdio.h>
int main ()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}

When the above code is compiled and executed, it produces following result:
a is less than 20
value of a is : 10

The if...else if...else Statement:
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax:
if(boolean_expression 1)
{
executes Statements;
}
else if( boolean_expression 2)
{
executes Statements;
}
else if( boolean_expression 3)
{
executes Statements;
}
else
{
executes Statements when the none of the above condition is true
}

Example:
#include <stdio.h>
int main ()
{
int a = 100;
if( a == 10 )
{
printf("Value of a is 10\n" );
}
else if( a == 20 )
{
printf("Value of a is 20\n" );
}
else if( a == 30 )
{
printf("Value of a is 30\n" );
}
else
{
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
return 0;
}

When the above code is compiled and executed, it produces following result:
None of the values is matching
Exact value of a is: 100

switch Statement
The switch statement is a better way of writing a program which employs an if-else ladder. It is C’s built-in multiple branch decision statement. The syntax for the switch statement is as follows:

switch (integer expression)
{
case constant1:
statement1;
break;
case constant2:
statement2;
break;
...
default:
statement;
}

The keyword break should be included at the end of each case statement. In general, whenever a break statement is encountered in C, it interrupts the normal flow of control. In the switch statement, it causes an exit from the switch statement. The default clause is optional.

Here is a simple example of a switch statement:
#include<stdio.h>
void main()
{
int n;
printf(“ Enter 1 or 2 or 3 n”);
scanf(“%d”, n);
switch(n)
{
case 1:
printf("value is ONEn");
break;
case 25:
printf("value is TWOn");
break;
case 99:
printf("value is THREEn");
break;
default:
printf("number is not availablen");
}
}

No comments:

Post a Comment