Control Structures: Part 2

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #5 Control Statements: Part 2.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
Chapter 5- Control Structures: Part 2
Problem Solving and Control Statements: Part 2
Control Statements: Part 2
Chapter 4 - Program Control
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 5 – Control Structures: Part 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 5- part 2 Control Statements: Loops 2
Control Structures: Part 1
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
Chapter 5 Control Statements: Loops 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 6 Control Statements: Part 2
Problem Solving and Control Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 - Program Control
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

Control Structures: Part 2 1

Outline Introduction Essentials of Counter-Controlled Repetition For/Next Repetition Structure Examples Using the For/Next Structure Select Case Multiple-Selection Structure Do/Loop While Repetition Structure Do/Loop Until Repetition Structure Using the Exit Keyword in a Repetition Structure Logical Operators Structured Programming Summary

For/Next Repetition Structure The For/Next repetition structure handles the details of counter-controlled repetition. To illustrate the power of For/Next, we now rewrite the previous program. The result is displayed in following figure:

ForCounter.vb Program Output 1 ' Fig. 5.2: ForCounter.vb 2 ' Using the For/Next structure to demonstrate counter-controlled 3 ' repetition. 4 5 Module modForCounter 6 7 Sub Main() 8 Dim counter As Integer 9 10 ' initialization, repetition condition and 11 ' incrementing are included in For structure 12 For counter = 2 To 10 Step 2 13 Console.Write(counter & " ") 14 Next 15 16 End Sub ' Main 17 18 End Module ' modForCounter ForCounter.vb Program Output Control variable initialized to 2 Step increments counter by 2 each iteration Next marks end of loop 2 4 6 8 10

For/Next Repetition Structure For/Next counter-controlled repetition Structure header initializes control variable, specifies final value and increment For keyword begins structure Followed by control variable initialization To keyword specifies final value Step keyword specifies increment Optional, Increment defaults to 1 if omitted May be positive or negative Next keyword marks end of structure Executes until control variable greater (or less) than final value

For/Next Repetition Structure Final value of control variable Initial value of control variable For keyword Increment of control variable For counter = 2 To 10 Step 2 Control variable name To keyword Step keyword Fig. 5.3 Components of a typical For/Next header.

Examples Using the For/Next Structure Vary the control variable from 1 to 100 in increments of 1 For i = 1 To 100 For i = 1 To 100 Step 1 Vary the control variable from 100 to 1 in increments of –1 For i = 100 To 1 Step –1 Vary the control variable from 7 to 77 in increments of 7 For i = 7 To 77 Step 7 Vary the control variable from 20 to 2 in increments of –2 For i = 20 To 2 Step -2 Vary the control variable over the sequence of the following values: 2, 5, 8, 11,14, 17, 20. For i = 2 To 20 Step 3 Vary the control variable over the sequence of the following values: 99, 88, 77,66, 55, 44, 33, 22, 11, 0. For i = 99 To 0 Step -11

Examples Using the For/Next Structure The next example computes compound interest using the For/Next structure. Consider the following problem statement: A person invests $1000.00 in a savings account that yields 5% interest. Assuming that all interest is left on deposit, calculate and print the amount of money in the account at the end of each year over a period of 10 years. To determine these amounts, use the following formula: a = p (1 + r) n where p is the original amount invested (i.e., the principal) r is the annual interest rate (e.g., .05 stands for 5%) n is the number of years a is the amount on deposit at the end of the nth year.

Select Case Multiple-Selection Structure Tests expression separately for each value expression may assume Select Case keywords begin structure Followed by controlling expression Compared sequentially with each case Code in case executes if match is found Program control proceeds to first statement after structure Case keyword Specifies each value to test for Followed by code to execute if test is true Case Else Optional Executes if no match is found Must be last case in sequence End Select is required and marks end of structure

Select Case Multiple-Selection Structure The following program is using a Select Case to count the number of different letter grades on an exam. Assume the exam is graded as follows: 90 and above is an A, 80–89 is a B, 70–79 is a C, 60–69 is a D and 0–59 is an F. This instructor gives a minimum grade of 10 for students who were present for the exam. Students not present for the exam receive a 0.

Optional Case Else executes if no match occurs with previous Cases Either 0 or any value in the range 10 to 59, inclusive matches this Case. Optional Case Else executes if no match occurs with previous Cases Required End Select marks end of structure

Program Output

Select Case Multiple-Selection Structure Case statements also can use relational operators to determine whether the controlling expression satisfies a condition. For example: Case Is < 0 uses keyword Is along with the relational operator, <, to test for values less than 0. Best control to use with is the ComboBox.

