Logo

dev-resources.site

for different kinds of informations.

Day 6: Learning Conditionals – The Building Blocks of Logic

Published at
1/10/2025
Categories
webdev
programming
beginners
cpp
Author
datboimayank
Categories
4 categories in total
webdev
open
programming
open
beginners
open
cpp
open
Author
12 person written this
datboimayank
open
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
cpp Article's
30 articles in total
Favicon
C++ 1st code
Favicon
Windows 上 VSCode 的 C/C++ 延伸模組處理編碼的問題
Favicon
As 10 Linguagens de Programação mais velozes do mundo
Favicon
The 10 fastest programming languages in the world
Favicon
Concurrency in C++: Mitigating Risks
Favicon
C++ or Rust? I'd stick to my good old C++
Favicon
Day 7: Unlocking the Power of Loops in C++
Favicon
Application scenarios of QML and C++ hybrid programming
Favicon
Bag: A header-only, simplistic, C++20, zipping & unzipping library.
Favicon
ИСТОРИЯ .NET
Favicon
How to Build a Node.js Camera Addon and Using It for Image Processing
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
C++20 - s(uper)size
Favicon
5 ways of passing unique pointer to a function in C++
Favicon
Securing C++ iostream: Key Vulnerabilities and Mitigation Strategies
Favicon
Reading Text Files Like a Pro - C++
Favicon
Day 7 : C++ language | Comparison Operators
Favicon
Day 5: C++ language | Arithmetic Operators
Favicon
Day 6: C++ Language | Assignment operators
Favicon
Day 6: Learning Conditionals – The Building Blocks of Logic
Favicon
OpenMP Data-Sharing Clauses: Differences Explained
Favicon
How to correctly rotate an OBB ?
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Day 3: C++ language | Variables | Datatypes | Part-1
Favicon
Bug of the week #2
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Antivirus in C++
Favicon
Let’s review some code #1: C++
Favicon
Switch Case
Favicon
Recording of selected portion of screen in .MP4 format using C++ in RAD studio

Featured ones: