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

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

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. ...

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...