Unit I-Control Structures

Paper VB.NET

Unit I
Unit II
Unit III
  • Variable , Arrays ,Collection, Subroutines, Functions,
  • control flow statements, Msgbox & Input box
  • Forms- loading, showing and hiding forms, Methods and events
  • Textbox, Label, Button, and other controls
  • इस Unit का PDF डाउनलोड करें
Unit IV
Unit V
  • Database Programming with ADO.Net
  • Creating connection, Data Adapters and Datasets, Data Reader
  • data bind controls , data grid,
  • Object model, connection object, command object, add ,delete, move &update records to dataset
  • इस Unit का PDF डाउनलोड करें

VB.Net Control Structures:-

Control structures (नियंत्रण संरचनाएँ) प्रोग्राम के फ्लो (flow) को कंट्रोल करती हैं। ये निर्णय लेने (decision-making), लूपिंग (looping), और कोड को व्यवस्थित करने में मदद करती हैं। किसी भी ‘कम्‍प्‍यूटर  प्रोग्राम’  में प्रोग्राम तीन तरह से रन हो सकते है.

1- सीक्‍वेंस  से:-  सिंक्‍वेंसियली इसमें कोड स्‍टेटमेंट, एक के बाद एक क्रम से रन होते हैं।                                                      

 2- Decision Making Structure ( Selection Construct ) सलेक्‍टिवली:-

 इस प्रक्रिया में  स्‍टेटमेंट का क्रियान्‍वित होना किसी कंडीशन पर निर्भर  होता है। यदि कंडीशन सत्‍य होती है तो कोड एक दिशा में रन होता है। यदि यह कंडीशन पूरी नहीं होती तो दूसरा कोड रन हो जाता है। कंडीशन यह तय करता है कि कौन सा स्‍टेटमेंट रन होगा।

विजुअल बेसिक में दो तरह के सलेक्‍शन कांर्स्‍टक्‍ट होते है।

1- If Statement 2- Select Case statement

3- Iteratively (Looping structure ):-

 इसमें किसी एक  एक्‍शन की पुनराव़त्ति  होती है। इस प्रकार के कंर्टोल में  एक ही सटेटमेंट का सेट बार-बार रन होता है,  जब तक दी गयी कंडीशन सत्‍य होती हो या फाल्‍स होती है। सटेटमेंट का सेट जो बार-बार रिपीट होता है उसे ‘बाडी आफ लूप’ कहा जाता है। लूप के प्रयोग से  किसी एक प्रासेस को तब तक रीपीट किया जा सकता है जब तक दी गयी कंडीशन न पूरी हो जाय।

सभी प्रोग्रामिंग लैंग्‍वेज में उपरोक्‍त सभी   कंर्टोल स्‍ट्रक्‍चर पाये जाते है।

Decision Structure (Selection Construct) 

1: If-Then Statement:- 

यह स्‍टेटमेंट एक कंडीशन चेक करता है, यदि कंडीशन सत्‍य है तो कोड के  अंदर के स्‍टेटमेंट रन हो जाते  है।  अगर कंडीशन फाल्‍स है तो प्रोग्राम का कंर्टोल कोड के बिना रन हुये अगले स्‍टेटमेंट में चला जाता है ।

Ex1  –

If studentGrade >= 60 Then

    Console.WriteLine(“Passed”)

End If

Ex2-

If (IntAge <= 12) Then

lblRateComment.Caption = “You qualify for the   children age”

End If

(The parenthesis is not required in above statement. It only helps to separate condition from other code)

 2: If…Then…Else Statement:-

 इस स्‍टेटमेंट में कंडीशन फाल्‍स होने पर, दूसरे  स्‍टेटमेंट  का विकल्‍प रन करने के  लिये ‘Else’  के द्वारा विकल्प दिया जाता है।

” If..Then” सलेक्‍शन स्‍ट्रक्‍चर कंडीशन के सत्‍य होने पर ही रन होता है। असत्‍य होने पर बिना कोड  रन हुये आगे बढ जाता है। लेकिन “If..Then..Else”, selection structure के द्वारा प्रोग्रामर यह निश्चित कर सकता है कि कंडीशन सत्‍य होने  पर क्‍या करना है, और कंडीशन असत्‍य होने पर क्‍या करना है । 

Ex-

If studentGrade >= 60 Then

   Console.WriteLine(“Passed”)

Else

   Console.WriteLine(“Failed”)

End If

……………………………………………………………………………………………

Ex-

If (IntAge <= 12) Then

     lblRateComment.Caption = “You qualify for the childrens rate “

Else

     lblRateComment.Caption = “You must pay full rate “

End If

………………………………………………………………………………………………………….

3: If… Then … ElseIf   Statement:-

