Image Source Google
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.
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++.
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).
As we discuss above that you have to declear a data type of variable before storing any information on that particular variable
| Type | Keyword |
|---|---|
| Boolean | bool |
| Character | char |
| Integer | int |
| Floating point | float |
| Double floating point | double |
| Valueless | void |
| Wide character | wchar_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.
| Type | Typical Bit Width | Typical Range |
|---|---|---|
| char | 1byte | -127 to 127 or 0 to 255 |
| unsigned char | 1byte | 0 to 255 |
| signed char | 1byte | -127 to 127 |
| int | 4bytes | -2147483648 to 2147483647 |
| unsigned int | 4bytes | 0 to 4294967295 |
| signed int | 4bytes | -2147483648 to 2147483647 |
| short int | 2bytes | -32768 to 32767 |
| unsigned short int | 2bytes | 0 to 65,535 |
| signed short int | 2bytes | -32768 to 32767 |
| long int | 4bytes | -2,147,483,648 to 2,147,483,647 |
| signed long int | 8bytes | same as long int |
| unsigned long int | 4bytes | 0 to 4,294,967,295 |
| long long int | 8bytes | -(2^63) to (2^63)-1 |
| unsigned long long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
| float | 4bytes | |
| double | 8bytes | |
| long double | 12bytes | |
| wchar_t | 2 or 4 bytes | 1 wide character |
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.
cout. cout in c++ is used to display some information to user it can be string data or Numeric data
cout<<"Enter number from 1 to 7 to access the name of days
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 .if else statement as well but as we have easier way to do that we don't need other thingsbreak 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.
#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";
}
}