Conditional Statement:
A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch.if-else Statement:
Syntax:
if (condition 1){
}
else if (condition 2)
{
}
else
{
}
Implementation of If-else statement in C:
Q: Write a program to find the greatest of three numbers.#include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { printf(“The Greatest Number is %d”,c); } return 0; }
Switch Statement:
A series of decisions in which a variable or expression is tested separately. The value of this expression may be of type int or char, but not of type double.SYNTAX:
switch ( controlling expression ) {case 1 :
statements1 ;
break;
case 2 :
statements 2 ;
break;
.
.
.
case n :
statements n ;
break;
default:
statements d ;
}
INTERPRETATION:
An expression of type int and char is evaluated and compared to each of the case labels until a match is found
When a match between the value of the controlling expression and a case label value is found, the statements following the case label are executed until a break statement is found. Then the rest of the switch statement is skipped.
The statements following a case label may be one or more C statements, so it does not need to make multiple statements into a single compound statement using braces.
If no case label value matches the controlling expression, the entire switch statement body is skipped unless it contains a default label. If so, the statements following the default label are executed when no other case label value matches the controlling expression.
Implementation of switch statement in C:
QUESTION:A hotel has a pricing policy as follows:
a. 2 people: $85
b. 3 people: $90
c. 4 people: $95
d. Additional people: $6 per person
If the customer is staying on company business, then there is 20% discount. If the customer age is over 60, then there is a 15% discount. A customer does not gets both discounts. Given the above data, print the cost of the room.
#include<stdio.h> int main() { int no,type,age; float dis,price,charge; printf("Please enter total number of person "); scanf("%d",&no); printf("Please enter your age "); scanf("%d",&age); printf("please enter stay type 1 for business and 2 for normal "); scanf("%d",&type); switch (no) { case 1 : charge=0; break; case 2 : charge=85; break; case 3 : charge=90; break; case 4 : charge=95; break; default : charge=(no-4)*6+95; break; } if (charge!=0) { if (type==1) { dis=charge*0.2; price=charge-dis; printf("discount is %f",dis); printf("\nTotal pay is %f",price); } else if (age>60) { dis=charge*0.15; price=charge-dis; printf("discount is %f",dis); printf("\nTotal pay is %f",price); } else { printf("No discount\n"); printf("Total pay is %f",charge); } } else { printf("No entry"); } return 0; }
ReplyDeleteYou expressed your thoughts in different way and i really enjoyed with your article.
Python Training in Chennai
Python Classes in Chennai
Big data training in chennai
JAVA Training in Chennai
Selenium Training in Chennai
Digital Marketing Course in Chennai
Python Training in Annanagar