C++ in Hindi
History of C++
- C++ Programming Language को बनाने की शुरुआत 1979 में हुई |
- C Programming में ही कुछ बदल करके 'C with Classes' ये C का concept आया |
- 'C with Classes' के साथ और भी कई concepts को बनाकर Bjarne Stroustrup ने 1983 में AT & T's Bell Labs में C++ Programming का अविष्कार किया गया |
Introduction of C++
- C++ Programming Language ये OOP(Object-Oriented Programming) Language है, इससे पहले भी 'Simula' ये भी OOP Language बनाई गयी थी |
- C++ में OOps concept के साथ कई features है |
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
- C++ Programming Language ये Middle Level Language है |
- C++ Programming को Windows, Linux, Mac OS या आदि Operating Systems पर चलाया जाता है |
Uses of C++
- C++ Programming Language का इस्तेमाल Computer Softwares बनाने के लिए किया जाता है |
- Computer Softwares के साथ-साथ Drivers, Computer Hardwares, servers के लिए भी इस्तेमाल किया जाता है |
Basic Concepts of OOP(Object-Oriented Programming)
OOP ये Method Classes और Objects पर निर्धारित होता है |
OOP ये एक ऐसी concept है, जिसमे Object के data और function का data structure होता है |
C++ ये एक Procedural(C Programming जैसी) और OOP Language भी है |
नीचे OOP के कुछ concepts short में दिए हुए है |
- Class : Class का जो object उससे related उसका behaviour, properties या attributes को define किया जाता है |for eg. अगर कोई Animal है | तो class में उसका behavior, उसके body के parts और उनकी संख्या इनको define किया जा सकता है |
- Object : Object को class पर create किया जाता है | Object के कई नाम हो सकते है, जो उसके data members से related होते है |
- Inheritance : Inheriatance में एक मुख्य class की attributes वो अपने sub-classes को inherit करता है | Inheriatance में मुख्य class को base class या parent class कहते है और उसके अन्दर या उसके subclasses को derived class या child class कहते है |
- Polymorphism : Polymorphism मतलब एक ही form में अनेक form होते है | इसमे same नाम के member function को अलग -अलग parameters होते है |example में एक shape के द्वारा square, Circle, Triangle इन सभी shapes को draw किया जा सकता है |
- Abstraction : Abstraction में कुछ जरुरत के हिसाब से important data को दिखाया जाता है और कुछ data या internal processes को hide किया जाता है |
- Encapsulation : Encapsulation data और class को conbine करके एक class के अन्दर रखा जाता है | ये 'Data Abstraction' भी कहलाया जाता है |
Reserved Keywords
| Reserved Keywords for C++ Programming | |||
|---|---|---|---|
| asm | else | new | this |
| auto | enum | operator | throw |
| bool | explicit | private | true |
| break | export | protected | try |
| case | extern | public | typedef |
| catch | false | register | typeid |
| char | float | reinterpret_cast | typename |
| class | for | return | union |
| const | friend | short | unsigned |
| const_cast | goto | signed | using |
| continue | if | sizeof | virtual |
| default | inline | static | void |
| delete | int | static_cast | volatile |
| do | long | struct | wchar_t |
| double | mutable | switch | while |
| dynamic_cast | namespace | template | |
| C++ Keywords | Description |
|---|---|
| asm | Assembly Language लिखने के लिए इस्तेमाल किया जाता है | |
| auto | Storage class का एक प्रकार है | जो Local variable के लिए भी इस्तेमाल होता है | |
| bool | boolean variable declare करने के लिए इस्तेमाल होता है | for eg. True और False |
| break | Loops या statements को break करने के लिए इस्तेमाल होता है | |
| case | Switch case statement को इस्तेमाल करने के लिए होता है | |
| catch | throw से exceptions handle करने के लिए इस्तेमाल होता है | |
| char | character variable declare करने के लिए इस्तेमाल होता है | |
| class | classes को declare करने के लिए इस्तेमाल होता है | |
| const | Const ये एक स्थिर variable के लिए इस्तेमाल होता है | |
| const_cast | Constant variable को cast किया जाता है | |
| continue | Loop को iterate किया जाता है | |
| default | Switch case statement के लिए इस्तेमाल होता है | |
| delete | Dynamic memory allocation के लिए इस्तेमाल होता है | |
| do | एक loop का प्रकार है | जिसके साथ while loop को इस्तेमाल किया जाता है | |
| double | floating-point data-type है | |
| dynamic_cast | pointer के साथ इस्तेमाल होता है | |
| else | if के साथ statement को इस्तेमाल किया जाता है | |
| enum | Enumeration data type का 'Keyword' है | |
| explicit | Constructor convert करने के लिए इस्तेमाल होता है | |
| export | template की definition को export किया जाता है | |
| extern | Storage class का एक प्रकार है | जिसका scope global होता है | |
| false | Boolean की एक value है | |
| float | Floating-point variable को declare करने के लिए इस्तेमाल होता है | |
| for | Loop का एक प्रकार है | |
| friend | non-member function; private data में access करने के लिए इस्तेमाल होता है | |
| goto | एक statement है , जिसमे label होता है | |
| if | एक statement है | जिससे condition सही है या गलत इसका पता चलता है | |
| inline | function के लिए इस्तेमाल होता है | |
| int | integer variable को declare करने के इस्तेमाल होता है | |
| long | long integer variable को declare करने के इस्तेमाल होता है | |
| mutable | एक storage class का प्रकार है | |
| namespace | same idetifiers(variables, functions, classes) अलग-अलग बताने के लिए इस्तेमाल होता है | |
| new | Dynamic memory allocation के लिए इस्तेमाल होता है | |
| operator | Overloaded operators को declare करने के लिए इस्तेमाल होता है | |
| private | Class के private members को declare करने के लिए इस्तेमाल होता है | |
| protected | Class के protected members को declare करने के लिए इस्तेमाल होता है | |
| public | Class के public members को declare करने के लिए इस्तेमाल होता है | |
| register | एक storage class का प्रकार है | |
| reinterpret_cast | Variable का cast-type change करने के लिए इस्तेमाल होता है | |
| return | function के लिए इस्तेमाल होता है | |
| short | short integer variable को declare करने के इस्तेमाल होता है | |
| signed | एक variable modifier है | |
| sizeof | variable का size return करने के लिए इस्तेमाल होता है | |
| static | एक storage class का प्रकार है | |
| static_cast | एक type_conversion है | |
| struct | structure को define या declare करने के लिए इस्तेमाल होता है | |
| switch | एक statement है | |
| template | Generic Program को लिखने के लिए इस्तेमाल होता है | |
| this | pointer को current object पर लाता है | |
| throw | Exception Handling के लिए इस्तेमाल होता है | |
| true | Boolean की एक value है | |
| try | Exception Handling के लिए इस्तेमाल होता है | |
| typedef | data type को alias_name देने के लिए इस्तेमाल होता है | |
| typeid | Object का वर्णन किया जाता है | |
| typename | Class का alternative है | |
| union | ये kayword अपने members को एक ही memory location पर assign किया जाता है | |
| unsigned | unsigned integer variable को declare करने के लिए इस्तेमाल किया जाता है | |
| using | namespace के साथ इस्तेमाल किया जाता है | |
| virtual | Runtime polymorphism के लिए इस्तेमाल होता है | |
| void | ये कुछ भी return नहीं करता | |
Program का Structure :
Preprocessor #include <iostream.h> Standard Namespace using namespace std; return_type & main function int main() Opening curly brace { printf i/o function cout<<"Hello World!"; return 0; return 0; Closing curly brace } ये Program C++ Programming में सबसे पहला और आसान Program है | ऊपर दिया हुआ Program सात भाग में बटा हुआ है |- Preprocessor
- Standard Namespace
- main function
- {
- cout<<"Hello World!";
- return 0;
- }
for eg. iostream.h में cin और cout आते है |
conio.h में getch function आता है |
और भी कुछ preprocessors है , जो Library functions में दिखाएंगे |
2.using namespace std; : ये एक Standard command library है | Program में input/Output के लिए इसका इस्तेमाल होता है | अगर इसका इस्तेमाल नहीं होता, तो cout और cin को कोई scope नहीं रह जाता | Program को बिना using namespace std; से इस्तेमाल किया जाता है | पर cout<< की जगह std::cout<< को लिखना पड़ता है |
3.int main() : यहाँ पर main function का return_type integer है | main function का return_type void भी लिखा जाता है पर void कोई भी value return नहीं करता | Program की शुरुआत main() function से होती है |
4.{ : हर function के codes या statements को curly brace open होने के बाद लिखा जाता है |
5.cout<<"Hello World!"; : cout को insertion operator(<<) के साथ लिखे हुए statement को output में print किया जाता है | इस statement को दो Double Quotes (" ") के अंदर लिखा जाता है |
6.return 0; : return 0 ये Program को बंद करने की अनुमति देता है | ये '0' main function को return करता है |
7.} : यहाँ पर } इस curly brace से main function को close किया है |C++ Hello Program using namespace
Source Code :
Output :123456789#include <iostream.h> using namespace std; int main(){ cout<<"Hello World!"; return 0; }Hello World!
C++ Hello Program without using namespace
Source Code :
Output :12345678#include <iostream.h> int main(){ std::cout<<"Hello World!"; return 0; }Hello World!
Program को Run कैसे करे ?
- Turbo C / C++ Download करे |
- Download किये हुए Turbo C / C++ को अपने Computer में install करे |
- उस Application को Open करके C Hello का Program लिखे |
- Program लिखने के बाद File पर जाकर save या F2 दबाया तो एक dialog box खुल जाएगा |
- Dialog Box खुलने के बाद उसे कोई भी नाम देकर उसे .cpp का extension दे | for eg. hello.cpp
- Save करने के बाद उसे F9 से Compile करे |
- अगर Program बिना error का हो तो CTRL+F9 से करे |
C++ Programming में दो प्रकार के Data Types होते है |
- Basic Data Types
- Derived Data Types
Data Types in Hindi
Basic Data Types Derived Data Types - int (Integer)
- char (Character)
- float
- double
- void
- bool(boolean)
- wchar_t(Wide Character)
- Arrays
- Pointers
- Structure
- Unions
- Enums (Enumerations)
Basic Data Types
1. int (Integer)- Integer Data Type में variable को declare करने के 'int' keyword का इस्तेमाल करते है |
- Integer Data Type सभी numeric values को store कर सकता है |
- Integer Data Type 2, 4 और 8 bytes के हो सकते है |
- अगर Computer का Processor 16-bit हो तो int का size 2 Bytes होता है |
- अगर Computer का Processor 32-bit हो तो int का size 4 Bytes होता है |
- अगर Computer का Processor 64-bit हो तो int का size 8 Bytes होता है |
Integer Data Types, Storage Size and Range
Data Types Storage Size Range int (16-bit) 2 Bytes -32,768 to 32,767 int (32-bit) 4 Bytes -2,147,483,648 to 2,147,483,647 int (64-bit) 8 Bytes -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807 unsigned int (16-bit) 2 Bytes 0 to 65,535 short int 2 Bytes -32,768 to 32,767 unsigned short int 2 Bytes 0 to 65,535 signed short int 2 Bytes –32,768 to 32,767 long int 4 Bytes –2,147,483,647 to 2,147,483,647 unsigned long int 4 Bytes 0 to 4,294,967,295 signed long int 4 Bytes –2,147,483,648 to 2,147,483,647 sizeof इस keyword से int Data Types के size का पता करे |
Source Code :1234567891011121314151617#include <iostream.h> using namespace std; int main() { cout<<"int(16-bit) storage size: "<<sizeof(int)<<endl; cout<<"int(32-bit) storage size: "<<sizeof(int)<<endl; cout<<"int(64-bit) storage size: "<<sizeof(int)<<endl; cout<<"unsigned int storage size: "<<sizeof(unsigned int)<<endl; cout<<"short int storage size: "<<sizeof(short int)<<endl; cout<<"unsigned short int storage size: "<<sizeof(unsigned short int)<<endl; cout<<"signed short int storage size: "<<sizeof(signed short int)<<endl; cout<<"long int storage size: "<<sizeof(long int)<<endl; cout<<"unsigned long int storage size: "<<sizeof(unsigned long int)<<endl; cout<<"signed long int storage size: "<<sizeof(signed long int)<<endl; return 0; }Outputint(16-bit) storage size: 2 int(32-bit) storage size: 4 int(64-bit) storage size: 8 unsigned int storage size: 4 short int storage size: 2 unsigned short int storage size: 2 signed short int storage size: 2 long int storage size: 4 unsigned long int storage size: 4 signed long int storage size: 4
2. char (Character)- Character Data Type में variable को declare करने के 'char' keyword का इस्तेमाल करते है |
- सिर्फ एक ही character को declare कर सकते है | for eg. 'H'
- अगर multiple character मतलब पूरे एक string को print करना हो तो double quotes("") का इस्तेमाल करते है | for eg. char arr[10] = "Hello";
- Character Data Type 1 byte का होता है |
for eg.1234567891011121314#include <iostream.h> using namespace std; int main() { char str1='H'; // declare varible in single quotes //char str2[10]='Hello'; // Get Warning Error char str3[10]="Hello"; cout<<"Single quoted Print Character : "<<str1<<endl; //cout<<"Single quoted Print String : "<<str2<<endl; //Get Warning Error cout<<"Double quoted Print Character : "<<str3<<endl; return 0; }Note : ऊपरवाला Program Warning Error देगा | User single quotes और double quotes के बिच का फर्क जानने के लिए ऊपरवाला Program दिया है |OutputSingle quoted Print Character : H Double quoted Print Character : Hello
Character Data Types, Storage Size and Range
Data Types Storage Size Range char 1 Byte -128 to 127 unsigned char (16-bit) 1 Byte 0 to 255 signed char 1 Byte --128 to 127 sizeof इस keyword से char Data Types के size का पता करे |
Source Code :1234567891011#include <iostream.h> using namespace std; int main(){ cout<<"char storage size : "<<sizeof(char)<<endl; cout<<"unsigned char storage size : "<<sizeof(unsigned char)<<endl; cout<<"signed char storage size : "<<sizeof(signed char)<<endl; return 0; }Outputchar storage size : 1 unsigned char storage size : 1 signed char storage size : 1
3. float (Floating-point)- Floating-point Data Type में variable को declare करने के 'float' keyword का इस्तेमाल करते है |
- Floating-pont Data Type 4 bytes का होता है | |
for eg.12345678910111213#include <iostream.h> using namespace std; int main() { float a=5, b=3.145; cout<<"Value of Float variable : "<<a<<endl; cout<<"Value of b : "<<b<<endl; return 0; }OutputValue of Float variable : 5 Value of b : 3.145000
Floating-point Data Type, Storage Size and Range
Data Types Storage Size Range Digits of Precision float 4 Bytes 1.2E-38 to 3.4E+38 6 sizeof इस keyword से char Data Types के size का पता करे |
Source Code :123456789#include <iostream.h> using namespace std; int main() { cout<<"Floating-point Storage size : "<<sizeof(float); return 0; }OutputFloating-point Storage size : 4
4. double- Double Data Type में variable को declare करने के 'double' keyword का इस्तेमाल करते है |
- Double Data Type 8 bytes का होता है | |
- Double और Floating-point Data Type में कोई फर्क नहीं है |
for eg.123456789101112#include <iostream.h> using namespace std; int main() { double a=5.45; cout<<"Value of Double variable : "<<a; return 0; }OutputValue of Double variable : 5.45
Double Data Types, Storage Size and Range
Data Types Storage Size Range Digits of Precision double 8 Bytes 2.3E-308 to 1.7E+308 15 long double 10 Bytes 3.4E-4932 to 1.1E+4932 19 sizeof इस keyword से char Data Types के size का पता करे |
Source Code :12345678910#include <iostream.h> using namespace std; int main() { cout<<"Double Storage size : "<<sizeof(double); return 0; }OutputDouble Storage size : 8
5. void- void मतलब null value |
- void में कोई value नहीं होती |
- ये data type function और function के parameter / argument के लिए इस्तेमाल करते है |
- ये कोई value return नहीं करता |
for eg.12345678910111213#include <iostream.h> using namespace std; void hello(void); // function with no return value and parameter main() { hello(); } void hello(void) { cout<<"Hello World"; }OutputHello World
6. bool(Boolean)- Boolean के लिए सिर्फ दो constant values होती है |
- अगर true होता है, तो 1 return किया जाता है और false होता है, तो 0 return किया जाता है |
for eg.1234567891011121314#include <iostream.h> using namespace std; int main(){ bool value = true; cout<<"value : "<<value<<endl; value = false; cout<<"Changed value is "<<value; return 0; }Outputvalue : 1 Changed value is 0
7. wchar_t(Wide character)- wchar_t ये wide characters होते है |
- ये data type 2 bytes या 4 bytes का होता है |
- wchar_t ये Unicode के लिए इस्तेमाल किया जाता है |
for eg.123456789101112#include <iostream.h> using namespace std; int main(){ wchar_t str[] = L"Hello";; wcout << "Wide character value : "<<str<<endl; //wcout is used for wide stream(Unicode) cout << "Size of the wide char : " <<sizeof(wchar_t); //cout is used for standard stream(Unicode) return 0; }OutputWide character value : Hello Size of the wide char : 2
- Storage Classes Variables का scope और lifetime तय करता है |
- Storage Classes Variables को कहाँ पर store करके रखे ये बताता है |
for eg. CPU, Register
C++ Programming में Storage Classes के पांच प्रकार है |
- Automatic Storage Class
- External Storage Class
- Register Storage Class
- Static Storage Class
- Automatic Storage Class
- External Storage Class
- Register Storage Class
- Static Storage Class
1. Automatic Storage Class
- Automatic Storage Class में 'auto' keyword का इस्तेमाल करते है |
- ये Normal Variable की तरह ही होता है |
- ये एक Local Variable है |
- इनकी visibility या scope function के अंदर होता है | बाहर वो destroyed हो जाते है |
- इनकी default value 'garbage' होती है |
Syntax for Automatic Storage Class
auto data_type variable_name = value(optional);
for eg.int a ; // and
auto int a ; // both are same
Source Code :
12345678910111213#include <iostream.h>
using namespace std;
int main(){
auto int a;
auto int b = 5;
cout<<"Value of a : "<<a<<endl;
cout<<"Value of b : "<<b;
return 0;
}
Output :Value of a : 6946708 // garbage value
Value of b : 5
int a ; // and auto int a ; // both are sameSource Code :
12345678910111213#include <iostream.h>
using namespace std;
int main(){
auto int a;
auto int b = 5;
cout<<"Value of a : "<<a<<endl;
cout<<"Value of b : "<<b;
return 0;
}
Output :Value of a : 6946708 // garbage value Value of b : 5
2. External Storage Class
- External Storage Class में 'extern' keyword का इस्तेमाल करते है |
- External Storage Class के variables का scope Global होता है
- Global Variable के कारण इनका इस्तेमाल Program में extern के साथ कहा पर भी और किसी भी function के अंदर होता है |
- इनकी default value '0' होती है |
Syntax for External Storage Class
extern data_type variable_name = value(optional);
for eg.extern int a ;
Source Code :
123456789101112131415161718192021#include <iostream.h>
using namespace std;
int num = 5 ;
void func(); // function declaration
int main(){
extern int num ;
cout<<"Value of num : "<<num<<endl;
func(); // function calling
return 0;
}
void func(){ // function definition
extern int num ;
cout<<"Value of num : "<<num;
}
Output :Value of num : 5
Value of num : 5
extern int a ;Source Code :
123456789101112131415161718192021#include <iostream.h>
using namespace std;
int num = 5 ;
void func(); // function declaration
int main(){
extern int num ;
cout<<"Value of num : "<<num<<endl;
func(); // function calling
return 0;
}
void func(){ // function definition
extern int num ;
cout<<"Value of num : "<<num;
}
Output :Value of num : 5 Value of num : 5
3. Register Storage Class
- Register Storage Class में 'register' keyword का इस्तेमाल करते है |
- Register Storage Class के variables का scope Local होता है
- Local Variable के कारण इनका इस्तेमाल Program में जिस function के अंदर इनको declare या initialize किया है उसी function के अंदर visible रहता है |
- Register Storage Variables Computer के Register पर store होते है |
- Register Storage Class की Memory 'Limited' होती है | अगर Register की Memory ख़त्म हो जाए तो वो CPU Memory पर store होते है |
- Register Variables का कोई address(&) नहीं होता |
- इनकी default value 'garbage' होती है |
Syntax for Register Storage Class
register data_type variable_name = value(optional);
for eg.register int a ;
Source Code :
123456789101112#include <iostream.h>
using namespace std;
int main(){
register int num = 5 ;
cout<<"Value of num : "<<num;
// printf("Value of num : %d\n",&num);
return 0;
}
Output :Value of num : 5
register int a ;Source Code :
123456789101112#include <iostream.h>
using namespace std;
int main(){
register int num = 5 ;
cout<<"Value of num : "<<num;
// printf("Value of num : %d\n",&num);
return 0;
}
Output :Value of num : 5
4. Static Storage Class
- Static Storage Class में 'static' keyword का इस्तेमाल करते है |
- Static Storage Class के Variables का scope Local और Global ये दोनों होता है |
- इनकी default value '0' होती है |
Syntax for Static Storage Class
static data_type variable_name = value(optional);
for eg.static int a ;
static int a ;
Code Description
निचे दिए हुए General File में int num = 1 ; ये value initialize की है और ये variable एक function में मतलब Local Variable लिया है | function1 इस function को for loop से repeat किया है | ये variable सामान्य होने के कारण इसकी value control के बाहर जाने के बाद नष्ट हो जाती है , इसीलिए इस variable की value increase नहीं होती बल्कि ये बार-बार initial होने से ये initial value को ही for loop के द्वारा बार-बार output में print करता है |
निचे दिए हुए Static File में static int num = 1 ; ये value initialize की है | इस program में General File के मुकाबले सिर्फ static keyword का use किया है | Static variables बिलकुल सामान्य variables के उलट प्रक्रिया करता है | Static Local variables की value control के बाहर जाने के बाद destroy नहीं होती | Static Local variables की value एक बार ही initialize होती है | Static Variables अपने control के बाहर जाने के बाद intialize हुई value को नष्ट नहीं करता, इसीलिए Static File में बार-बार initial value print नहीं होती |
निचे दिए हुए General File में int num = 1 ; ये value initialize की है और ये variable एक function में मतलब Local Variable लिया है | function1 इस function को for loop से repeat किया है | ये variable सामान्य होने के कारण इसकी value control के बाहर जाने के बाद नष्ट हो जाती है , इसीलिए इस variable की value increase नहीं होती बल्कि ये बार-बार initial होने से ये initial value को ही for loop के द्वारा बार-बार output में print करता है |
निचे दिए हुए Static File में static int num = 1 ; ये value initialize की है | इस program में General File के मुकाबले सिर्फ static keyword का use किया है | Static variables बिलकुल सामान्य variables के उलट प्रक्रिया करता है | Static Local variables की value control के बाहर जाने के बाद destroy नहीं होती | Static Local variables की value एक बार ही initialize होती है | Static Variables अपने control के बाहर जाने के बाद intialize हुई value को नष्ट नहीं करता, इसीलिए Static File में बार-बार initial value print नहीं होती |
General File :
123456789101112131415161718#include <iostream.h>
using namespace std;
func(){
int num = 1;
cout<<num;
num++;
}
int main()
{
int i;
for(i=0; i<10; i++){
cout<<endl;
func();
}
return 0;
}
Output :
1
1
1
1
1
1
1
1
1
1
123456789101112131415161718#include <iostream.h>
using namespace std;
func(){
int num = 1;
cout<<num;
num++;
}
int main()
{
int i;
for(i=0; i<10; i++){
cout<<endl;
func();
}
return 0;
}
Output :1 1 1 1 1 1 1 1 1 1
Static File :
123456789101112131415161718#include <iostream.h>
using namespace std;
func(){
static int num = 1;
cout<<num;
num++;
}
int main()
{
int i;
for(i=0; i<10; i++){
cout<<endl;
func();
}
return 0;
}
Output :
1
2
3
4
5
6
7
8
9
10
123456789101112131415161718#include <iostream.h>
using namespace std;
func(){
static int num = 1;
cout<<num;
num++;
}
int main()
{
int i;
for(i=0; i<10; i++){
cout<<endl;
func();
}
return 0;
}
Output :1 2 3 4 5 6 7 8 9 10
What is a Variable ?
- ये data types की values अपने अंदर store करके रखता है |
- Variable ये एक memory location का नाम भी होता है |
Rules for Variable
- Variable ये case-sensetive होता है | for eg int a और int A ये अलग-अलग variables है |
- Variable की शुरुआत किसी भी alphabet(a-z, A-Z) या underscore( _ ) से होती है |
- Variables का नाम alphanumeric हो सकता है | For eg. a1 = 5, var1, var2
- Variable ये space को allow नहीं करता |
- Variable name कोई भी C++ Keywords नहीं होता |
हर एक data type का variable होता है |
| data type | Example | Description |
|---|---|---|
| int | int a = 2; | numeric values को declare करने के लिए int data type का इस्तेमाल होता है | |
| char | char ch = 'H'; and char str[]="hello"; | Character को declare करने के लिए char data type का इस्तेमाल करते है | |
| float | float f = 2.3; | floating-point जैसे variables को declare करने के लिए float data type का इस्तेमाल होता है, लेकिन ये single-precision होते है | |
| double | double d = 2.30; | floating-point जैसे variables को declare करने के लिए double data type का इस्तेमाल होता है, लेकिन ये double-precision होते है | |
| bool | bool b = true; or bool b = (!false) | bool के लिए दो values होती है | True और False |
| wchar_t | wchar_t str[] = L"Hello"; | ये एक wide character data type है | |
C Variable का Declaration और initialization
Variable Declaration
- जब Variable declare होता है तब ये variable जिस data type होता है, उसके हिसाब से Memory allocate करता है |
- Variable Declare होने के बाद ये अपने अंदर Garbage Value लेता है |
Syntax for Single Variable Declaration
data_type single_variable_name;
for eg.int a;
Source Code :
1234567891011#include <iostream.h>
using namespace std;
int main(){
int a;
cout<<"Value of a : "<<a;
return 0;
}
Output :Value of a : 4309822
| Variable_name | a | |
| Variable_value | 4309822 | Garbage Value |
| Address | 0x69fefc |
Syntax for Multiple Variable Declaration
data_type multiple_variable_name;
for eg.int a, b, c;
Source Code :
1234567891011121314#include <iostream.h>
using namespace std;
int main(){
int a, b, c;
cout<<"Value of a : "<<a<<endl;
cout<<"Value of b : "<<b<<endl;
cout<<"Value of c : "<<c<<endl;
return 0;
}
Output :Value of a : 4309822 Value of b : 6946708 Value of c : 4309728
| Variable_name | a | b | c | |
| Variable_value | 4309822 | 6946708 | 4309728 | Garbage Value |
| Address | 0x69fefc | 0x69fef8 | 0x69fef4 |
Variable Initialization
- जब Variable initialize होता है तब ये variable जिस data type होता है, उसके हिसाब से Memory allocate करता है |for eg. int for 2bytes(16-bit) | 4bytes(32-bit) | 8bytes(64-bit), char
- Variable intialization में Variable को normal value दी जाती है |
- Variable intialization में एक variable सिर्फ एक ही value लेता है for eg.
int a = 5, 6 ;int a = 5;
Syntax for Single Variable Initialization
data_type single_variable_name = value;
for eg.int a=5;
Source Code :
123456789101112#include <iostream.h>
using namespace std;
int main(){
int a = 5;
cout<<"Value of a : "<<a;
return 0;
}
Output :Value of a : 5
| Variable_name | a |
| Variable_value | 5 |
| Address | 0x69fefc |
Syntax for Multiple Variable Initialization
data_type single_variable_name = value, single_variable_name = value;
for eg.int a=5, b=6;
Source Code :
123456789101112#include <iostream.h>
int main(){
int a = 5, b = 6;
cout<<"Value of a : "<<a<<endl;
cout"Value of b : "<<b;
return 0;
}
Output :Value of a : 5 Value of a : 6
| Variable_name | a | b | |
| Variable_value | 5 | 6 | |
| Address | 0x69fefc | 0x69fef8 |
Variable Redeclaration
Variable को redeclare नहीं किया जा सकता |
Source Code :
123456789101112#include <iostream.h>
using namespace std;
int main(){
int a, b;
int b;
cout << "Value of a : " <<a;
return 0;
}
Output :error : redeclaration of 'int b'
Variable Scopes
- Variable Scope के दो प्रकार है |
- Local Variable
- Global Variable
Local Variable
- Local Variables function के अंदर होते है |
- Local Variables जिस function के अंदर होते है वह पर ही वो visible रहते है |
- Local Variables की default value 'garbage value' होती है |
Source Code :
1234567891011121314#include <iostream.h>
using namespace std;
int main(){
int a = 5, b = 6, c; // Local Variable
cout<<"Value of a : "<<a;
cout<<"Value of b : "<<b;
cout<<"Garbage Value of c : "<<c;
return 0;
}
Output :Value of a : 5 Value of b : 6 Default Value of c : 4309710 // garbage value
Global Variable
- Global Variables function के बाहर होते है |
- Global Variables की visibility पूरे program में होती है |
- Global Variables की default value '0' होती है |
Source Code :
1234567891011121314#include <iostream.h>
using namespace std;
int a = 5, b = 6, c; // Global Variable
int main(){
cout<<"Value of a : "<<a;
cout<<"Value of b : "<<b;
cout<<"Default Value of c : "<<c;
return 0;
}
Output :Value of a : 5 Value of b : 6 Default Value of c : 0
Constants
- Constant की value fixed होती है |
- Constant किसी भी data type का हो सकता है |
- Constant को Literals भी कहते है |
- Constant Pointer भी होता है |
Syntax :
const data_type variable_name = value (optional) ;Constant के प्रकार :
- Integer Constant
- Decimal Constant
- Octal Constant
- Hexadecimal Constant
- Floating-point / Real Constant
- Character Constant
- String Constant
- Preprocessor
With Examples
| Constant Types | with Examples |
|---|---|
| Integer | eg. 2, 10, -2 |
| Decimal (Integer) | eg. 2, 10, -2 |
| Octal (Integer) | eg. 02, 010 |
| Hexadecimal (Integer) | eg. 0x12, 0x1f |
| Floating-point/Real | eg. -2.4, 4.8 |
| Character | eg. 'i', 'j' |
| String | eg. "Hello", "Hi" |
| Preprocessor | eg. #define a 5 |
Integer Constant Types:
- Decimal Integer Constant
- Octal Integer Constant
- Hexadecimal Integer Constant
Integer Constants
- Integer Constant normal Variable की तरह काम करता है |
- Integer Constant positive (+) या negative (-) हो सकता है |
- Integer Constant की Range -32768 से 32767 तक होती है |
1234567891011#include <iostream.h>
using namespace std;
int main(){
const int num = 5; // integer Constant
cout<<"integer constant value : "<<num;
return 0;
}
Outputinteger constant value : 5
Character Constants
- Character Constant normal Variable की तरह काम करता है |
- Character Constant सिर्फ single character लेता है | |
- Escape Sequences के साथ भी character constant इस्तेमाल किया जाता है |
| Escape Sequences | Explaination |
|---|---|
| \' | Single Quotation Mark |
| \" | Double Quotation Mark |
| \\ | Backslash |
| \? | Question Mark |
| \a | Audible Bell |
| \b | Backspace |
| \f | Form Feed |
| \n | New line |
| \r | Carriage Return |
| \h | Horizontal Tab |
| \v | Vertical Tab |
1234567891011121314#include <iostream.h>
using namespace std;
int main(){
const char ch = 'H'; // character constant
const char escape[]= "Hello\tWorld"; // Escape sequence - Horizontal Tab and string constant
cout<<"Character constant : "<<ch<<endl;
cout<<"String constant : "<<escape<<endl;
return 0;
}
OutputCharacter constant : H String constant : Hello World
Floating-point Constant
- Floating-point Constant normal float variable की तरह ही काम करता है |
- Floating-point Constant में Decimal point (.) होता है |
- अगर Floatint-point Constant की value integer type की हो तो वो Decimal point (.) लेता है |
12345678910111213#include <iostream.h>
using namespace std;
int main(){
const float num1 = 5;
const float num2 = 3.525984; // Floating-point constant
cout<<"Floating-point constant : "<<num1<<endl;
cout<<"Floating-point constant : "<<num2;
return 0;
}
OutputFloating-point constant : 5 Floating-point constant : 3.525984
String Constant
- String Constant की value Double Quotation Mark (" ") के अंदर लिखी जाती है |
- String Constant Single और Multiple characters लेता है |
- String Constant Escape Sequences के साथ भी इस्तेमाल करते है |
12345678910111213141516#include <iostream.h>
using namespace std;
int main(){
const char str1 []= "H"; // String Constant with single character
const char str2 []= "Hello World"; // Normal String constant
const char str3 [] = "Hello\nWorld"; // String Constant with Escape Sequence
cout<<"String Constant with single character : "<<str1<<endl;
cout<<"Normal String constant : "<<str2<<endl;
cout<<"String Constant with Escape Sequence : "<<str3<<endl;
return 0;
}
OutputString Constant with single character : H Normal String constant : Hello World String Constant with Escape Sequence : Hello World
Preprocessor Constant
- Preprocessor के साथ भी Constant value रख सकते है |
12345678910111213#include <iostream.h>
#define a 5 // Constant value with preprocessor
#define b 10 // Constant value with preprocessor
using namespace std;
int main(){
cout<<"Value of a : "<<a<<endl;
cout<<"Value of b : "<<b;
return 0;
}
OutputValue of a : 5 Value of b : 10
Types of Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment / Decrement Operators
- Conditional / Ternary Operator
1. Arithmetic Operators
| Operators | Explaination |
|---|---|
| + (Addition) | ये दो Operands को add करता है | |
| - (Subtraction) | ये right operand से left operand को निकाल देता है | |
| *(Multiplication) | ये दो Operands को multiply करता है | |
| / (Division) | ये right operand द्वारा left operand को divide करता है | |
| % (Modulus) | ये right operand द्वारा left operand को divide करके remainder निकालता है | |
123456789101112131415161718192021222324252627#include <iostream.h>
using namespace std;
int main() {
int a,b,c;
cout<<"Enter two numbers ";
cin>>a>>b;
c=a+b;
cout<<"Addition of a and b is "<<c<<endl;
c=a-b;
cout<<"Subtraction of a and b is "<<c<<endl;
c=a*b;
cout<<"Multiplication of a and b is "<<c<<endl;
c=a/b;
cout<<"Division of a and b is "<<c<<endl;
c=a%b;
cout<<"Remainder of a and b is "<<c<<endl;
return 0;
}
OutputEnter two numbers 5 6 Addition of a and b is 11 Subtraction of a and b is -1 Multiplication of a and b is 30 Division of a and b is 0 Remainder of a and b is 5
2. Relational Operators
| Operators | Explaination |
|---|---|
| < (less than) | एक Operand की value दूसरे Operand से कम हो तो ये true return करता है | for eg. num1=5; num2=6; num1 < num2 |
| > (greater than) | एक Operand की value दूसरे Operand से ज्यादा हो तो ये true return करता है | for eg. num1=6; num2=5; num1 > num2 |
| <= (less than or equal to) | एक Operand की value दूसरे Operand से कम हो या बराबर (equal) हो तो ये true return करता है | for eg. num1=5; num2=5; num1 <= num2 |
| (greater than or equal to) | एक Operand की value दूसरे Operand से ज्यादा हो या बराबर (equal) हो तो ये true return करता है | for eg. num1=5; num2=5; num1 >= num2 |
| == (equal to) | दो Operands जब बराबर(equal) होते है, तब ये true return करता है | |
| != (not equal to) | दो Operands जब एक-दूसरे से अलग होते है, तब ये true return करता है | |
1234567891011121314151617181920212223242526272829303132333435363738394041424344#include <iostream.h>
using namespace std;
int main(){
int a=6, b=5;
if(a<b){
cout<<" a is less than b"<<endl;
}
else{
cout<<" a is greater than b"<<endl;
}
if(a<=b){
cout<<" a is less than b"<<endl;
}
else{
cout<<" a is greater than b"<<endl;
}
if(a>b){
cout<<" a is greater than b"<<endl;
}
else{
cout<<" a is less than b"<<endl;
}
if(a>=b){
cout<<" a is greater than b"<<endl;
}
else{
cout<<" a is less than b"<<endl;
}
if(a==b){
cout<<" a is equal to b"<<endl;
}
else{
cout<<" a is not equal to b"<<endl;
}
return 0;
}
Outputa is greater than b a is greater than b a is greater than b a is greater than b a is not equal to b
3. Logical Operators
| Operators | Explaination |
|---|---|
| && (logical &&) | अगर दोनों conditions true हो तो ये true return करेगा | for eg. (5<6) && (6>5) |
| || (logical OR) | अगर दोनों में से एक भी true है , तो ये true return करेगा | for eg. (5<6) || (6>5) |
| ! (logical not) | अगर condition true हो तो ये उसे false कर देता है | for eg. !((5<6) && (6>5)) !((5<6) || (6>5)) |
1234567891011121314151617181920212223242526#include <iostream.h>
using namespace std;
int main(){
if((5<6) && (6>5)){
cout<<"Condition is true."<<endl;
}
else{
cout<<"Condition is false.[n"<<endl;
}
if((5<6) || (6>5)){
cout<<"Condition is true."<<endl;
}
else{
cout<<"Condition is false."<<endl;
}
if(!((5<6) && (5>6))){
cout<<"Condition is true."<<endl;
}
else{
cout<<"Condition is false."<<endl;
}
return 0;
}
OutputCondition is true. Condition is true. Condition is true.
4. Bitwise Operators
| a | b | a & b | a | b | a ^ b |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
| Operation on AND(a&b) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| अगर a = 20, b = 12 हो तो, | ||
| 20 | 0 0 0 1 0 1 0 0 | |
| 12 | 0 0 0 0 1 1 0 0 | |
| 4 | 0 0 0 0 0 1 0 0 | |
| Operation on OR(a|b) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| अगर a = 20, b = 12 हो तो, | ||
| 20 | 0 0 0 1 0 1 0 0 | |
| 12 | 0 0 0 0 1 1 0 0 | |
| 28 | 0 0 0 1 1 1 0 0 | |
| Operation on XOR(a^b) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| अगर a = 20, b = 12 हो तो, | ||
| 20 | 0 0 0 1 0 1 0 0 | |
| 12 | 0 0 0 0 1 1 0 0 | |
| 24 | 0 0 0 1 1 0 0 0 | |
Binary Left Shift( << ) and Right Shift( >> )
- Left Shift(<<) for e.g. a=20; /* 0001 0100 */ a << 2 में numeric value के binary value में हर binary number को 2 binary numbers left side से shift करता है | for e.g.a=20; /* 0001 0100 */ तो इसका 0101 0000 मतलब 80 हो जायेगा |
- Right Shift(>>) for e.g. a=20; /* 0001 0100 */ ये Left shift से बिलकुल उलट है | Right Shift a>> 2 में numeric value के binary value में हर binary number को 2 binary numbers right side से shift करता है | for e.g.a=20; /* 0001 0100 */ तो इसका 0000 0101 मतलब 5 हो जायेगा |
Complement Operator (~)
- ये Operator सारे bit reverse करता है |
- ये Operator 0 को 1 कर देता है और 1 को 0 कर देता है |
| Operation on Complement( ~ ) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| ~12 | 0 0 0 0 1 1 0 0 | |
| 243 | 1 1 1 1 0 0 1 1 | |
यहाँ पर Output -13 आने के बजाय 243 आया ऐसा क्यों ?
2's Complement of 243 -(reverse of 243 in binary + 1)
| Operation on 2's Complement( ~ ) | ||
|---|---|---|
| Decimal Value | Binary Value | 2's Complement |
| 243 | 1111 0011 | -(0000 1100+1) = -(0000 1101) = -13(output) |
1234567891011121314151617181920212223242526272829#include <iostream.h>
using namespace std;
int main() {
int a=20; /* 0001 0100 */
int b=12; /* 0000 1100 */
int c;
c=a&b;
cout<<"value of c is "<<c<<endl; /* 4 = 0000 0100 */
c=a|b;
cout<<"value of c is "<<c<<endl; /* 28 = 0001 1100 */
c=a^b;
cout<<"value of c is "<<c<<endl; /* 24 = 0001 1000 */
c=a<<2;
cout<<"value of c is "<<c<<endl; /* 80 = 0101 0000 */
c=a>>2;
cout<<"value of c is "<<c<<endl; /* 5 = 0000 0101 */
cout<<"value of c is "<<~b<<endl; /* -13 = 1111 0011 */
return 0;
}
Outputvalue of c is 4
value of c is 28
value of c is 24
value of c is 80
value of c is 5
value of c is -13
5. Assignment Operators
Assignment Operators ग्यारह प्रकार के होते है |
- Assignment Operator (=)
- Add Assignment Operator (+=)
- Subtract Assignment Operator (-=)
- Multiply Assignment Operator (*=)
- Divide Assignment Operator (/=)
- Modulus Assignment Operator (%=)
- Bitwise AND Assignment Operator (&=)
- Bitwise OR Assignment Operator (|=)
- Bitwise XOR Assignment Operator (^=)
- Left Shift Assignment Operator (<<=)
- Right Shift Assignment Operator (>>=)
| Operators | Examples |
|---|---|
| = (assignment) | c = a + b |
| += (add assignment) | c += a same as c = c + a |
| -= (subtract assignment) | c -= a same as c = c - a |
| *= (multiply assignment) | c *= a same as c = c * a |
| /= (divide assignment) | c /= a same as c = c / a |
| %= (modulus assignment) | c %= a same as c = c % a |
| &= (AND assignment) | c &= a same as c = c & a |
| |= (OR assignment) | c |= a same as c = c | a |
| ^= (XOR assignment) | c ^= a same as c = c ^ a |
| <<= (Left Shift assignment) | c <<= a same as c = c << a |
| = (Right Shift assignment) | c >>= a same as c = c >> a |
12345678910111213141516171819202122232425262728293031323334353637383940414243#include <iostream.h>
using namespace std;
int main() {
int a=20, b=12;
b = a + b;
cout<<"value of b is "<<b<<endl;
b += a;
cout<<"value of b is "<<b<<endl;
b -= a;
cout<<"value of b is "<<b<<endl;
b *= a;
cout<<"value of b is "<<b<<endl;
b /= a;
cout<<"value of b is "<<b<<endl;
b %= a;
cout<<"value of b is "<<b<<endl;
b &= 2;
cout<<"value of b is "<<b<<endl;
b |= 2;
cout<<"value of b is "<<b<<endl;
b ^= 2;
cout<<"value of b is "<<b<<endl;
b <<= 2;
cout<<"value of b is "<<b<<endl;
b >>= 2;
cout<<"value of b is "<<b<<endl;
return 0;
}
Outputvalue of b is 32 value of b is 52 value of b is 32 value of b is 640 value of b is 32 value of b is 12 value of b is 0 value of b is 2 value of b is 0 value of b is 0 value of b is 0
6. Increment (++) and Decrement (--) with Prefix and Postfix
- Increment Operator (++) ये variable की value 1 से बढ़ा देता है |
- Decrement Operator (--) ये variable की value 1 से घटा देता है |
| Operators | Same as |
|---|---|
| ++a (Increment Prefix) | a = a + 1 |
| --a (Decrement Prefix) | a = a - 1 |
| a++ (Increment Postfix) | |
| a-- (Decrement Postfix) |
123456789101112131415161718#include <iostream.h>
using namespace std;
int main() {
int a=20;
cout<<"Print Value with prefix : "<<++a<<endl; // increase value with increment prefix
cout<<"Value of a : "<<a<<endl;
cout<<"Print Value with prefix : "<<--a<<endl; // decrease value with decrement prefix
cout<<"Value of a : "<<a<<endl;
cout<<"Print Value with postfix : "<<a++<<endl; // increase value with increment postfix
cout<<"Value of a : "<<a<<endl;
cout<<"Print Value with postfix : "<<a--<<endl; // decrease value with decrement postfix
cout<<"Value of a : "<<a<<endl;
return 0;
}
OutputPrint Value with prefix : 21 Value of a : 21 Print Value with prefix : 20 Value of a : 20 Print Value with postfix : 20 Value of a : 21 Print Value with postfix : 21 Value of a : 20
7. Conditional / Ternary Operator (?:)
- Conditional Operator में तीन Expressions होते है |
- Conditional Operator को Ternary Operator भी कहते है |
- Conditional Operator में अगर पहला expression true होता है, तो वो दूसरा expression output में print करता है |
- अगर Conditional Operator में पहला expression false होता है, तो वो तीसरा expression output में print करता है |
Syntax for Conditional / Ternary Operator
expression1 ? expression 2 : expression 3 for eg. Source Code :
12345678910111213#include <iostream.h>
using namespace std;
int main()
{
int a = 100, b ;
b = ( a == 100 ? 2 : 0 ) ;
cout<<"Value of a is "<<a<<endl;
cout<<"Value of b is "<<b;
return 0;
}
Output :Value of a is 100 Value of b is 2
- जब तक किसी condition का satisfaction नहीं होता तब तक Loop का statement repeat होता रहता है |
C Loops के 4 प्रकार है |
- while loop
- do-while loop
- for loop
- nested loop
1.While Loop
- While Loop में variable को initialize करना जरुरी है
- अगर While Loop को repeat करना हो तो, increment/decrement operator का इस्तेमाल किया जाता है
- जबतक While Loop में Condition true होती तब तक repeat होता रहता है |
Syntax for While Loop
variable initialization ; while(condition){ statements; variable increment/decrement; } for eg.while(i<10){
cout<<i<<endl; // statement of while loop
i++; //increment operator
}
Example for While Loop
Source Code :
1234567891011121314#include <iostream.h>
using namespace std;
int main(){
int i=0; // Variable initialization
while(i<10) // condition of while loop
{
cout<<i<<endl; // statement of while loop
i++; //increment operator
}
return 0;
}
Output :0 1 2 3 4 5 6 7 8 9
2.Do-While Loop
- जबतक Do-While Loop में Condition true होती तब तक repeat होता रहता है |
- Do-While की खासियत ये है कि, अगर condition false भी हो तो ये एक statement को output में print करता है |
Syntax for Do-While Loop
do{ statements; }while(condition); for eg.do{
cout<<i<<endl;
i++;
}while(i<10);
Example for Do-While Loop
Source Code :
1234567891011121314#include <iostream.h>
using namespace std;
int main()
{
int i=0; // Variable initialization
do{
cout<<i<<endl;
i++;
}while(i<10);
return 0;
}
Output :0 1 2 3 4 5 6 7 8 9
3.For Loop
- For Loop को सिर्फ Variable Declaration कि जरुरत होती है | ये statement छोड़के सभी काम अपने अंदर ही करता है |
- जबतक While Loop में Condition true होती तब तक repeat होता रहता है |
Syntax for For Loop
for( variable_initialization; condition; increment/decrement){ statements; } for eg.for( i=0; i<10; i++){
cout<<i<<endl; // statement of while loop
}
Example for For Loop
Source Code :
123456789101112#include <iostream.h>
using namespace std;
int main(){
int i;
for( i=0; i<10; i++){
cout<<i<<endl; // statement of while loop
}
return 0;
}
Output :0 1 2 3 4 5 6 7 8 9
4.Nested Loop
- Nested Loop ये loop का कोई प्रकार नहीं है |
- Nested Loop में एक loop में दूसरा loop लिया जाता है |
- Nested Loop While, Do-While, For Loop के होते है |
Example for Nested Loop
Source Code :
123456789101112131415#include <iostream.h>
using namespace std;
int main(){
int i,j;
for(i=1;i<4;i++){
for(j=1;j<4;j++){
cout<<i<<"\t"<<j<<endl;
}
}
return 0;
}
Output :1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
C++ Programming में Statement के सात प्रकार है
- if Statement
- if_else Statement
- else_if Statement
- switch case Statement
- break Statement
- continue Statement
- goto Statement
1. if Statement
- if Statement में अगर Condition true होती है तब Statement Execute होता है |
Syntax for if Statement
if(condition){ statement(s); } for eg.int a=10, b=20;
if( a < b ){
cout<<"a is less than b";
}
Example for if Statement
Source Code :
123456789101112#include <iostream.h>
using namespace std;
int main(){
int a=10, b=20;
if (a < b)
{
cout<<"a is less than b";
}
return 0;
}
Output :a is less than b
2. if_else Statement
- if_else Statement में अगर Condition true हो तो वो if का statement Execute करता है | अगर Condition false होती है तो else का statement execute करता है |
Syntax for if_else Statement
if(condition){ statement(s); }else{ statement(s); } for eg.int a=10, b=20;
if( a == b ){
cout<<"a is equal to b";
}else{
cout<<"a is not equal to b";
}
Example for if_else Statement
Source Code :
123456789101112131415#include <iostream.h>
using namespace std;
int main(){
int a=10, b=20;
if( a == b ){
cout<<"a is equal to b";
}else{
cout<<"a is not equal to b";
}
return 0;
}
Output :a is not equal to b
3. else_if Statement
- else_if Statement में अगर if की Condition true होती है तो if का statement execute होता है | अगर if का condition false होता है तो वो अगले condition पर जाकर check करता है | अगर वो condition true होता है तो वो उसका statement execute करता है | अगर कोई भी condition true नहीं होती तो वो else का statement execute करता है |
Syntax for else_if Statement
if(condition){ statement(s); }else if(condition){ statement(s); }else{ statement(s); } for eg.int a=10, b=20;
if( a < b ){
cout<<"a is less than b";
}else if( a > b ){
cout<<"a is greater than b";
}else{
cout<<"a is equal to b";
}
Example for else_if Statement
Source Code :
12345678910111213141516#include <iostream.h>
using namespace std;
int main(){
int a=10, b=20;
if( a < b ){
cout<<"a is less than b";
}else if( a > b ){
cout<<"a is greater than b";
}else{
cout<<"a is equal to b";
}
return 0;
}
Output :a is less than b
4. switch case Statement
- Switch case statement में expression होता है और उससे related कुछ cases होते है | जो case उस expression या declare किये हुए variable से match होती है तब वो output में print होता है | अगर कोई भी case expression से match नहीं होती तो वो default का statement output में print करेगा | आपको हर statement के बाद break लगाना पड़ता है, इसका मतलब वो उसके पहले का statement ही print करेगा | अगर आप break नहीं लगाते तो वो पहला और दूसरा ये दोनों statement को print करेगा | default case के बाद break नहीं लगाते |
Syntax for switch Statement
switch (expression){ case value1 : statement1 ; break; case value2 : statement2 ; break; default : statement3 ; }Example for switch Statement
Source Code :
123456789101112131415161718192021222324252627282930313233343536373839404142434445#include <iostream.h>
using namespace std;
int main(){
char Day='D';
switch(Day){
case 'A' :
cout<<"Today is Sunday";
break;
case 'B' :
cout<<"Today is Monday";
break;
case 'C' :
cout<<"Today is Tuesday";
break;
case 'D' :
case 'E' :
cout<<"Today is Wednesday";
break;
case 'F' :
cout<<"Today is Thurday";
break;
case 'G' :
cout<<"Today is Friday";
break;
case 'H' :
cout<<"Today is Saturday";
break;
default :
cout<<"Day is Not Found";
}
return 0;
}
Output :Today is Wednesday
5. break Statement
- Break Statement Program के loops और switch case के execution का काम किसी condition पर बंद कर देता है |
Syntax for break Statement
break;Example for break Statement
Source Code :
1234567891011121314151617#include <iostream.h>
using namespace std;
int main()
{
int i=0;
while ( i < 20 ){
cout<<"value of i is "<<i;
i++;
if ( i == 10)
break;
}
return 0;
}
Output :value of i is 0 value of i is 1 value of i is 2 value of i is 3 value of i is 4 value of i is 5 value of i is 6 value of i is 7 value of i is 8 value of i is 9
6. continue Statement
- Continue Statement Program के loops के condition के हिसाब से बीचवाले statements को skip कर देता है और बादवाले statement को execute करता है |
Syntax for continue Statement
continue;Example for continue Statement
Source Code :
123456789101112131415161718#include <iostream.h>
using namespace std;
int main(){
int i;
for(i=0;i<10;i++){
if(i==7){
cout<<"Number %d is skipped."<<i;
continue;
}
cout<<i<<endl;
}
return 0;
}
Output :0 1 2 3 4 5 6 Number 7 is skipped. 8 9
7. goto Statement
Go to ये C++ Programming का statement है | इसमें labels का use किया जाता है | Goto Statement के दो प्रकार होते है |- Forward
- Backward
जब goto statement कुछ statement को छोड़कर उसके अगले statement को execute करता है , उसे Forward goto statement कहते है और किसी पिछले या execute हुए statement को फिरसे execute करने के लिए अपने पिछले label पर जाता है , उसे Backward goto statement कहते है |
Syntax for Forward and Backward goto Statement
Syntax for Forward
goto label ; statement ; ----------- label ;Syntax for Backward
label ; statement ; ----------- goto label ;Example for goto Statement
Source Code :
123456789101112131415161718192021222324252627282930#include <iostream.h>
using namespace std;
int main(){
int num1, num2, num3;
char ch;
yes : cout<<"Enter two values"<<endl;
cin>>num1>>num2;
num3 = num1 + num2 ;
cout<<"Addition of "<<num1<<" and "<<num2<<" is "<<num3;
cout<<"\nDo you want to continue y(yes) or n(No)";
cin>>ch;
if(ch=='y'){
goto yes;
}
else if(ch=='n'){
goto no;
}
else{
cout<<"You entered other key";
}
no : cout<<"Do you want to exit ?, Press Enter";
return 0;
}
Output :Enter two values 4 5 Addition of 4 and 5 is 9 Do you want to continue y(yes) or n(No)y Enter two values 6 4 Addition of 6 and 4 is 10 Do you want to continue y(yes) or n(No)n Do you want to exit ?, Press Enter