इस कंडीशनल कंस्ट्रक्ट में अगर पहली लाइन सत्‍य है तो पहले स्‍टेटमेंट का “सेट” रन होगा और कंट्रोल इंड स्‍टेटमेंट पर चला जायेगा।

अगर पहला स्‍टेटमेंट फाल्‍स है तो दूसरा कंडीशन देखा जायगा अगर सत्‍य निकला तो उसके बाद के स्‍टेटमेंट रन होगें और कंट्रोल ‘इंड इफ’ स्‍टेटमेंट पर चला जायेगा। यह प्रक्रिया तब तक चलती रहेगी जब तक ‘इल्‍स’  या  ‘इंड इफ’ स्‍टेटमेंट मिलता रहेगा।  

            If (intAge <= 4) Then

                        lblRateCommment.Caption = “ you are kid “

            ElseIf(IntAge <=12) Then

                        lblRateCommment.Caption = “ you are qualify to be child “

            ElseIf(IntAge <65) Then

                        lblRateCommment.Caption = “ you are adi;t “

            Else

                        lblRateCommment.Caption = “ you qualify for senior citizen   “

            End If

……………………………………………………………

4: Select  Case Multiple-Selection Structure:-

Select Case एक conditional statement है जो multiple conditions को check करने के लिए इस्तेमाल किया जाता है। यह If-ElseIf ladder का एक बेहतर विकल्प है, जिससे कोड को clean और readable बनाया जा सकता है।

Occasionally, an algorithm contains a series of decisions in which the algorithm tests a variable or expression separately for each value that the variable or expression might assume. The algorithm then takes different actions based on those values. Visual Basic provides the SelectCasemultiple-selection structure to handle such decision making.

Ex.  

Principal = InputBox(“Enter principal amount”)

Time = inputBox(“Enter time in years”)

Dim Rate As Single, Sint As Single

Select Case Principal

            Case Is <10000

                        Rate = 0.05

            Case 10000 To 19999.99

                        Rate = 0.065

            Case 20000 To 49999.99

                        Rate =0.085

            Case Is>= 50000

                        Rate = 0.10

End Select

            Sint = Principal * Rate * Time

MsgBox “Interest Amount is” & Sint

लूपिंग र्स्‍टक्‍चर:-

लूपिंग स्ट्रक्चर प्रोग्रामिंग में एक कंट्रोल स्ट्रक्चर है जो कोड के एक ब्लॉक को बार-बार (repeatedly) एक्सीक्यूट करने की सुविधा प्रदान करता है. जब तक कि कोई निर्धारित शर्त (condition) पूरी नहीं हो जाती।

विजुअल बेसिक में 3 तरह के लूपिंग र्स्‍टक्‍चर पाये जाते है:-

1- For ……. Next

2- Do loop:-

a-  Do While … Loop b-  Do …. Loop While c-  Do Until ….. Loop d- Do … Loop Until

3-  While …Wend

1- For…. Next Repetition Structure:-

इस लूप में कंडीशन चेक की जाती है अगर दी गयी कंडीशन सत्‍य है तो लूप के अंदर के स्‍टेटमेंट रन हो जाते है जैसे ही कंडीशन फाल्‍स होती है लूप रूक जाता

Syntex-

For counter = start To end [Step increment]
‘ कोड ब्लॉक
Next

Ex-

For i = 1 To 5
Console.WriteLine(i)
Next

2-While Repetition Structure:-

 इन  लूप के प्रयोग से  प्रोग्राम में किसी कोड को पुनराव्रत्ति करने की छमता दी जा सकती है। किसी एक स्‍टेटमेंट या स्‍टेटमेंट के समूह को तब तक रीपीट किया जा सकता है जब तक दी गयी कंडीशन न पूरी हो जाय। इनके प्रयोग से कोड की लम्‍बाइ घट जाती है।  

1- While लूप:- 

While लूप में सर्वप्रथम कंडीशन चेक किया जाता है. अगर कंडीशन सत्‍य है तो ही लूप के अंदर के स्‍टेटमेंट रन होंगे फाल्‍स होने पर कंर्टोल लूप के बाहर चला जायेगा. जब तक कंडीशन सही (True) है, तब तक कोड चलाता है।

Dim product As Integer = 2

While product <= 1000

     product = product * 2

End While

 [Note: While structure’s में शर्त शुरू में ही असत्‍य हो जाये तो लूप के बाडी के स्‍टेटमेंट कभी इक्‍सीक्‍यूट नहीं होंगे।]

2-Do While….Loop Repetition Structure

यह लूप भी While  लूप की तरह ही काम करता है. लेकिन र्शत असत्‍य होने पर भी, लूप की बाडी के स्‍टेटमेंट, कम से कम  एक बार रन हो जाते हैं। Do While → जब तक कंडीशन True है, तब तक चलाता है।

Dim product As Integer = 2

Do While product <= 1000

      product = product * 2

Loop

