Case & Repetitive Statements

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Pertemuan 06 Repetition Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Chapter 6 - Visual Basic Schneider
5.05 Apply Looping Structures
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
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.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Controlling Program Flow with Looping Structures
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
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.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
`. Data Types Integer - This data type is used to represent integer. Float - This data type is used to represent floating point number. Double - This.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
UNIT 5 Lesson 15 Looping.
Chapter 6: Loops.
Chapter 5- Control Structures: Part 2
Visual Basic 6 (VB6) Data Types, And Operators
Lecture 7: Repeating a Known Number of Times
Problem Solving and Control Statements: Part 2
Chapter 6 More Conditionals and Loops
Control Structures: 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.
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
The University of Texas – Pan American
Outline Altering flow of control Boolean expressions
Problem Solving and Control Statements
What output is produced by the following fragment?
Control Structures Part 3
Control Structures Part 1
JavaScript: Control Statements II
Prepared By: Deborah Becker
PROGRAM FLOWCHART Iteration Statements.
ASP control structure BRANCHING STATEMENTS
CSCI 1100/1202 February 6, 2002.
Presentation transcript:

Case & Repetitive Statements Skill Area 306.2 Materials Prepared by Dhimas Ruswanto, BMm

Lecture Overview Case Statement Do While…Loop Do…Loop While Do Until…Loop Do…Loop Until For…Next

Case Statement

Case Statement Its objective is to check several possible constant values for an expression. Something similar to what we did at the beginning of this section with the concatenation of several if and else if instructions.

Case Statement Select Case (expression) Case constant1 The syntax for the case statement is as follows: Select Case (expression) Case constant1 group of statements 1 Case constant2 group of statements 2 . . . Case Else group of statements End Select

Case Statement (Example) Dim num As Integer Dim msg As String num = Val(txtDisplay.Text) Select Case num Case 1 To 50 msg = "Pass“ Case 71, 76, 79 To 100 msg = "High" Case Is > 101 msg = "Higher“ Case Else msg = "Invalid“ End Select MsgBox(msg)

Case Statement (Example) Both of the following code fragments have the same behavior: Case example if-else equivalent Select Case name Case "Adam" MsgBox(“Position is MANAGER") Case "Bob” MsgBox(“Position is SUPERVISOR") Case "Charlie” MsgBox(“Position is ACCOUNTANT") Case "Eve" MsgBox(“Position is SECRETARY") Case Else MsgBox("No data found") End Select If name = "Adam" Then ElseIf name = "Bob" Then ElseIf name = "Charlie" Then ElseIf name = "Eve" Then Else End If

Types of repetition statements: A repetition statement can repeat actions, depending on the value of a condition (which can be either truer or false) Types of repetition statements: Do While…Loop Do…Loop While Do Until…Loop Do…Loop Until

Do While…loop & do Loop…while

Do While..Loop & Do..Loop While Do While..Loop & Do..Loop While – a control statement that executes a set of body statements while its loop-continuation condition is True Loop-continuation condition – the condition used in a repetition statement that enables repetition to continue while the condition is True and that causes repetition to terminate when the condition becomes False Syntax: Do While loop-continuation condition block of statements Loop Do block of statements Loop While loop-continuation condition

Do While..Loop & Do..Loop While Example: Do While product <= 50 product += 3 Loop Do product +=3 Loop While product <= 50 In the Do While..Loop statement, the loop-continuation condition is tested at the beginning of the loop, before the body of the loop is performed. In Do..Loop While statement performs the loop-continuation condition after the loop body if performed. Therefore, in a Do..Loop While statement, the loop body always executes at least once. A Do While..Loop executes only if its loop-continuation condition evaluates to true.

Do While..Loop & Do..Loop While Example: Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop Dim number As Integer Do   MsgBox("The number is " & number)  number = number + 1 Loop While number < 4

Do While..Loop & Do..Loop While Example: Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop The output of the code above will be different with the code below Dim number As Integer Do While number < 4   number = number + 1 Loop MsgBox("The number is " & number) 

Do until…loop & do Loop…until

Do Until..Loop & Do..Loop Until Do Until..Loop & Do..Loop Until– a control statement that executes a set of body statements while its loop-termination condition becomes True. Loop-termination condition – the condition used in a repetition statement that enables repetition to continue while the condition is False and that causes repetition to terminate when the condition becomes True Syntax: Do Until loop-termination condition block of statements Loop Do block of statements Loop Until loop-termination condition

Do Until..Loop & Do..Loop Until Example: Do Until product >= 50 product += 3 Loop Do product +=3 Loop Until product >= 50 In the Do Until..Loop statement, the loop-termination condition is tested at the beginning of the loop, before the body of the loop is performed. In Do..Loop Until statement performs the loop-termination condition after the loop body if performed. Therefore, in a Do..Loop Until statement, the loop body always executes at least once. A Do Until..Loop executes only if its loop-termination condition evaluates to true.

Do While..Loop & Do Until..Loop Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop Do Until…Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number)   number = number + 1 Loop

For…next

Counter-controlled Repetition Four essential elements of counter-controlled repetition: The name of a control variable (or loop counter) that is used to determine whether the loop continues to iterate The initial value of the control variable The increment (or decrement) by which the control variable is modified during each iteration of the loop (that is, each time the loop is performed) The condition that tests for the final value of the control variable (to determine whether looping should continue)

answer = answer + startNumber For…Next Statement Optional (depends on the program) Syntax: For controlVariable = initialValue To finalValue Step increment statements Next Example Dim counter As Integer For counter = 2 To 10 Step 2 MsgBox("The number is " & counter) Next Dim answer As Integer Dim startNumber As Integer answer = 0 For startNumber = 1 To 4 answer += startNumber MsgBox(answer) Next answer = answer + startNumber

For…Next vs. Do While..Loop vs. Do Until..Loop Dim number As Integer Do While number < 4  MsgBox("The number is " & number)  number = number + 1 Loop Do Until…Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number)   number = number + 1 Loop For..Next Dim number As Integer For number = 1 To 4 MsgBox("The number is " & number)   Next

--- continue to next slide ---