Select Case Multiple-Selection Structure Case a Case b Case z . . . Case Else action(s) false Case a action(s) Case b action(s) Case z action(s) true Fig. 5.11 Flowcharting the Select Case multiple-selection structure.

Do/Loop While Repetition Structure Similar to While and Do/While Loop-continuation condition tested after body executes Loop body always executed at least once Begins with keyword Do Ends with keywords Loop While followed by condition

Do/Loop While Repetition Structure

Do/Loop While Repetition Structure action(s) condition true false Fig. 5.13 Flowcharting the Do/Loop While repetition structure.

Do/Loop Until Repetition Structure Similar to Do Until/Loop structure Loop-continuation condition tested after body executes Loop body always executed at least once

Print the numbers from 1–5. 1 ' Fig. 5.14: LoopUntil.vb 2 ' Using Do/Loop Until repetition structure 3 4 Module modLoopUntil 5 6 Sub Main() 7 8 Dim counter As Integer = 1 9 10 ' print values 1 to 5 11 Do 12 Console.Write(counter & " ") 13 counter += 1 14 Loop Until counter > 5 15 16 End Sub ' Main 17 18 End Module ' modLoopUntil Condition tested after body executes 1 2 3 4 5

Do/Loop Until Repetition Structure action(s) condition false true Fig. 5.15 Flowcharting the Do/Loop Until repetition structure.

Using the Exit Keyword in a Repetition Structure Exit Statements: Alter the flow of control: Cause immediate exit from a repetition structure Exit Do Executed in Do While/Loop, Do/Loop While, Do Until/Loop or Do/Loop Until structures. Exit For Executed in For structures Exit While Executed in While structures

counter is 3 when loop starts, specified to execute until it is greater than 10

Program Output

Logical Operators To handle multiple conditions more efficiently, Visual Basic provides logical operators that can be used to form complex conditions by combining simple ones. The logical operators are AndAlso, And, OrElse, Or, Xor and Not. We consider examples that use each of these operators.

Logical Operators Logical operator with short-circuit evaluation: Execute only until truth or falsity is known: AndAlso operator Returns true if and only if both conditions are true OrElse operator Returns true if either or both of two conditions are true

Logical Operators 2. Logical Operators without short-circuit evaluation: And and Or Similar to AndAlso and OrElse respectively Always execute both of their operands Used when an operand has a side effect Condition makes a modification to a variable Should be avoided to reduce subtle errors Xor Returns true if and only if one operand is true and the other false

Logical Operators Normally, there is no compelling reason to use the And and Or operators instead of AndAlso and OrElse. However, some programmers make use of them when the right operand of a condition produces a side effect (such as a modification of a variable’s value) or if the right operand includes a required method call, as in the following program segment: Console.WriteLine("How old are you?") If (gender = "F" And Console.ReadLine() >= 65) Then Console.WriteLine("You are a female senior citizen.") End If Here, the And operator guarantees that the condition Console.ReadLine() >= 65 is evaluated, so ReadLine is called regardless of whether the overall expression is true or false. It would be better to write this code as two separate statements—the first would store the result of Console.ReadLine() in a variable, then the second would use that variable with the AndAlso operator in the condition.

Logical Operators Logical Negation Not Used to reverse the meaning of a condition Unary operator Requires one operand Can usually be avoided by expressing a condition differently

Logical Operators Fig. 5.17 Truth table for the AndAlso (logical AND) operator.

Logical Operators Fig. 5.18 Truth table for the OrElse (logical OR) operator.

Logical Operators Fig. 5.19 Truth table for the boolean logical exclusive OR (Xor) operator. Fig. 5.20 Truth table for operator Not (logical NOT).

Logical Operators Fig. 5.22 Precedence and associativity of the operators discussed so far.

Structured Programming Summary Promotes simplicity Produces programs that are easier to understand, test, debug and modify Rules for Forming Structured Programs If followed, an unstructured flowchart cannot be created Only three forms of control needed Sequence Selection If/Then structure sufficient to provide any form of selection Repetition While structure sufficient to provide any form of repetition

Structured Programming Summary Sequence Selection If/Then structure (single selection) Select Case structure (multiple selection) T F . . . If/Then/Else structure (double selection) . . . F T Fig. 5.23 Visual Basic’s single-entry/single-exit sequence and selection structures.

Structured Programming Summary Repetition While structure For/Next structure T F T F Do/Loop Until structure Do/Loop While structure F T T F Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures.

Structured Programming Summary Repetition Do While/Loop structure Do Until/Loop structure T F F T For Each/Next structure T F Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures.