DEV Community

Mayank Roy
Mayank Roy

Posted on

Day 6: Learning Conditionals – The Building Blocks of Logic

*Day 6 of my C++ journey was all about conditionals, the decision-makers of programming. I explored if, else, else if, and switch statements to handle different scenarios based on conditions. *

Image description

1. If Statement
Challenge:

Write a program that checks if the user wants to order Green Tea. If the user types “Green Tea,” the program should confirm their order.

#include <iostream>
#include <string>
using namespace std;

int main(){

    string order;
    cout << "What would you like to order? ";
    getline(cin, order);

    if(order == "Green Tea"){
        cout << "Your Order For Green Tea is placed!" << endl;
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Going through the code:

  • The stringorder; line declares a variable named order of typestring and does not assign a value to it.

  • The cout << "What would you like to order? ";line prints the string "What would you like to order? " to the console.

  • Thegetline(cin, order); line reads a line of input from the console and assigns it to the order variable.

  • Theif(order == "Green Tea") line starts an if statement that checks if the order variable is equal to the string “Green Tea”.

  • Thecout << "Your Order For Green Tea is placed!" << endl; line prints the string "Your Order For Green Tea is placed!" followed by a newline character to the console.

-return 0; line indicates that the program has finished executing and returns a value of 0. It can return any value, but in this case, we are returning 0 as this is the exit code for a successful program execution.

2. If-Else Statement
Challenge:

Write a program that checks if a tea shop is open. If the current hour (input by the user) is between 8 AM and 6 PM, the shop is open; otherwise, it’s closed.

#include <iostream>

using namespace std;

int main(){

    int hour;

    cout << "Enter the current hour (0-23):";
    cin  >> hour;

    if ( hour >= 8 && hour <= 18){
        cout << "The Shop is open" << endl;
    }
    else{
        cout << "The Shop is closed" << endl;
    }


    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Going through the code:

  • The int hour;line declares a variable named hour of type intand does not assign a value to it.

  • The cout << "Enter the current hour (0-23): "; line prints the string “Enter the current hour (0-23): ” to the console.

  • The cin >> hour; line reads an integer from the console and assigns it to thehour variable.

  • The if(hour >= 8 && hour <= 18) line starts an if statement that checks if the hour variable is greater than or equal to 8 and less than or equal to 18.

  • The cout << "The shop is Open" << endl; line prints the string “Tea shop is OPEN!” followed by a newline character to the console.

  • The else { line starts an else block that is executed if the if statement is false.

  • The cout << "The shop is closed" << endl;line prints the string "The shop is closed"followed by a newline character to the console.

  • return 0;line indicates that the program has finished executing and returns a value of 0. It can return any value, but in this case, we are returning 0 as this is the exit code for a successful program execution.

3. Nested If-Else
Challenge:

A tea shop offers discounts based on the number of tea cups ordered. Write a program that checks the number of cups ordered and applies a discount:* More than 20 cups:

  • 20% discount
  • Between 10 and 20 cups: 10% discount
  • Less than 10 cups: No discount
#include <iostream>

using namespace std;

int main(){

    int cups;
    double pricePerCup = 2.5, totalPrice, discountedPrice ;

    cout << "Enter the number of tea cups you want :" << endl;
    cin >> cups;

    totalPrice = cups * pricePerCup;

    if( cups > 20 ){
        discountedPrice = totalPrice - (totalPrice * 0.20);
        cout << "You have ordered  " << cups << "  cups you are eligible for 20% discount" << endl ;
        cout << "Original Price\t" << totalPrice << endl << "Discounted Price\t" << discountedPrice << endl ;
    }else if( cups >= 10 && cups <= 20){
        discountedPrice = totalPrice - (totalPrice * 0.10);
         cout << "You have ordered\t" << cups << "\tcups you are eligible for 10% discount" << endl ;
        cout << "Original Price\t" << totalPrice << endl << "Discounted Price\t" << discountedPrice << endl ;
    }else{
        cout << "You are not eligible for a discount" << endl;
    }



    return 0;
}
Enter fullscreen mode Exit fullscreen mode

4. Switch Case
Challenge:

Write a program that lets the user select a tea type from a menu. Use a switch statement to display the price based on the selected tea:

  • Green Tea: $2
  • Black Tea: $4
  • Oolong Tea: $10
  • Chamoline Tea : $15
#include <iostream>
#include <string>

using namespace std;

int main(){
    int choice;
    double price;

    cout << "Select your tea \n";
    cout << "1. Green Tea \n";
    cout << "2. Black  Tea \n";
    cout << "3. Oolong Tea \n";
    cout << "4. Chamoline  Tea \n";
    cout << "Enter your choice in number: \n";

    cin >> choice;

    switch(choice){
        case 1 :
            price = 2.0 ;
            cout << "You have selected Green Tea. Price:  $" << price << endl ;
            break;
        case 2 :
            price = 4.0 ;
            cout << "You have selected Black Tea. Price:  $" << price << endl ;
            break;
        case 3 :
            price = 10.0 ;
            cout << "You have selected Oolong Tea. Price:  $" << price << endl ; 
            break;
        case 4 :
            price = 15.0 ;
            cout << "You have selected Chamoline Tea. Price:  $" << price << endl ;
            break;
        default :
            cout << "Invalid Choice!!" << endl ;
            break;
    }


    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)