Learning C++ ____ Learning variables Switch cases and writing a simple program.

in #tutorial5 years ago

learn-code-e1455713167295.jpg
Image Source Google

Hello People.

Today we are going to learn about various thing like variables , Switch case, and we are going to write a simple program with the help of switch in C++ Programming language.so before writing any program first we need to learn what is variables.

Variables in C++

To understand variable in programming language we can say that variables in c++ or any other is same as variable in Math . Variable can store information that can be used multiple time while writing a script. But the concept of variable is different in C++ as compare to advance languages like JavaScript and Python. In object oriented language like c++ you have to declare the type of variable before passing any value through a variable. The variable in C++ can be integer(int),String(string),Float(float),char . These are the basic datatype used in c++.

C++ is case-Sensitive

The most important thing in c++ language is that this programming language is case-sensitive it means that if you declare a variable in lower case then if you need to use that variable you will need to type the exact case(Lower OR Uper).

Data Type:

As we discuss above that you have to declear a data type of variable before storing any information on that particular variable

TypeKeyword
Booleanbool
Characterchar
Integerint
Floating pointfloat
Double floating pointdouble
Valuelessvoid
Wide characterwchar_t

These variable can store particular type of data and will consume a particular memory size.

The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.

TypeTypical Bit WidthTypical Range
char1byte-127 to 127 or 0 to 255
unsigned char1byte0 to 255
signed char1byte-127 to 127
int4bytes-2147483648 to 2147483647
unsigned int4bytes0 to 4294967295
signed int4bytes-2147483648 to 2147483647
short int2bytes-32768 to 32767
unsigned short int2bytes0 to 65,535
signed short int2bytes-32768 to 32767
long int4bytes-2,147,483,648 to 2,147,483,647
signed long int8bytessame as long int
unsigned long int4bytes0 to 4,294,967,295
long long int8bytes-(2^63) to (2^63)-1
unsigned long long int8bytes0 to 18,446,744,073,709,551,615
float4bytes
double8bytes
long double12bytes
wchar_t2 or 4 bytes1 wide character

Moving to our script

In the pervios post we downloaded the DevC++ compiler so we are going to use that compiler to write our very basic script.

So we are going to writer a script that will ask user to enter number from 1-7 and if the user enter 1 it will display Monday and if user enter 2 it will display Thusday and so on. Without wasting time lets begin.

1.We will open the devc++ compiler

Capture.JPG

2.Then write the basic syntax.

Capture2.JPG

First of all we need to display a message to user to enter a number. So we are going to cout. cout in c++ is used to display some information to user it can be string data or Numeric data

Capture3.JPG
cout<<"Enter number from 1 to 7 to access the name of days

The text in double quotes is a string data. We don't need to assign a variable there or need can do something like this.

Capture5.JPG

So declared a variable with string data type named text and stored some text on it .Now when ever wee need that information we can just write that variable instead of writing the whole sentence but as need do not need that sentence so i will remove that variable and will write the text in double quotes .

Now need to variable on which we can store a the number which user will enter . So

Capture4.JPG

So we declared a variable with name number and this variable will take data from user through cin. cin in C++ is used to read data from user.

Capture6.JPG

Now here we are using Switch case to check either the value enter by the user is match by the case or not .We can use the if else statement as well but as we have easier way to do that we don't need other things

The next thing is break Break is used to break the flow of program if we do not use the break statement the compiler will still execute the script even the particular condition is satisfied.

Capture7.JPG

Ok so we are almost ready we wrote a script. If the user enterd data match with case 1 it will execute the data of case else it will break the statement and will move to next case.

Capture8.JPG
Capture8.JPG

our script is working fine

But what if non of the case match with the entered data what if the user enter 8 which is not defined in the script. In this case we use the default case if no case match the default case will be executed .

Capture.JPG
Capture2.JPG

As you can see that we entered 8 which is not in our case so the default statement is executed

Source Code

       #include<iostream>
    using namespace std;
     int main()
      {
        cout<<"Enter number from 1 to 7 to access the name of days";
            int number;
                    cin>>number;
                switch(number){
    case 1:
        cout<<"monday";
        break;
        case 2:
            cout<<"Thusday";
            break;
            case 3:
                cout<<"wednesday";
                break;
                case 4:
                    cout<<"Thursday";
                    break;
                    case 5:
                        cout<<"Friday";
                        break;
                        
                        case 6:
                            cout<<"saturday";
                            break;
                            case 7:
                                cout<<"sunday";
                                break;
                                default:
                                    cout<<"Incorrect input Please enter number from 1 to 7";
                                    
        
}

}

That's enough for today take care and stay happy and safe. See you on next post.

Sort:  

Congratulations @pakgamer! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made your First Vote

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.19
JST 0.034
BTC 91275.94
ETH 3099.80
USDT 1.00
SBD 2.90