Wednesday 22 January 2014

Explain each decision statements with example.

It checks the condition,then executes its sub block.

if statement-It checks the condition,if condition is true then executes its sub block.
Otherwise when condition is false,the compiler skips lines within the if block.
It is enclosed in curly braces.
Use curly braces even with a single statement.

syntax
if(condition is true)
{
statement;
}

Write an example for if statement checking if entered number is less than 10.
#include<stdio.h>
#include<conio.h>
main()
 {
  clrscr();
  int a;
  printf("Enter the number:");
  scanf("%d",&a);
  if(a<10)
   {
    printf("Entered number is less than 10");
    else
    printf("Entered number is not less than 10");
   }
}

Output

Enter the number:5

Entered number is less than 10

if else statement-It has two blocks.if condition is true first block is executed otherwise if condition is false else block is executed.

syntax
if(condition is true)
  {
   statement1;
  }
 else
  {
  statement2;
  }

Write example for if else statement.Read values of a,b,c Add and check after addition if it is in the range of 100 & 200 or not.
 

#include<stdio.h>
#include<conio.h>
main()
 {
  clrscr();
  int a,b,c;
  printf("Enter the numbers a b c:");
  scanf("%d %d %d",&a.&b,&c);
  printf("\n a=%d b=%d c=%d",a,b,c)
  d=a+b+c;
  if(d>=100 & d<=200)
    {
    printf("\n Sum is in between 100 and 200");
    else
    printf("\n Sum is not in between 100 and 200");
    }
 }

Output
Enter the numbers a b c: 50 50 20
a=50 b=50 c=20
Sum is in between 100 and 200

nested if statement-If condiion is true then first block is executed.Otherwise in else block condition is checked with the if statement..

syntax
if(condition is true)
  {
   statement1;
  }
   else if (condition is true)
              {
              statement2;
              }
            else
              {
              statement3;
              }

Write an example for nested if else statement where find largest among 3 numbers.

#include<stdio.h>
#include<conio.h>
main()
 {
  clrscr();
  int a,b,c;
  printf("Enter the numbers a b c:");
  scanf("%d %d %d",&a.&b,&c);
  printf("\n a=%d b=%d c=%d",a,b,c)
 
  if(a>b)
    {
      if(a>c)
          {
          printf("\n Largest number is %d",a);
          }
          else
          {
          printf("\n  Largest number is %d",c ");
          }

    }
  else
    {
       if(c>b)
           {
           printf("\n Largest number is %d",c);
           }
           else
           {
          printf("\n  Largest number is %d",b ");
           }

   }
 
Output

Enter the numbers a b c:10 20 30
Largest number is 30

break statement-break skips from the loop in which it is defined and then goes automatically to first statement after loop.

continue statement-It is used for continuing in next iteration of loop statement and it skips the statement after this statement.

goto statement-Where label is the position where the control is to be transfered.

syntax
goto label;

where label name must start with any character

Write an example of goto statement checking the number whether even or odd.
   
    #include<stdio.h>
    #include<conio.h>   
    main()
     {
    int a;
    clrscr();    
    printf("Enter the number:");
    scanf("%d %d",&a);
      if(a%2==0)
             {
             goto even:
             }
               else
             {
             goto odd;
             }
    even:
    printf("\n %d is even");
    return;
    odd:
    printf("\n %d is odd");
    }

       Output
     
 Enter the number:4
       4 is even

Switch statement-Used to make a choice from a number of options.It need only one argument of any data type,which is checked with many case options.
   
    syntax
    switch(variable)
        {
        case constant a:
        statement;
        break;
        case constant b:
        statement;
        break;
        default:
        statement;
        }
Write an example for switch statement showing functions like addition ,subtraction
,multiplication,division,remainder.
    #include<stdio.h>
    #include<conio.h>
    main()
     {
          clrscr();
          int a,b,c,ch;
        printf("0 \n TERMINATED BY CHOICE");
          printf("1 \n FOR ADDITION");
          printf("2 \n FOR SUBTRACTION");
          printf("3 \n FOR MULTIPLICATION");
          printf("4 \n FOR DIVISION");
          printf("5 \n FOR REMAINDER");
          printf("6 \n FOR EXIT");
          printf("\n ENTER YOUR CHOICE")
        scanf("%d",&ch);
            if(ch<=6 && ch>=0)
                {
                    printf("Enter 2 numbers:");
                    scanf("%d %d",&a,&b);
                }
        switch(ch)
        {
       
        case 1:
        c=a+b;
        printf("ADDITION:%d",c);
        break;
       
        case 2:
        c=a-b;
        printf("SUBTRACTION:%d",c);
        break;

        case 3:
        c=a*b;
        printf("MULTIPLICATION:%d",c);

        case 4:
        c=a/b;
        printf("DIVISION:%d",c);
        break;
       
        case 5
        c=a+b;
        printf("REMAINDER:%d",c);
        break;

        case 0:
        printf("\n TERMINATED BY CHOICE");
        exit();
        break;
        default:
        printf("Invalid choice");
           }
    }
       

No comments:

Post a Comment