vb control structures

62
Project Project on on VB Control VB Control Structures Structures Prepared By : Sudhir Prepared By : Sudhir Kataria Kataria PGT Computer Sc. PGT Computer Sc.

Upload: natashadeeps

Post on 19-Nov-2014

920 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: VB Control Structures

ProjectProject

onon

VB Control StructuresVB Control Structures

Prepared By : Sudhir KatariaPrepared By : Sudhir Kataria PGT Computer Sc.PGT Computer Sc.

K V Chamera IIK V Chamera II

Page 2: VB Control Structures

Detailed Lesson PlanDetailed Lesson Plan

Page 3: VB Control Structures

Project SummaryProject Summary This project named “Control Structure” is mainly This project named “Control Structure” is mainly

focused on the working principle of various control focused on the working principle of various control Structures in VB and their implementation in Structures in VB and their implementation in Programming.Programming.

Various Practical Examples have been included in Various Practical Examples have been included in this project relating to real life problems for the better this project relating to real life problems for the better understanding of the concepts.understanding of the concepts.

Programming questions, Output related questions are Programming questions, Output related questions are formed so as to improve the logical & problem formed so as to improve the logical & problem solving ability of the students.solving ability of the students.

Page 4: VB Control Structures

Learning ObjectivesLearning Objectives Working principles of various types of control Working principles of various types of control

structures in VB.structures in VB. Syntax & semantics of various statements in VB.Syntax & semantics of various statements in VB. Implementation of Control structures in Implementation of Control structures in

Programming.Programming. Connecting real-world entities using control Connecting real-world entities using control

structures.structures. Solving real-life problems using control structures.Solving real-life problems using control structures. Enhancement of Logical & Problem solving ability of Enhancement of Logical & Problem solving ability of

the learner.the learner.

Page 5: VB Control Structures

Assessment ProceduresAssessment Procedures Students performance will be judged/assessed by Students performance will be judged/assessed by

inspecting the following factors :inspecting the following factors : Logic in Programming.Logic in Programming. Efficiency & Effectiveness of Programs.Efficiency & Effectiveness of Programs. Usage of various control structures.Usage of various control structures. Correct syntax.Correct syntax. Correct code.Correct code. Following stylistic guidelines while programming i.e. Following stylistic guidelines while programming i.e.

object naming conventions, meaningful object names, object naming conventions, meaningful object names, proper indentation & spacing, prettyprinting style etc.proper indentation & spacing, prettyprinting style etc.

Page 6: VB Control Structures

Implementation ProceduresImplementation Procedures Students will be explained about the working Students will be explained about the working

principle of individual statement with the help principle of individual statement with the help of its proper syntax and example.of its proper syntax and example.

After explaining working principle of every After explaining working principle of every statement, students will be given Practical statement, students will be given Practical Examples, implemented using VB language on Examples, implemented using VB language on Computer.Computer.

Students will be demonstrated the usage and Students will be demonstrated the usage and working of every statement in variety of ways, working of every statement in variety of ways, according to the options available in the according to the options available in the syntax.syntax.

Page 7: VB Control Structures

Implementation ProceduresImplementation Procedures Students will be given practical questions to Students will be given practical questions to

solve and implement using VB language.solve and implement using VB language. After implementing every type of control After implementing every type of control

structure, students will be given a Home structure, students will be given a Home Assignment based on the statements studied.Assignment based on the statements studied.

Students will be given a consolidated Students will be given a consolidated Assignment in the end of the chapter so as to Assignment in the end of the chapter so as to test the logical ability of the children and their test the logical ability of the children and their understanding about the different types of understanding about the different types of control structures and their implementation.control structures and their implementation.

Page 8: VB Control Structures

Resources UsedResources Used I am in teaching profession from last 12 years so this I am in teaching profession from last 12 years so this

complete lesson plan, assessment procedure, complete lesson plan, assessment procedure, programming questions, output related questions etc., programming questions, output related questions etc., are formed with my previous experience and are formed with my previous experience and knowledge. Still I would like to mention few knowledge. Still I would like to mention few resources I have used for the preparation of this resources I have used for the preparation of this Project :Project :

