Control Statements in C++

Control Statement 

C++ Program में ऐसे statements, जो अन्य statements या program execution के flow को control या handle करते हैं, इन्हे Control Statements कहा जाता है | 

दूसरे शब्दों में हम कह सकते हैं कि जिन Statements पर program के कुछ अन्य statements निर्भर करते हैं, उन्हें Control Statements कहा जाता है | 

जैसे - 

  • if . . . . . . else 
  • nested if 
  • ladder if 
  • switch . . . . . . case 
  • do . . . . . . .while 
  • while 
  • for 


if . . . . else statement

यह एक Conditional Statement होता है | जब हम C++ program में कोई Condition apply करना चाहते हैं, तब if . . . . else statement का use करते हैं | 

इन Statement का use करने के लिए if के बाद parenthesis () लगाया जाता है तथा parenthesis के अंदर condition लिखी जाती है : if (condition)

यदि parenthesis में लिखी हुई condition true होती है, तो if (condition) के बाद लिखे गए statement को execute किया जाता है तथा यदि condition false हो जाती है, तो program, if के बाद लिखे गए statement को jump कर जाता है तथा आगे else के code block में लिखे हुए statement को execute कर देता है | 

Syntax:

if (Condition)

{

. . .. . . . . . . .

. . . . . . . . . . .

}

else

{

. . .. . . . . . . .

. . . . . . . . . . .

}

if parenthesis के अंदर condition apply करने के लिए Relational Operators की आवश्यकता होती है | C++ programming में 6 Relational Operators होते हैं - 

>     :     Grater Than 

<     :     Less Than 

>=   :    Grater Or Equal 

<=   :    Less Or Equal

==   :    Equal to 

!=    :    Not Equal to


Example : 

* Program to find greater number in two numbers:


Output: 

* Program to identify Even or Odd number:

Output: 


Nested if Statement 

जब C++ program में एक if statement के अंदर कोई अन्य if statement define किया जाता है, ऐसे if statement को nested if statement कहा जाता है | 

Syntax :

if (Condition)
{
        if (Condition)
        . . . . . . . . . . . . . . . . 
        else 
        . . . . . . . . . . . . . . . . 
}

Example:
* Program to find largest number among three given numbers:

Output:


Ladder if Statement

सामान्यतः C++ program में if . . . . else statement का use करने पर if condition के false होते ही मात्र एक ही विकल्प होता है, else के code block को execute करना | किन्तु यदि condition के false होने पर भी हमें कई विकल्प apply करना हो तो ladder if का use करते हैं | इस statement में else के साथ if लगाकर पुनः अगली condition apply कर दी जाती है | 

Syntax:
if (condition)
{
. . . . . . . . . . . .
}
else if (condition)
{
. . . . . . . . . . . .
}
else if (condition)
{
. . . . . . . . . . . .
}
else
{
. . . . . . . . . . . .
}

* Program to enter obtained marks of a student and find total, percent and result:

Output:






टिप्पणियाँ

इस ब्लॉग से लोकप्रिय पोस्ट

Introduction to Programming

Procedure & Flow Chart

File Extensions & Data Types