Learning C++ ____ Learning variables Switch cases and writing a simple program.
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
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 |
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
2.Then write the basic syntax.
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
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.
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
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.
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.
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.
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 .
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";
}
}
Congratulations @pakgamer! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
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!