Text book of Informatics Practices by Sumita Arora.Text book of Informatics Practices by Sumita Arora. Previous year CBSE Board question papers.Previous year CBSE Board question papers. Computer.Computer.

Page 9: VB Control Structures

VB Control StructuresVB Control Structures

Created By : Sudhir KatariaCreated By : Sudhir Kataria PGT Computer Sc.PGT Computer Sc.

K V Chamera IIK V Chamera II

Page 10: VB Control Structures

Control FlowControl Flow In a program, statements may be executed In a program, statements may be executed

sequentially, selectively or iteratively. Every sequentially, selectively or iteratively. Every programming language provides constructs to programming language provides constructs to support sequence, selection or iteration. So support sequence, selection or iteration. So there are three types of programming there are three types of programming constructs :constructs :

Sequential ConstructsSequential Constructs Selection ConstructsSelection Constructs Iterative ConstructsIterative Constructs

Page 11: VB Control Structures

Sequential ConstructSequential Construct The sequential construct means the statements The sequential construct means the statements

are being executed sequentially. This are being executed sequentially. This represents the default flow of statements.represents the default flow of statements.

Stament 1

Stament 2

Stament 3

Page 12: VB Control Structures

Selection ConstructSelection Construct The selection construct means the execution of The selection construct means the execution of

statement(s) depending upon the condition-statement(s) depending upon the condition-test. If a condition evaluates to true, a course-test. If a condition evaluates to true, a course-of-action (a set of statements) is followed of-action (a set of statements) is followed otherwise another course-of-action is otherwise another course-of-action is followed. This construct is also called decision followed. This construct is also called decision construct as it helps in decision making.construct as it helps in decision making.

Page 13: VB Control Structures

Selection ConstructSelection Construct

Condition? Statement 1 Statement 2

Statement 2

Statement 1

One course-of-action

Another course-of-action

true

false

Page 14: VB Control Structures

Iterative ConstructsIterative Constructs The iterative or repetitive constructs The iterative or repetitive constructs

means repetition of a set-of-statements means repetition of a set-of-statements depending upon a condition-test. A set-depending upon a condition-test. A set-of-statements are repeated again and of-statements are repeated again and again till the condition or Boolean again till the condition or Boolean Expression evaluates to true. The Expression evaluates to true. The iteration constructs are also called as iteration constructs are also called as looping constructs.looping constructs.

Page 15: VB Control Structures

Iterative ConstructIterative Construct

Condition?

Statement 2

Statement 1 The loop body

false

The exit conditionTrue

Page 16: VB Control Structures

Selection ConstructsSelection Constructs VB provides two types of selection construct :VB provides two types of selection construct : 1) If statement1) If statement 2) Select Case statement2) Select Case statement The If Statement : If statement of VB comes in The If Statement : If statement of VB comes in

various forms & are given below:various forms & are given below: 1) If..Then Statement1) If..Then Statement 2) If..Then..Else Statement2) If..Then..Else Statement 3) If..Then..ElseIf Statement3) If..Then..ElseIf Statement 4) Nested Ifs4) Nested Ifs

Page 17: VB Control Structures

If..Then StatementIf..Then Statement Def. : An If..Then statement tests a particular Def. : An If..Then statement tests a particular

condition; if the condition evaluates to true, a condition; if the condition evaluates to true, a course-of-action is followed otherwise it is course-of-action is followed otherwise it is ignored.ignored.

Syntax : Syntax : If (boolean expression) ThenIf (boolean expression) Then statementsstatementsEnd IfEnd If

Page 18: VB Control Structures

If..Then StatementIf..Then Statement Example 1. : Example 1. : If (Num>0) ThenIf (Num>0) Then Print “It is a positive number”Print “It is a positive number”End ifEnd if

