3. Decision statements

 operators and expression :


Operator  is a symbol that tells the compiler to perform all mathematical or logical tasks.


Types  of operators :


Types of operator

symbolic representation 

Arithmetic operator

+, -, *,  /, %

Relational operator

>,<, = =,>=, <=,  !=

Logical operator

&& , || , !

Increment and decrement Operator 

++ and --

Assignment operator

=

Bitwise operator

&, >>,<<, |

Conditional operator

?, :

Special operator 

& , size of, * (pointer)


                            


 Decision statements  :

  

  • If statement 
  • If….else statement 
  • Nested if statement 
  • If-else-if ladder
  • Switch statement 
  • Jump statement 





Importent Keywords:


    1)break

    2)continue

    3)goto

    4)return 


1) if statement :  


if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. 



if(condition) 
{
   // Statements to execute if
   // condition is true
}


Example

Write a c program to check whether the entered number is less than 10


#include<stdio.h> 

int main() {
    int n;
    printf("enter the numbers\n");
    scanf("%d",&n);
    if(n<10){
    printf("entered no is less than 10\n");
    }
    return 0;
    }

Output :

enter the numbers

9

entered no is less than 10


Write a c program to check equivalence of two numbers


#include<stdio.h> 
int main() {
    int n,m ;
    printf("enter the numbers\n");
    scanf("%d%d",&n,&m);
    if(m==n){
        printf("both are equal");
    }
     return 0;
}


Output :

enter the numbers

5

5

both are equal


Write a c program to check whether the  candidate age is greater than 17

or not .if yes,display message “Eligible for voting’’.



#include<stdio.h> 
int main() {
    int age ;
    printf("enter the age\n");
    scanf("%d", &age);
     if(age>17){
         printf("eligible for voting\n");
     }
     return 0;
}

Output : 

enter the age

32

eligible for voting



2) if– else statement : 


The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the C else statement. We can use the else statement with if statement to execute a block of code when the condition is false.


if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}


Example


Write a c program both value same or not :


#include<stdio.h> 

int main() {
    int a,b;
    printf("enter the value of a\n");
    scanf("%d",&a);
     printf("enter the value of b\n");
    scanf("%d",&b);
    if(a==b){
        printf("both are equal\n");
    }
    else {
        printf("both are not equal\n");
    }

    return 0;
}


Output :


enter the value of a

2

enter the value of b

2

both are equal



Write a c program  to check whether smaller or greater:


#include<stdio.h> 
int main() {
    int  a = 20;
    if (a < 15){
        printf("a is smaller than 15");
    }
    else{
        printf("a is greater than 15");
    }       
    return 0;   
}

Output : 


a is greater than 15



Write a c program to check whether number is even or odd :



#include<stdio.h>
int main (){ int n; printf("enter the numbers\n"); scanf("%d",&n); if(n%2==0) { printf("%d is a even no\n",n); } else { printf("%d is not a even no\n",n); } return 0; }

Output :


enter the numbers

5

5 is not a even no


Write a c program to check a person is eligible for voting or not 




#include<stdio.h>
int main (){ int age; printf("enter the age\n"); scanf("%d",&age); if(age>=80) { printf("eligible for voting",age); } else { printf(" not eligible for voting",age); } return 0; }

Output :


enter the age

88

eligible for voting


Write a c program to check vowels or not:



#include<stdio.h>
int main (){ char c ; printf("enter the alphabet\n"); scanf("%c",&c); if(c=='A'|| c=='a'||c=='E'||c=='e'||c=='I'|| c=='i'||c=='O'||c=='o'||c=='U'||c=='u') { printf("it is a vowels "); } else { printf("it is a consonant "); } return 0; }

Output :


enter the alphabet

a

it is a vowels 


Write a c program to calculate the salary of an engineering representative based on the sales . bonus and incentive to be offered to him will be based on total sales.if the sales exceed Rs=10,000/-follow  the particulars of table (1) and (2)


Table 1                                        Table 2


Basic = 3000                             Basic = 3000

Hra     = 20% of basic               Hra   = 20% of basic 

Da     = 110% of basic              Da     = 110% of basic 

Conveyance= Rs = 500             Conveyance= Rs = 500

Incentive = 10% of sales           Incentive = 5% of sales 

Bonus = RS 500                          Bonus = RS 200



