ElseIf: Another Conditional Statement

Similar documents
Iteration -- Once Is Not Enough

What Is In the MsgBox?

3.4. FOR-LOOPS 65. for <v a r i a b l e > in < sequence >:

Functions and Procedures. Functions. Built In Functions. Built In Functions in VB FIT 100 FIT 100

What Have You Learned About Programming So Far? Expressions

Programming for Experimental Research. Flow Control

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

This exam is worth 30 points, or about 28% of your total course grade. The exam contains

Recursion. CSCI 112: Programming in C

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

Set and Set Operations

C++ Programming: From Problem Analysis to Program Design, Third Edition

ECE 122. Engineering Problem Solving with Java

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Textbook. Topic 5: Repetition. Types of Loops. Repetition

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

Controls Structure for Repetition

Loop Structures. Loop Structures. Algorithm to record 5 TV programmes. Recall Structured Programming..3 basic control structures.

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Loops. Upsorn Praphamontripong. CS 1110/CS 1111 Introduction to Programming Spring 2018

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Developing Algorithms

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

Semantics of programming languages

Control Structures 1 / 17

Conditionals and Recursion. Python Part 4

IDENTIFYING UNIQUE VALUES IN AN ARRAY OR RANGE (VBA)

CS1100 Introduction to Programming

2.2 (a) Statement, subroutine, procedure, function, parameter, loop

CMSC 201 Fall 2018 Lab 04 While Loops

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Fundamentals of Programming Session 13

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Computational Expression

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

St. Benedict s High School. Computing Science. Software Design & Development. (Part 1 Computer Programming) National 5

LOOPS. Repetition using the while statement

STUDENT LESSON A12 Iterations

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

PHP. control structures - loops indexed arrays. Recap: PHP files are processed top to bottom in sequence. Starting at the top

Decision Making and Loops

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Lecture 7 Tao Wang 1

Loops / Repetition Statements

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

Control Structures II. Repetition (Loops)

Programming Concepts and Skills. ASCII and other codes

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Chapter 5: Control Structures II (Repetition)

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Homework. Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Goals of this Lecture

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

CS 61A Midterm #2 - October 5, 1998

Relational Operators. > greater than < less than >= greater than or equal to <= less than or equal to <> not equal to = equal to

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Lab 5 - Repetition. September 26, 2018

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Language Fundamentals

CPSC 427: Object-Oriented Programming

C++ Programming Lecture 1 Software Engineering Group

Pointers, Arrays and Parameters

Java Review. Fundamentals of Computer Science

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Binary Search and Worst-Case Analysis

Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

Binary Search and Worst-Case Analysis

Programming. Why is programming fun?

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

CS1004: Intro to CS in Java, Spring 2005

142

Structure of Programming Languages Lecture 5

4 Algorithms. Definition of an Algorithm

20. VB Programming Fundamentals Variables and Procedures

LSP 121. LSP 121 Math and Tech Literacy II. Topics. Loops. Loops. Loops

Function: function procedures and sub procedures share the same characteristics, with

EE 121. Office suite. Lecture 3: Introduction to programming and C programming language

Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals

Intro. Scheme Basics. scm> 5 5. scm>

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Quiz 1: Functions and Procedures

UNIT 2 USING VB TO EXPAND OUR KNOWLEDGE OF PROGRAMMING

Transcription:

If This, Then What? If color = true Then If thecolor = blue Then lblsuess.caption = blue fish lblsuess.caption = red fish lblsuess.caption = thenumber & & fish Take out a piece of paper. Write your name on it. What does this program put into the label Suess if the variables have the following values: A. color = false; thecolor = blue ; thenumber = one B. color = true; thecolor = green ; thenumber = two C. color = true; thecolor = blue ; the Number = three (I ll collect these at the end of class today. These won t be graded.) If: Another Conditional Statement The conditional statement (If-Then-) is the only way (so far) to control which statements are executed In VB6, If solves the problem of testing a long sequence of alternatives If <T/F condition> Then <statement list> Stmts for 1st cond If <T/F condition> Then <statement list> Stmts for 2nd cond If <T/F condition> Then <statement list> Stmts for 3rd cond If <T/F condition> Then <statement list> Stmts for 4th cond... <statement list> Stmts for otherwise

Example If txtnum.text = 1 Then Beatle = John If txtnum.text = 2 Then Beatle = Paul If txtnum.text = 3 Then Beatle = George If txtnum.text = 4 Then Beatle = Ringo Beatle = Who? EndIf Executed if Text = 1 Executed if Text 1 and Text = 2 Executed if Text 1 or 2 and Text = 3 Executed if Text 1 or 2 or 3 and Text = 4 Caution With If An If statement that uses If passes through all of the previous cases before reaching a given test. Think about the consequences If somevar < 20 Then Result = Less than 20 If somevar < 10 Then Result = Less than 10... Will this statement ever be executed?

Contrast With Nested If If is not a nested test as seen before, though the logic is similar If txtnum.text = 1 Then Beatle = John If txtnum.text = 2 Then Beatle = Paul If txtnum.text = 3 Then Beatle = George If txtnum.text = 4 Then Beatle = Ringo Beatle = Who? If txtnum.text = 1 Then Beatle = John If txtnum.text = 2 Then Beatle = Paul If txtnum.text = 3 Then Beatle = George If txtnum.text = 4 Then Beatle = Ringo Beatle = Who? A Thought Experiment Think about writing a program to output the following: 10 seconds 9 seconds 8 seconds 7 seconds 6 seconds 5 seconds 4 seconds 3 seconds 2 seconds 1 second BLAST OFF!!!

Iteration -- Once Is Not Enough Though people don t like to repeat themselves, computers can repeat steps systematically without tiring. If program instructions are to be performed more than once, the computer can be programmed to repeat the instructions without explicitly writing out new instructions each time. The Idea of Iteration CONCEPT: Iteration is the repeated execution of a series of statements in a program The basic idea behind iteration is to: Repeat a bunch of steps STOP at some point and continue with the rest of the program To perform iteration, programming languages include special statements often called iteration statements

Key Elements: Repeating Statements and a Stop Condition CONCEPT: There are two crucial components of all iterations: The statements that will be repeated -- called the loop body A test specifying when the repetition stops stop condition Additionally, loops typically have at least one variable that is explicitly changed inside the loop -- this is called the iteration variable When the iteration variable contains a certain value (defined by the program), then the loop stops Some value must change at some point between consecutive iterations, or else the loop will never terminate it is an infinite loop. General Form Of VB6 Iteration VB6, like most languages, has several iteration statements but we will only study one here the Do-While loop Do While <termination condition> <statements> Loop The semantics are as follows: The termination condition is tested and if it is false the statements are all skipped; execution continues after Loop If it is true, the statements are performed once The termination condition is tested again, and if it is false the loop is over and the statements are skipped; continue after Loop If it is true, the statements are performed a second time

An Example An easy way to get the idea of iteration is to print out the iteration variable... Declaration of iteration variable Private Sub Form_Click() Dim iteratevar As Integer Initialization of iteration variable iteratevar = 0 Do While iteratevar < 10 iteratevar = iteratevar + 1 Loop Body MsgBox ( iteratevar is & iteratevar) Loop End Sub Some Questions to Consider Why does iteratevar have the initial value of 1? Why does the loop end? How many times does the loop execute? Private Sub Form_Click() Dim iteratevar As Integer iteratevar = 0 Do While iteratevar < 10 iteratevar = iteratevar + 1 MsgBox ( iteratevar is & iteratevar) Loop End Sub