Example 2 :Example 2 :If txtAge.Text>=18 ThenIf txtAge.Text>=18 Then Print “You are eligible to vote”Print “You are eligible to vote”End ifEnd if

Page 19: VB Control Structures

If..Then..Else StatementIf..Then..Else Statement If..Then..Else statement provides an alternate choice If..Then..Else statement provides an alternate choice

to the user i.e. if the condition is true then a set of to the user i.e. if the condition is true then a set of statements are executed otherwise another set of statements are executed otherwise another set of statements are executed.statements are executed.

Syntax : Syntax : If (boolean Expression) ThenIf (boolean Expression) Then VB Statement(s)VB Statement(s)ElseElse VB Statement(s)VB Statement(s)End IfEnd If

Page 20: VB Control Structures

Examples of If..Then..ElseExamples of If..Then..Else Example 1 :Example 1 :If txtAge.Text>=18 ThenIf txtAge.Text>=18 Then Print “You are eligible to vote”Print “You are eligible to vote”ElseElse Print “Sorry, You are not eligible to vote”Print “Sorry, You are not eligible to vote”End IfEnd If

Example 2 :Example 2 :If Num Mod 2=0 Then If Num Mod 2=0 Then Print “It is an Even Number”Print “It is an Even Number”ElseElse Print “It is an Odd Number”Print “It is an Odd Number”End IfEnd If

Page 21: VB Control Structures

If..Then..ElseIf StatementIf..Then..ElseIf Statement If..Then..ElseIf statement is used to test a number of mutually If..Then..ElseIf statement is used to test a number of mutually

exclusive cases and only executes one set of statements for the exclusive cases and only executes one set of statements for the case that is true first.case that is true first.

Syntax : Syntax : If (Boolean Expression) ThenIf (Boolean Expression) Then Statement(s)Statement(s)ElseIf (Boolean Expression 2) ThenElseIf (Boolean Expression 2) Then Statement(s)Statement(s)ElseIf (Boolean Expression 3) ThenElseIf (Boolean Expression 3) Then Statement(s)Statement(s)::[ Else[ Else Statement(s)Statement(s)End IfEnd If

Page 22: VB Control Structures

Example of If..Then..ElseIf Example of If..Then..ElseIf

If (Age<=4) ThenIf (Age<=4) Then Print “Your rate is free.”Print “Your rate is free.”ElseIf (Age<=12) ThenElseIf (Age<=12) Then Print “You qualify for the children’s rate.”Print “You qualify for the children’s rate.”ElseIf (Age<65) ThenElseIf (Age<65) Then Print “You must pay full rate”Print “You must pay full rate”ElseElse Print “You qualify for the seniors’ rate.”Print “You qualify for the seniors’ rate.”End IfEnd If

Page 23: VB Control Structures

Nested IfsNested Ifs A nested If is an if that has another If in its if’s body A nested If is an if that has another If in its if’s body

or in its else’s body. The nested if can have one of the or in its else’s body. The nested if can have one of the following three forms :following three forms :

1. If (expresssion 1) Then 1. If (expresssion 1) Then If (expression 2 ) ThenIf (expression 2 ) Then Statement 1Statement 1 [Else[Else Statement 2Statement 2 End IfEnd If ElseElse body-of-else]body-of-else] End IfEnd If

Page 24: VB Control Structures

Nested IfsNested Ifs

2. If (expression 1) Then2. If (expression 1) Then body-of-ifbody-of-if ElseElse :: If (expression 2) ThenIf (expression 2) Then Statement-1Statement-1 [Else[Else Statement-2]Statement-2] End IfEnd If

Page 25: VB Control Structures

Nested If’sNested If’s 3)3) If (expression 1) ThenIf (expression 1) Then :: If (expression 2) ThenIf (expression 2) Then Statement-1Statement-1 [Else[Else Statement-2]Statement-2] End IfEnd If ElseElse If (expression 3) ThenIf (expression 3) Then Statement-3Statement-3 [Else[Else Statement-4]Statement-4] : : End IfEnd If End IfEnd If