#include<stdio.h>
int main (){ float bs,hra,da,cv,incentive,bonus,sale,totalsales; printf("enter the total sales\n"); scanf("%f",&sale); if(sale > 1000000){ bs=3000; hra=20*bs/100; da=110*bs/100; cv=500; incentive=sale*10/100; bonus = 500; } else{ bs=3000; hra=20*bs/100; da=110*bs/100; cv=500; incentive=sale*5/100; bonus = 200; } total sales = bs+hra+da+cv+incentive+bonus; printf("\ntotal sales %f",sale); printf("\n basic salary %f",bs); printf("\n hra %f",hra); printf("\n da %f",da); printf("\n cv %f",cv); printf("\n inc %f",incentive); printf("\n bonus %f",bonus); printf("\n gross salary %f", totalsales); return 0; }


Output: 


enter the  total sales

10,0000

total sales 10.000000

basic salary 3000.000000

hra 600.000000

da 3300.000000

cv 500.000000

inc 0.500000

bonus 200.000000

gross salary 7600.500000



3) Nested if-else statement   

It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).



if( boolean_expression 1){
    /* Executes when the boolean expression 1 is true */
    if(boolean_expression 2) {
        /* Executes when the boolean expression 2 is true */
        }
}

Example 


Write a program to calculate energy bills. Read the starting and ending meter reading the charge are as follows 


No of units consumed          rates in (rs)


200-500                                 3.50

100-200                                 2.50

Less than 100                        1.50


#include<stdio.h> 
int main() {
    int initial,final,consumed;
    float total;
    printf("enter the initial readings\n");
    scanf("%d",&initial);
     printf("enter the final readings\n");
    scanf("%d",&final);
    consumed = final - initial;
     if(consumed>=200 && consumed<=500)
        total = consumed*3.50;
        else if(consumed>=100 && consumed<1200 0="" bill="" consumed="" d="" else="" f="" getchar="" if="" is="" of="" printf="" return="" total="" units="">

Output :


enter the initial readings

800

enter the final readings

850

total bill of 50 units is 75.000000 


4) if-else if Ladder


All the values of the variable check with different conditions.


Example 


Write a program to find the largest number out of three numbers Read the numbers through the keyword.


#include<stdio.h>
int main (){ int a,b,c; printf("enter the value of a:"); scanf("%d",&a); printf("enter the value of b:"); scanf("%d",&b); printf("enter the value of c:"); scanf("%d",&c); printf("largest out of three numbers: "); if(a>b){ if(a>c){ printf("a = %d\n ",a); } else{ printf("c = %d\n ",c); } } else{ if(c>b){ printf("c = %d\n ",c); } else{ printf("b = %d\n ",b); } } }


Output:


enter the value of a:12

enter the value of b:23

enter the value of c:56

largest out of three numbers: c = 56


5) Switch Statement :


A switch statement is a multiway branch statement in the program if there is a possibility to make a choice from a number of options.

Executed default statement . 


Syntax:


Switch (variable name)
Case 1:
//Statement;
Break;
Case 2:
//Statement ;
Break;
Default:
//Statement:

Write a program to make a simple calculator.

#include<stdio.h>
int main(){ int a,b,c; char op; printf("\nenter the operator\n"); scanf("%c",&op); printf("enter the value of a: "); scanf("%d",&a); printf("enter the value of b: "); scanf("%d",&b); switch(op){ case '+': c = a+b; break; case '-': c = a-b; break; case '*': c = a*b; break;         case '/': c = a/b; break; case '%': c = a%b; break; } printf("calculation is %d ", c); return 0; }

Output :


enter the operator

+

enter the value of a: 56

enter the value of b: 78

calculation is 134 



Write a program to display the names of the days of week.


#include<stdio.h>
void main (){ int day,i; printf("enter the no between 1 to 7: "); scanf("%d",&day); switch(day){ case 1: printf("1st day of the week is Sunday"); break; case 2: printf("2nd day of the week is Monday"); break; case 3: printf("3rd day of the week is Tuesday"); break; case 4: printf("4th day of the week is Wenesday"); break; case 5: printf("5th day of the week is Thursday"); break; case 6: printf("6th day of the week is Friday"); break; case 7: printf("7th day of the week is Saturday"); break; } }

Output:


enter the no between 1 to 7: 4

4th day of the week is Wednesday



Post a Comment

Previous Post Next Post