Translate

Labels

Showing posts with label CONDITIONAL STATEMENTS. Show all posts
Showing posts with label CONDITIONAL STATEMENTS. Show all posts

Tuesday, 1 January 2013

CONDITIONAL STATEMENT ( SWITCH )

SWITCH  STATEMENT

The switch_case statement is the modular replacement of the cumbersome nested if_ else structure.The switch and case statements help to control complex conditional and branching operations.The switch statement transfers control to a statement within its body.The syntax of the switch_case statement is as follows:

switch(conditional expression)
{
     case  constant-expression 1:
                                     ................
                                     break;
     case  constant-expression 2:
                                     ...............
                                     break;
      .
      default:
                                      ...............
}

The type of switch (conditional expression) and the case constant-expression must be integer type.Control passes to the statement whose case constant expression matches the value of switch(conditional expression). The switch statement can include any number of case statements, but no two case statements within the same switch statement can have the same constant-expression. Execution of the statement body begins at the selected case statement body begins at the selected case statement and proceeds until the end of the body  or until a break statement transfers the control out of the body.The break statement is used to end processing of a particular case statement within the switch statement.Program continues to the next case,executing the statements until a break or the end of the statement is reached.The default statement is executed  if no case is equal to the value of switch (conditional expression ).If the default statement is omitted, and no case match is found , none of the statements in the switch body are executed.The default statement and is an optional statement and it need not come at the end; it can appear anywhere in the body of the switch statement.The 
program to solve the simple arithmetic problem using nested if_else structure if rewritten using switch_case statement as follows:

#include<stdio.h> 
void main()
{
int  a,b,c,choice;
scanf("%d%d",&a,&b);       /*b is not zero*/
printf("1.addition\n");           /* option 1 */
printf("2.subtraction\n");      /* option 2 */  
printf("3.multiplication\n");   /* option 3 */
printf("4.division\n");           /* option 4 */
scanf("%d",&choice);
switch (choice)

{
case 1:
c=a+b;
printf("%d",c);
break;
case 2:
c=a-b;
printf("%d",c);
break;
case 3:
c=a*b;
printf("%d",c);
break;
case 4:
c=a/b;
printf("%d",c);
break;
default:
printf("The choice is out of range\n");
}

The above program is modular and has easy readability than the program written using nested if_else structure.Consider another example that displays whether the given character is a vowel or consonant.

char ch;
ch='a';
switch(ch)
{
case 'a':
case 'e':
case 'i';
case 'o':
case 'u':printf("the given character is vowel ");
break;
default:printf("the given character is consonant");
}

In the above example as the value of the character ch is 'a',the first case is satisfied and since there is no break statement, the program continues to the next case, executing the statements until a break or the end of the statement is reached.Hence, if the value of the character ch is 'a' or 'e' or 'i' or 'o' or 'u', the statements belongs to the case 'u'have been executed, otherwise, the statement belongs to the default case is executed.  

Monday, 31 December 2012

CONDITIONAL STATEMENTS ( IF )

IF STATEMENT

In C, the conditional statements rely on idea of boolean expressions.The if statements controls conditional branching.The boolean expression or in other words a relation expression will yield true or false value.In C, a value other than 0 is true and 0 is considered as false.The body of an if statement is excuted if the value of the expression is true i.e..,non_zero.There are two forms of if statement.

if (relation expression)
statement;

if(relation expression)
statement 1;
else
statement 2;

 In the first form of, if relational expression is true (non zero), the statement is executed. If the expression is false, the statement is ignored.In the second form ,which uses else, statement 2 is executed if the expression is false.With both forms,control then passes from the statement to the next statement in the program.
Here is a C program demonstrating a simple if statement:

#include<stdio.h>
void main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x>0)
printf("The value is positive\n");
}

This program accepts a number from user.It then tests the number using a conditional expression of the if statement to see if it is greater than 0.If it is,the program prints a message.Otherwise,the program is silent i.e..,there will be no output.The (x>0) portion of the program is the boolean expression.C evaluates this expression to decide whether or not to print the message.If the boolean expression evaluates to true, then C executes the single line immediately following the if statement (or a block of lines within braces immediately following the if statement ) .If the boolean expression is false, then C skips the line or block of lines immediately following the if statement.consider another example:

#include<stdio.h>
void main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x<0)
printf("The value is negative\n");
else if(x=0)
printf("The value is zero\n");
else
printf("The value is positive\n");


in this example the if_else_if construct is used which we can call as a nested if_else structure.The nested if_else structure is used to perform some operations based on choices.Consider a simple arithmetic problem in which add, subtract, multiply and divide operations are to be carried out depending on the choice.The options may be displayed.The program can be written in two ways:
using simple if:

#include<stdio.h>
void main()
{
int a,b,c,choice;
scanf("%d%d",&a,&b);       /*b is not zero*/
printf("1.addition\n");           /* option 1 */
printf("2.subtraction\n");      /* option 2 */  
printf("3.multiplication\n");   /* option 3 */
printf("4.division\n");           /* option 4 */
scanf("%d",&choice);
if(choice= =1)
c=a+b;
if(choice= =2)
c=a-b;
if(choice= =3)
c=a*b;
if(choice= =4)
c=a/b;
printf("The result =%d\n",c);
}  

The above program estimates the value based on the choice and prints the result.The number of comparisons made in the program is 4 irrespective of any choice.It performs comparisons unnecessarily.When (choice==1) is true, it is not necessary to evaluate the other conditions and may be skipped. The use of nested if_else construct will solve this problem.
using nested if_else construct:

  #include<stdio.h>
void main()
{
int a,b,c,choice;
scanf("%d%d",&a,&b);       /*b is not zero*/
printf("1.addition\n");           /* option 1 */
printf("2.subtraction\n");      /* option 2 */  
printf("3.multiplication\n");   /* option 3 */
printf("4.division\n");           /* option 4 */
scanf("%d",&choice);
if(choice= =1)
c=a+b;
else
if(choice= =2)
c=a-b;
else
if(choice= =3)
c=a*b;
else
if(choice= =4)                  /* comparison is optional */
c=a/b;
printf("The result =%d\n",c);
}  

 The above program uses nested if_else construct.if the first condition is true, that is ,(choice = = 1) is true the statement c=a+b; is executed and the statement following the else is skipped.Therefore, if the first condition is true, only one comparison is made and all the other comparison are skipped.Only when the first condition fails the program continues to compare the second condition and it goes on similarly.This program works faster than the previous program.but if choice is 4, both programs have to do four comparisons.