Page 26: VB Control Structures

Example of Nested If’sExample of Nested If’s

If Num>0 ThenIf Num>0 Then Print “It is a positive number”Print “It is a positive number”ElseElse If Num<0 ThenIf Num<0 Then Print “It is a negative number”Print “It is a negative number” ElseElse Print “The number is equal to zero”Print “The number is equal to zero” End IfEnd IfEnd IfEnd If

Page 27: VB Control Structures

Select-Case StatementSelect-Case Statement Select-Case is a multiple branching statement Select-Case is a multiple branching statement

and is used to executed a set of statements and is used to executed a set of statements depending upon the value of the expression. It depending upon the value of the expression. It is better to use Select-Case statement in is better to use Select-Case statement in comparison to If..Then..ElseIf Statement when comparison to If..Then..ElseIf Statement when the number of checks are more. There are 3 the number of checks are more. There are 3 different forms of using Select-Case statements different forms of using Select-Case statements and are given below :and are given below :

Page 28: VB Control Structures

Different forms of Select-CaseDifferent forms of Select-Case

1. Select Case : Simplest Form [Exact match]1. Select Case : Simplest Form [Exact match]Select Case ExpressionSelect Case ExpressionCase ValueCase Value ’ ’one or more visual basic statementsone or more visual basic statementsCase ValueCase Value ’ ’one or more visual basic statementsone or more visual basic statementsCase Else :Case Else : ’ ’one or more visual basic statementsone or more visual basic statementsEnd SelectEnd Select

Page 29: VB Control Structures

Example of Form 1Example of Form 1

Select Case byMonthSelect Case byMonth Case 1,3,5,7,8,10,12Case 1,3,5,7,8,10,12 number_of_days=31number_of_days=31 Case 2Case 2 number_of_days=28number_of_days=28 Case 4,6,9,11Case 4,6,9,11 number_of_days=30number_of_days=30End SelectEnd Select

Page 30: VB Control Structures

Syntax of Form 2Syntax of Form 2

