C++ in HindiImage result for 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
asmelsenewthis
autoenumoperatorthrow
boolexplicitprivatetrue
breakexportprotectedtry
caseexternpublictypedef
catchfalseregistertypeid
charfloatreinterpret_casttypename
classforreturnunion
constfriendshortunsigned
const_castgotosignedusing
continueifsizeofvirtual
defaultinlinestaticvoid
deleteintstatic_castvolatile
dolongstructwchar_t
doublemutableswitchwhile
dynamic_castnamespacetemplate

C++ KeywordsDescription
asmAssembly Language लिखने के लिए इस्तेमाल किया जाता है |
autoStorage class का एक प्रकार है | जो Local variable के लिए भी इस्तेमाल होता है |
boolboolean variable declare करने के लिए इस्तेमाल होता है | for eg. True और False
breakLoops या statements को break करने के लिए इस्तेमाल होता है |
caseSwitch case statement को इस्तेमाल करने के लिए होता है |
catchthrow से exceptions handle करने के लिए इस्तेमाल होता है |
charcharacter variable declare करने के लिए इस्तेमाल होता है |
classclasses को declare करने के लिए इस्तेमाल होता है |
constConst ये एक स्थिर variable के लिए इस्तेमाल होता है |
const_castConstant variable को cast किया जाता है |
continueLoop को iterate किया जाता है |
defaultSwitch case statement के लिए इस्तेमाल होता है |
deleteDynamic memory allocation के लिए इस्तेमाल होता है |
doएक loop का प्रकार है | जिसके साथ while loop को इस्तेमाल किया जाता है |
doublefloating-point data-type है |
dynamic_castpointer के साथ इस्तेमाल होता है |
elseif के साथ statement को इस्तेमाल किया जाता है |
enumEnumeration data type का 'Keyword' है |
explicitConstructor convert करने के लिए इस्तेमाल होता है |
exporttemplate की definition को export किया जाता है |
externStorage class का एक प्रकार है | जिसका scope global होता है |
falseBoolean की एक value है |
floatFloating-point variable को declare करने के लिए इस्तेमाल होता है |
forLoop का एक प्रकार है |
friendnon-member function; private data में access करने के लिए इस्तेमाल होता है |
gotoएक statement है , जिसमे label होता है |
ifएक statement है | जिससे condition सही है या गलत इसका पता चलता है |
inlinefunction के लिए इस्तेमाल होता है |
intinteger variable को declare करने के इस्तेमाल होता है |
longlong integer variable को declare करने के इस्तेमाल होता है |
mutableएक storage class का प्रकार है |
namespacesame idetifiers(variables, functions, classes) अलग-अलग बताने के लिए इस्तेमाल होता है |
newDynamic memory allocation के लिए इस्तेमाल होता है |
operatorOverloaded operators को declare करने के लिए इस्तेमाल होता है |
privateClass के private members को declare करने के लिए इस्तेमाल होता है |
protectedClass के protected members को declare करने के लिए इस्तेमाल होता है |
publicClass के public members को declare करने के लिए इस्तेमाल होता है |
registerएक storage class का प्रकार है |
reinterpret_castVariable का cast-type change करने के लिए इस्तेमाल होता है |
returnfunction के लिए इस्तेमाल होता है |
shortshort integer variable को declare करने के इस्तेमाल होता है |
signedएक variable modifier है |
sizeofvariable का size return करने के लिए इस्तेमाल होता है |
staticएक storage class का प्रकार है |
static_castएक type_conversion है |
structstructure को define या declare करने के लिए इस्तेमाल होता है |
switchएक statement है |
templateGeneric Program को लिखने के लिए इस्तेमाल होता है |
thispointer को current object पर लाता है |
throwException Handling के लिए इस्तेमाल होता है |
trueBoolean की एक value है |
tryException Handling के लिए इस्तेमाल होता है |
typedefdata type को alias_name देने के लिए इस्तेमाल होता है |
typeidObject का वर्णन किया जाता है |
typenameClass का alternative है |
unionये kayword अपने members को एक ही memory location पर assign किया जाता है |
unsignedunsigned integer variable को declare करने के लिए इस्तेमाल किया जाता है |
usingnamespace के साथ इस्तेमाल किया जाता है |
virtualRuntime polymorphism के लिए इस्तेमाल होता है |
voidये कुछ भी return नहीं करता |
  • Program का Structure :

    Preprocessor#include <iostream.h>
    Standard Namespaceusing namespace std;
    return_type & main functionint main()
    Opening curly brace{
    printf i/o functioncout<<"Hello World!";
    return 0;return 0;
    Closing curly brace}

    ये Program C++ Programming में सबसे पहला और आसान Program है | ऊपर दिया हुआ Program सात भाग में बटा हुआ है |
    1. Preprocessor
    2. Standard Namespace
    3. main function
    4. {
    5. cout<<"Hello World!";
    6. return 0;
    7. }
    1.Preprocessor : Program में सबसे पहले preprocessors/header को लिखा जाता है | ये preprocessors अलग-अलग काम के लिए विभाजित किये हुए है, 
    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 :
    123456789#include <iostream.h>
    using namespace std;
    
    int main(){
    
     cout<<"Hello World!";
    
    return 0;
    }
    
    Output :
    Hello World!
    

    C++ Hello Program without using namespace

    Source Code :
    12345678#include <iostream.h>
    
    int main(){
    
     std::cout<<"Hello World!";
    
    return 0;
    }
    
    Output :
    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 होते है |
      1. Basic Data Types
      2. Derived Data Types

      Data Types in Hindi

      Basic Data TypesDerived Data Types
      1. int (Integer)
      2. char (Character)
      3. float
      4. double
      5. void
      6. bool(boolean)
      7. wchar_t(Wide Character)
      1. Arrays
      2. Pointers
      3. Structure
      4. Unions
      5. 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 TypesStorage SizeRange
      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 Bytes0 to 65,535
      short int2 Bytes-32,768 to 32,767
      unsigned short int2 Bytes0 to 65,535
      signed short int2 Bytes–32,768 to 32,767
      long int4 Bytes–2,147,483,647 to 2,147,483,647
      unsigned long int4 Bytes0 to 4,294,967,295
      signed long int4 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;
      }
      
      Output
      int(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 दिया है |
      Output
      Single quoted Print Character : H
      Double quoted Print Character : Hello
      

      Character Data Types, Storage Size and Range

      Data TypesStorage SizeRange
      char1 Byte-128 to 127
      unsigned char (16-bit)1 Byte0 to 255
      signed char1 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;
      }
      
      Output
      char 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;
      }
      
      
      Output
      Value of Float variable : 5
      Value of b : 3.145000
      

      Floating-point Data Type, Storage Size and Range

      Data TypesStorage SizeRangeDigits of Precision
      float4 Bytes1.2E-38 to 3.4E+386

      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;
      }
      
      Output
      Floating-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;
      }
      
      
      Output
      Value of Double variable : 5.45

      Double Data Types, Storage Size and Range

      Data TypesStorage SizeRangeDigits of Precision
      double8 Bytes2.3E-308 to 1.7E+30815
      long double10 Bytes3.4E-4932 to 1.1E+493219

      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;
      }
      
      
      Output
      Double 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";
      }
      
      
      Output
      Hello 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;
      }
      
      
      Output
      value : 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;
      }
      
      Output
      Wide 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 के पांच प्रकार है |

  1. Automatic Storage Class
  2. External Storage Class
  3. Register Storage Class
  4. 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

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

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

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 ;

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 :

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

