The if-else STATEMENTS in C++

 The if-else STATEMENTS in C++


The if-else statements can control the flow of the code. For example,

if you are 10 and only 10, you can play this game
or else if you are 13 and only 13, you can play gta4
or else if you are 18+, you can play any game
else, you can't play

From this, you can try and first guess, which ages won't be able to play games.

Only ages 10,13 and 18+ can play at least some game. However, 9 and below and 11,12,14-17 can not play any games.

This is how the flow control in a code works, now keep in mind, the syntax is

  • if (condition) {}
  • else if (condition) {}
  • else {}
Also, keep in mind,
  • a==5 checks if a is 5 and returns true or false.
  • a=5 initializes the value 5 to a.
Try to print the output like this



#include<bits/stdc++.h>
using namespace std;
int main()
{
    string username;
    cout<<"Enter your username - ";
    cin>>username;
    
    cout<<endl;
    
    cout<<"Welcome to ABC Online Banking "<<username<<endl<<endl;
    
    cout<<"What would you like to do?"<<endl;
    cout<<"1.Deposit"<<endl;
    cout<<"2.Withdraw"<<endl;
    cout<<"3.Loans"<<endl;
    cout<<"4.Credit Score"<<endl;
    
    int dothis;
    
    cout<<"Enter your option - ";
    cin>>dothis;
    
    //dothis=1 assigns, dothis==1 checks
    if(dothis==1)
    {
        cout<<endl<<"You selected Deposit"<<endl;
    }
    else if(dothis==2)
    {
        cout<<endl<<"You selected Withdraw"<<endl;
    }
    else if(dothis==3)
    {
        cout<<endl<<"You selected Loans"<<endl;
    }
    else
    {
        cout<<endl<<"You selected Credit Score"<<endl;
    }
    //there will be changes eventually
}

Comments