Select Case : Second Form [Relational Test]Select Case : Second Form [Relational Test]Select Case ExpressionSelect Case ExpressionCase is relation :Case is relation : ’ ’one or more visual basic statementsone or more visual basic statementsCase is relation :Case is relation : ’ ’one or more visual basic statementsone or more visual basic statements[Case Else :[Case Else : ’ ’one or more visual basic statementsone or more visual basic statementsEnd SelectEnd Select

Page 31: VB Control Structures

Example of Form 2Example of Form 2Select Case marksSelect Case marks Case Is < 50Case Is < 50 Result = “Fail”Result = “Fail” Case Is < 60Case Is < 60 Result = “Grade B”Result = “Grade B” Case Is < 75Case Is < 75 Result = “Grade A”Result = “Grade A” Case ElseCase Else Result = “Grade A+”Result = “Grade A+”End SelectEnd Select

Page 32: VB Control Structures

Third Form of Select CaseThird Form of Select Case

Select Case : Third Format [Range Check]Select Case : Third Format [Range Check]Select Case ExpressionSelect Case ExpressionCase exp1 To exp2:Case exp1 To exp2: ’ ’one or more visual basic statementsone or more visual basic statementsCase exp1 To exp2:Case exp1 To exp2: ’ ’one or more visual basic statementsone or more visual basic statements[Case Else:[Case Else: ’ ’one or more visual basic statementsone or more visual basic statementsEnd SelectEnd Select

Page 33: VB Control Structures

Example of Form 3 Example of Form 3

Select Case AgeSelect Case Age Case 2 to 4 : Print “PreNursery”Case 2 to 4 : Print “PreNursery” Case 4 to 6 : Print “Kindergarden”Case 4 to 6 : Print “Kindergarden” Case 6 to 10 : Print “Primary”Case 6 to 10 : Print “Primary” Case Else : Print “Others”Case Else : Print “Others”End SelectEnd Select

Page 34: VB Control Structures

Home Assignment 1Home Assignment 1 Write a program in VB to compare three no’s Write a program in VB to compare three no’s

and print the smallest number among these and print the smallest number among these no’s.no’s.

Write a program in VB to check the eligibilty Write a program in VB to check the eligibilty of a person to vote. Display a message “You of a person to vote. Display a message “You are eligible to vote” if the age of the person is are eligible to vote” if the age of the person is greater than or equal to 18 otherwise print greater than or equal to 18 otherwise print “Sorry! You are not eligible to vote”“Sorry! You are not eligible to vote”

Write a program to compare two no’s and then Write a program to compare two no’s and then print the square and cube of the larger number print the square and cube of the larger number among these no’s.among these no’s.

Page 35: VB Control Structures

Home AssignmentHome Assignment Write a program to display the grade obtained Write a program to display the grade obtained

by the child according to the marks obtained by by the child according to the marks obtained by him/her. Criteria for assigning the grades is him/her. Criteria for assigning the grades is given below : If marks are given below : If marks are

>=90 - Grade is A>=90 - Grade is A <90 and >80 – Grade is B<90 and >80 – Grade is B <80 and >=70 – Grade is C<80 and >=70 – Grade is C <70 and >=60 – Grade is D<70 and >=60 – Grade is D <60 – Grade is E<60 – Grade is E

Page 36: VB Control Structures

Iterative Constructs (Looping Iterative Constructs (Looping Structures)Structures)

Loop : A loop is said to be the set of Loop : A loop is said to be the set of instructions which are repeated again and again instructions which are repeated again and again in a program.in a program.

Types of Loops in VB : Types of Loops in VB : 1)1) Sentinel-controlled Loop Structures : repeat Sentinel-controlled Loop Structures : repeat

statements until a special value called sentinel statements until a special value called sentinel value (or the terminating value) is reached.value (or the terminating value) is reached.

2)2) Counter-controlled Loop Structures : repeat the Counter-controlled Loop Structures : repeat the set of statements until the value specified by set of statements until the value specified by the counter variable is reached.the counter variable is reached.

Page 37: VB Control Structures

Looping StructuresLooping Structures VB offers broadly following three types of VB offers broadly following three types of

looping structures :looping structures :1.1. For..NextFor..Next2.2. Do LoopDo Loop

a) Do While..Loopa) Do While..Loopb) Do..Loop Whileb) Do..Loop Whilec) Do Until..Loopc) Do Until..Loopd) Do..Loop Untild) Do..Loop Until

3.3. While..WendWhile..Wend

Page 38: VB Control Structures

For..Next StatementFor..Next Statement This type of statement is used when the user This type of statement is used when the user

knows in advance how many times the loop is knows in advance how many times the loop is going to be executed. going to be executed.

Syntax :Syntax :For <counter Variable>=<start_val> To <end_val> Step For <counter Variable>=<start_val> To <end_val> Step

<increment/Decrement Value><increment/Decrement Value> ‘ ‘ One or more VB StatementsOne or more VB StatementsNext <counter Variable>Next <counter Variable>

Page 39: VB Control Structures

ExamplesExamples Example 1 : Generate natural no’s from 1 to Example 1 : Generate natural no’s from 1 to

100100For I = 1 To 100 For I = 1 To 100 Print IPrint INext INext I Example 2 : Generate first 20 even no’s.Example 2 : Generate first 20 even no’s.For E = 2 to 40 Step 2For E = 2 to 40 Step 2 Print EPrint ENext ENext E

Page 40: VB Control Structures

More ExamplesMore Examples Example 3 : Generate odd no’s from 100 to 30 Example 3 : Generate odd no’s from 100 to 30

in a list box.in a list box.For O = 99 to 31 Step -2For O = 99 to 31 Step -2 ListO.AddItem(O)ListO.AddItem(O)Next ONext O Example 4 : Generate table of any number N.Example 4 : Generate table of any number N.For T = 1 To NFor T = 1 To N Print N; “*”; T; “=”; N*TPrint N; “*”; T; “=”; N*TNext TNext T