3- Do Until /Loop Repetition Structure

यह लूप तब तक  रन होगा जब तक कंडीशन फाल्‍स होगी। जबकि While और  DoWhile/ Loop में लूप तब रन होता है जब कंडीशन सत्‍य होती है।  Do Until → जब तक कंडीशन False है, तब तक चलाता है।

Ex . –

Dim product As Integer = 2

Do Until product >= 1000

      product = product * 2

Loop

4-Do/Loop While Repetition Structure

Do/LoopWhile structure में कंडीशन लूप की बाडी के बाद चेक की जाती है । कंडीशन चेक करने के पहले लूप एक बार रन हो जाता है। इस कारण इस लूप से कम से कम एक बाद लूप रन होगा।   जब कंडीशन फाल्‍स हो जायेगी तब लूप का कंट्रोल while  स्‍टेटमेंट के बाद चला जायेगा।

Dim product As Integer = 1

Do

      product = product * 2

Loop While produce <= 1000

5-Do/LoopUntil Repetition Structure

इस लूप में कंडीशन लूप के आखिर में चेक की जाती है। यह लूप तब तक रन होता है जब तक कंडीशन असत्‍य होती है । जैसे ही कंडीशन सत्‍य हो जाती है लूप बंद हो जाता है।  कंडीशन चेक होने के पहले यह लूप कम से कम एक बार रन हो जाता है ।   

while लूप तब रन होता है जब कंडीशन सत्‍य हो जबकी वहीं untill  लूप तब रन होता है जब कंडीशन फाल्‍स हो जाय ।

Dim product As Integer = 1

Do

      product = product * 2

Loop Until product >= 1000

Using the Exit Keyword in a Repetition Structure:-

The ExitDo, ExitWhile and ExitFor statements alter the flow of control by causing immediate exit from a repetition structure. The ExitDo statement can be executed in a Do While/Loop, Do/Loop While, Do Until/Loop or Do/Loop Until structure and causes the program to exit immediately from that repetition structure. Similarly, the Exit For and Exit While statements cause immediate exit from For/Next and While loops, respectively. Execution continues with the first statement that follows the repetition structure. Figure 3.9 demonstrates the ExitFor, ExitDo and ExitWhile statements in various repetition structures.

Operators

·         Arithmetic Operators

·         Comparison Operators

·         Logical/Bitwise Operators

·         Bit Shift Operators

·         Assignment Operators

·         Miscellaneous Operators

Arithmetic Operators:-

^             Raises one operand to the power of another

+             Adds two operands

–              Subtracts second operand from the first

*             Multiplies both operands

/              Divides one operand by another and returns a floating point result

              Divides one operand by another and returns an integer result

MOD   Modulus Operator and remainder of after an integer division

Comparison Operators:-

= Checks if the values of two operands are equal or not; if yes, then condition becomes true.
<> Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true.
> Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true.
< Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true.

Logical/Bitwise Operators

AndIt is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions.
  
OrIt is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions.
NotIt is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.
IsFalseIt determines whether an expression is False.
IsTrueIt determines whether an expression is True.

 Assignment Operators:- 

Visual Basic .NET निम्‍न assignment operators प्रयोग किये जाते हैं:-

+= operator अपने राइट साइड के आपरेंड को लेफ्ट साइड के आपरेंड में जोड़ कर लेफ्ट साइड में एसाइन कर देता है।

 =, राइट साइड की वैल्‍यू को लेफ्ट साइड में एसाइन करता है

+=, राइट साइड की वैल्‍यू को लेफ्ट की वैल्‍यू  में जोड़ कर लेफ्ट में जोड़ कर लेफ्ट को एसाइन करता है।

-=, राइट साइड की वैल्‍यू को लेफ्ट  में घटाकर लेफ्ट को एसाइन करता है।

 *=, राइट साइड की वैल्‍यू को लेफ्ट  में गुणाकरके  लेफ्ट को एसाइन करता है।

 /=, लेफ्ट  साइड की वैल्‍यू को राइट से भागदेकर लेफ्ट को एसाइन करता है। फ्लोटिंग प्‍वांइट डिवीजन

 =, लेफ्ट  साइड की वैल्‍यू को राइट से भागदेकर लेफ्ट को एसाइन करता है। इंटीजर  डिवीजन

 ^= ,  लेफ्ट  साइड के आपरेंड को राइट आपरेंड का घात  देकर  लेफ्ट को एसाइन करता है।  

&=  राइट की स्ट्रिंग को लेफ्ट से जोडृकर नइ  स्ट्रिंग को लेफ्ट में एसाइन कर देता है।

Str1 &= Str2 is same as Str1 = Str1 & Str2

एसाइनमेंट आपरेटर एक स्‍टेटमेंट में केवल एक बार प्रयोग किये जा सकते हैं ।

error: Content is protected !!