What is a Variable ?

  • ये data types की values अपने अंदर store करके रखता है |
  • Variable ये एक memory location का नाम भी होता है |

Rules for Variable

  1. Variable ये case-sensetive होता है | for eg int a और int A ये अलग-अलग variables है |
  2. Variable की शुरुआत किसी भी alphabet(a-z, A-Z) या underscore( _ ) से होती है |
  3. Variables का नाम alphanumeric हो सकता है | For eg. a1 = 5, var1, var2
  4. Variable ये space को allow नहीं करता |
  5. Variable name कोई भी C++ Keywords नहीं होता |

हर एक data type का variable होता है |
data typeExampleDescription
intint a = 2;numeric values को declare करने के लिए int data type का इस्तेमाल होता है |
charchar ch = 'H'; and char str[]="hello";Character को declare करने के लिए char data type का इस्तेमाल करते है |
floatfloat f = 2.3;floating-point जैसे variables को declare करने के लिए float data type का इस्तेमाल होता है, लेकिन ये single-precision होते है |
doubledouble d = 2.30;floating-point जैसे variables को declare करने के लिए double data type का इस्तेमाल होता है, लेकिन ये double-precision होते है |
boolbool b = true; or bool b = (!false)bool के लिए दो values होती है | True और False
wchar_twchar_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 लेता है |
Garbage Value :Garbage Value Variable को Compiler द्वारा दी जाती है | 

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_namea
Variable_value4309822 Garbage Value
Address0x69fefc

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_nameabc
Variable_value430982269467084309728 Garbage Value
Address0x69fefc0x69fef80x69fef4


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_namea
Variable_value5
Address0x69fefc

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_nameab
Variable_value56
Address0x69fefc0x69fef8

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 के दो प्रकार है |
  1. Local Variable
  2. 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 Typeswith Examples