Page 41: VB Control Structures

More ExamplesMore Examples Example 5 : Find factorial of a given number Example 5 : Find factorial of a given number

N.N.::Fact=1Fact=1For I= 1 to NFor I= 1 to N Fact = Fact * IFact = Fact * INext INext IPrint “Factorial of ”; N; “=”; FactPrint “Factorial of ”; N; “=”; Fact::

Page 42: VB Control Structures

Home Assignment 2Home Assignment 2 Write Programs for the following problems :Write Programs for the following problems :1)1) To Generate all the natural no’s from 200 to To Generate all the natural no’s from 200 to

500.500.2)2) To Generate the table of 10.To Generate the table of 10.3)3) To Generate all the even no’s from 200 to To Generate all the even no’s from 200 to

400 in reverse order.400 in reverse order.4)4) To Print first 20 multiples of the number To Print first 20 multiples of the number

input by the user. input by the user.

Page 43: VB Control Structures

Do..Loop StructuresDo..Loop Structures Do While..Loop : Do While loop is an entry Do While..Loop : Do While loop is an entry

controlled loop in which the condition is controlled loop in which the condition is placed at the entry point. This statement placed at the entry point. This statement executes the statements specified in the body executes the statements specified in the body of the loop till the condition evaluates to true. of the loop till the condition evaluates to true. The loop may not be executed at all the if the The loop may not be executed at all the if the condition is initially false.condition is initially false.

Syntax : Syntax : Do While <condition or boolean expression> Do While <condition or boolean expression> ‘ ‘ One or more VB StatementsOne or more VB StatementsLoopLoop

Page 44: VB Control Structures

Examples of Do While..LoopExamples of Do While..Loop Example 1 : Never executes loopExample 1 : Never executes loopDim A as ByteDim A as ByteA=10A=10Do While A>10Do While A>10 A=A-1A=A-1LoopLoop Example 2 : Executes loopExample 2 : Executes loopDim P as ByteDim P as ByteP=20P=20Do While P>5Do While P>5 P=P-2P=P-2LoopLoop

Page 45: VB Control Structures

Do..Loop WhileDo..Loop While Do Loop While is an exit controlled loop as Do Loop While is an exit controlled loop as

the condition is placed at exit point. The body the condition is placed at exit point. The body of the loop is going to be executed at least of the loop is going to be executed at least once whether the condition evaluates to true or once whether the condition evaluates to true or false. Loop is executed as long as the result of false. Loop is executed as long as the result of the condition remains true.the condition remains true.

Syntax :Syntax :DoDo One or more VB StatementsOne or more VB StatementsLoop While <condition or Boolean Expression>Loop While <condition or Boolean Expression>

Page 46: VB Control Structures

ExamplesExamples Example 1 :Example 1 :DoDo num = InputBox (“Enter a number”)num = InputBox (“Enter a number”) sum = sum + numsum = sum + numLoop While num < > 0Loop While num < > 0 Here the statements inside the loop will be Here the statements inside the loop will be

executed once no matter what the comparison executed once no matter what the comparison test evaluates to.test evaluates to.

Page 47: VB Control Structures

Do..Until LoopDo..Until Loop Do Until loop is an entry controlled loop in which the Do Until loop is an entry controlled loop in which the

condition is placed at the entry point. This statement condition is placed at the entry point. This statement executes the statements specified in the body of the executes the statements specified in the body of the loop till the condition evaluates to false. The loop loop till the condition evaluates to false. The loop may not be executed at all the if the condition is may not be executed at all the if the condition is initially true.initially true.

Syntax : Syntax : Do Until <condition or boolean expression> Do Until <condition or boolean expression> ‘ ‘ One or more VB StatementsOne or more VB StatementsLoopLoop

Page 48: VB Control Structures

