Skip to main content

Conditional Statement

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; 
 
}

Comments

Post a Comment

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Single LinkList in Java

Linked List: Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. LinkedList − A Linked List contains the connection link to the first link called First. Types of Linked List: Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations: Insert:  Inserts at tail,  specific index. Delete: Deletes from the tail.  specific index. ...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step