Integereg. 2, 10, -2
Decimal (Integer)eg. 2, 10, -2
Octal (Integer)eg. 02, 010
Hexadecimal (Integer)eg. 0x12, 0x1f
Floating-point/Realeg. -2.4, 4.8
Charactereg. 'i', 'j'
Stringeg. "Hello", "Hi"
Preprocessoreg. #define a 5

Integer Constant Types:

  1. Decimal Integer Constant
  2. Octal Integer Constant
  3. Hexadecimal Integer Constant
Integer Constants
  • Integer Constant normal Variable की तरह काम करता है |
  • Integer Constant positive (+) या negative (-) हो सकता है |
  • Integer Constant की Range -32768 से 32767 तक होती है |
for eg. Source Code :
1234567891011#include <iostream.h>
using namespace std;

int main(){

const int num = 5; // integer Constant

    cout<<"integer constant value : "<<num;

return 0;
}
Output
integer constant value : 5
Character Constants
  • Character Constant normal Variable की तरह काम करता है |
  • Character Constant सिर्फ single character लेता है | |
  • Escape Sequences के साथ भी character constant इस्तेमाल किया जाता है |
Escape SequencesExplaination
\'Single Quotation Mark
\"Double Quotation Mark
\\Backslash
\?Question Mark
\aAudible Bell
\bBackspace
\fForm Feed
\nNew line
\rCarriage Return
\hHorizontal Tab
\vVertical Tab
for eg. Source Code :
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;
}

Output
Character 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 (.) लेता है |
for eg. Source Code :
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;
}
Output
Floating-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 के साथ भी इस्तेमाल करते है |
for eg. Source Code :
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;
}

Output
String Constant with single character : H
Normal String constant : Hello World
String Constant with Escape Sequence : Hello
World
Preprocessor Constant
  • Preprocessor के साथ भी Constant value रख सकते है |
for eg. Source Code :
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;
}
Output
Value of a  : 5
Value of b  : 10
Types of Operators
  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Increment / Decrement Operators
  7. Conditional / Ternary Operator
1. Arithmetic Operators
OperatorsExplaination
+ (Addition)ये दो Operands को add करता है |
- (Subtraction)ये right operand से left operand को निकाल देता है |
*(Multiplication)ये दो Operands को multiply करता है |
/ (Division)ये right operand द्वारा left operand को divide करता है |
% (Modulus)ये right operand द्वारा left operand को divide करके remainder निकालता है |
For eg. Source Code :
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;
}
Output
Enter 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
OperatorsExplaination
< (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 करता है |
For eg. Source Code :
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;
}

Output
a 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
OperatorsExplaination
&& (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))
For eg. Source Code :
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;
}

Output
Condition is true.
Condition is true.
Condition is true.
4. Bitwise Operators
Truth Table for &, |, ^
aba & ba | ba ^ b
00000
01011
11110
10011
Operation on AND(a&b)
Decimal ValueBinary Value
अगर a = 20, b = 12 हो तो,
200 0 0 1   0 1 0 0
120 0 0 0   1 1 0 0
40 0 0 0   0 1 0 0
Operation on OR(a|b)
Decimal ValueBinary Value
अगर a = 20, b = 12 हो तो,
200 0 0 1   0 1 0 0
120 0 0 0   1 1 0 0
280 0 0 1   1 1 0 0
Operation on XOR(a^b)
Decimal ValueBinary Value
अगर a = 20, b = 12 हो तो,
200 0 0 1   0 1 0 0
120 0 0 0   1 1 0 0
240 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 ValueBinary Value
~120 0 0 0   1 1 0 0
2431 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 ValueBinary Value2's Complement
2431111 0011-(0000 1100+1) = -(0000 1101) = -13(output)
For eg. Source Code :
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;
}

Output
value 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 ग्यारह प्रकार के होते है |
  1. Assignment Operator (=)
  2. Add Assignment Operator (+=)
  3. Subtract Assignment Operator (-=)
  4. Multiply Assignment Operator (*=)
  5. Divide Assignment Operator (/=)
  6. Modulus Assignment Operator (%=)
  7. Bitwise AND Assignment Operator (&=)
  8. Bitwise OR Assignment Operator (|=)
  9. Bitwise XOR Assignment Operator (^=)
  10. Left Shift Assignment Operator (<<=)
  11. Right Shift Assignment Operator (>>=)
OperatorsExamples
= (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
For eg. Source Code :
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;
}

Output
value 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 से घटा देता है |
OperatorsSame as
++a (Increment Prefix)a = a + 1
--a (Decrement Prefix)a = a - 1
a++ (Increment Postfix)
a-- (Decrement Postfix)
for eg. Source Code :
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;
}
Output
Print 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 प्रकार है |
  1. while loop
  2. do-while loop
  3. for loop
  4. 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 के सात प्रकार है
  1. if Statement
  2. if_else Statement
  3. else_if Statement
  4. switch case Statement
  5. break Statement
  6. continue Statement
  7. 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