Examples of Do Until..LoopExamples of Do Until..Loop Example 1 : Never executes loopExample 1 : Never executes loopDim A as ByteDim A as ByteA=10A=10Do Until A<10Do Until A<10 A=A-1A=A-1LoopLoop Example 2 : Executes loopExample 2 : Executes loopDim P as ByteDim P as ByteP=20P=20Do Until P<5Do Until P<5 P=P-2P=P-2LoopLoop

Page 49: VB Control Structures

Do..Loop UntilDo..Loop Until Do Loop Until is an exit controlled loop as the Do Loop Until is an exit controlled loop as the

condition is placed at exit point. The body of condition is placed at exit point. The body of the loop is going to be executed at least once the loop is going to be executed at least once whether the condition evaluates to true or whether the condition evaluates to true or false. Loop is executed as long as the result of false. Loop is executed as long as the result of the condition remains false.the condition remains false.

Syntax :Syntax :DoDo One or more VB StatementsOne or more VB StatementsLoop Until <condition or Boolean Expression>Loop Until <condition or Boolean Expression>

Page 50: VB Control Structures

ExamplesExamples Example 1 :Example 1 :DoDo num = InputBox (“Enter a number”)num = InputBox (“Enter a number”) sum = sum + numsum = sum + numLoop Until num = 0Loop Until num = 0 Here the statements inside the loop will be Here the statements inside the loop will be

executed once no matter what the comparison executed once no matter what the comparison test evaluates to.test evaluates to.

Page 51: VB Control Structures

While..WendWhile..Wend While..Wend loop is functionally equivalent to While..Wend loop is functionally equivalent to

the Do While..Loop. It executes a set of VB the Do While..Loop. It executes a set of VB statements till the condition evaluates to true.statements till the condition evaluates to true.

Syntax :Syntax :While <Condition>While <Condition> one or more vb statementsone or more vb statementsWendWend

Page 52: VB Control Structures

ExamplesExamples Example 1 : Generate the sum of first 10 Example 1 : Generate the sum of first 10

natural no’snatural no’sI = 1I = 1While I<=10While I<=10 Sum = Sum + ISum = Sum + I I = I + 1I = I + 1WendWendPrint “Sum of 10 natural no’s = ” ; SumPrint “Sum of 10 natural no’s = ” ; Sum

Page 53: VB Control Structures

Nested LoopsNested Loops A loop within another loop is called as Nested A loop within another loop is called as Nested

Loop.Loop. Example :Example :For I = 1 to 5For I = 1 to 5 For J = 1 To 3For J = 1 To 3 Print JPrint J Next JNext JPrintPrintNext INext I

Inner Loop Outer Loop

Page 54: VB Control Structures

Working of Nested LoopsWorking of Nested Loops In nested loops the inner loop is executed In nested loops the inner loop is executed

completely for one time execution of the outer completely for one time execution of the outer loop. In the Previous example the Inner Loop loop. In the Previous example the Inner Loop will be executed three times for every will be executed three times for every execution of the outer loop.execution of the outer loop.

Nested loops are very useful when there is a Nested loops are very useful when there is a requirement to generate different kind of requirement to generate different kind of patterns as output.patterns as output.

Page 55: VB Control Structures

ExamplesExamples Example : Program to generate the output given below :Example : Program to generate the output given below :112 22 23 3 33 3 34 4 4 4 4 4 4 4 5 5 5 5 55 5 5 5 5 Sol : Sol : For I = 1 To 5For I = 1 To 5 For J = 1 To IFor J = 1 To I Print I;Print I; Next JNext JPrintPrintNext INext I

Page 56: VB Control Structures

Home Assignment 3 – Nested LoopsHome Assignment 3 – Nested Loops Write programs to generate the following patterns :Write programs to generate the following patterns :1)1) 11 2) 1 2 3 4 52) 1 2 3 4 5

1 21 2 1 2 3 4 1 2 3 41 2 31 2 3 1 2 3 1 2 31 2 3 41 2 3 4 1 2 1 21 2 3 4 51 2 3 4 5 1 1

3)3) A A A BA BA B CA B CA B C DA B C D

Page 57: VB Control Structures

Home Assignment 4 – Output Home Assignment 4 – Output Related QuestionsRelated Questions

Specify the output of the following output related Specify the output of the following output related questions, assume that variables are declared :questions, assume that variables are declared :

1)1) sum=0 sum=0 2)2) sum = 0 sum = 0For I = 1 To 4For I = 1 To 4 p=2 : q=4p=2 : q=4 For J = 1 To 3For J = 1 To 3 Do While p<=10Do While p<=10 sum = sum + Jsum = sum + J If p Mod 2 = 0 ThenIf p Mod 2 = 0 Then Next JNext J sum = sum + psum = sum + pPrint “Sum = ”;sumPrint “Sum = ”;sum End IfEnd IfNext INext I p=p+1p=p+1

LoopLoop Print sumPrint sum

Page 58: VB Control Structures

Output related questionsOutput related questions3)3) R=5 : S=3 R=5 : S=3 4)4) M=2 : N=4 M=2 : N=4DoDo Do Until M>12Do Until M>12 Print R, SPrint R, S N = N + MN = N + M R = R + 1R = R + 1 If M Mod 3 = 0 ThenIf M Mod 3 = 0 Then If R = 7 Then If R = 7 Then N = N - MN = N - M Exit DoExit Do ElseElse End IfEnd If N = N + MN = N + M S = S + RS = S + R End If End If Loop While R <=10Loop While R <=10 M=M+1M=M+1

LoopLoop Print M, NPrint M, N

Page 59: VB Control Structures

Home Assignment - 5Home Assignment - 5 Programming Questions : Develop programs for the Programming Questions : Develop programs for the

followings :followings : To generate the mirror image of the number entered To generate the mirror image of the number entered

by the user.by the user. To generate the prime numbers between 100 and 700. To generate the prime numbers between 100 and 700. To Check whether the number entered by the user is a To Check whether the number entered by the user is a

prime number or not.prime number or not. To print first 30 multiples of the number entered by To print first 30 multiples of the number entered by

the user.the user. To generate the factorial of the number input by the To generate the factorial of the number input by the

user.user.

Page 60: VB Control Structures

Home Assignment - 5Home Assignment - 5 To generate the Fibonacci series upto first 15 terms To generate the Fibonacci series upto first 15 terms

i.e. 0 1 1 2 3 5 8…………..i.e. 0 1 1 2 3 5 8………….. To find the sum of all the numbers divisible by 7 To find the sum of all the numbers divisible by 7

from 100 to 200.from 100 to 200. To check whether the year entered by the user is leap To check whether the year entered by the user is leap

year or not.year or not. To find and print the sum of digits of the number To find and print the sum of digits of the number

entered by the user.entered by the user. To find and print the product of digits of the number To find and print the product of digits of the number

accepted from the user.accepted from the user.

Page 61: VB Control Structures

Assessment ToolsAssessment Tools Students will be given Programming problems, Students will be given Programming problems,

output and error finding questions to test output and error finding questions to test whether they understood the various concepts whether they understood the various concepts taught through this presentation.taught through this presentation.

Various parameters are used to check the Various parameters are used to check the understanding of the concept and its understanding of the concept and its implementation in Programming.implementation in Programming.

All the parameters mention in the next slide All the parameters mention in the next slide have their own importance, so all these have their own importance, so all these parameters needs to be checked by the Teacher parameters needs to be checked by the Teacher teaching Programming.teaching Programming.

Page 62: VB Control Structures

Assessment ToolAssessment Tool

ParameterParameter Specify whetherSpecify whether RemarkRemark

LogicLogic Correct/IncorrectCorrect/Incorrect

EffectiveEffective Yes/NoYes/No

EfficientEfficient Yes/NoYes/No

CodingCoding Correct/IncorrectCorrect/Incorrect

SyntaxSyntax Correct/IncorrectCorrect/Incorrect

Following Following Naming Naming ConventionsConventions

Yes/NoYes/No