Control Structures.  Need to have program statements that control execution flow  Simple statements to branch execution based on conditions (If/Then/Else)

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
5.04 Apply Decision Making Structures
Selection (decision) control structure Learning objective
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
VB.Net Decisions. The If … Then Statement If condition Then Statements End If If condition Then Statements Else Statements End If Condition: –Simple condition:
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
Computer Science Selection Structures.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Lecture Set 5 Control Structures Part A - Decisions Structures.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Branches and Program Design
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
22/11/ Selection If selection construct.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
JavaScript, Fourth Edition
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Decision Structures, String Comparison, Nested Structures
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
Control statements Mostafa Abdallah
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Higher Computing Software Development -So Far- 5/10/10.
Decisions with Select Case and Strings Chapter 4 Part 2.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
CPS120: Introduction to Computer Science Decision Making in Programs.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Logical Operations In Matlab.
Microsoft Visual Basic 2005: Reloaded Second Edition
Presentation transcript:

Control Structures

 Need to have program statements that control execution flow  Simple statements to branch execution based on conditions (If/Then/Else)  Nested If statements  And/Or/XOR  Not  Select Case statements  Class variables

 All of the previous VB.NET that you have been shown are imperative commands:  Do this, then that  Linear  Only branching is some of them are event handlers  It is essential to have conditional branching in your programs:  User input  Based on current machine condition  Other factors

Select random number (1- 100) Yay!! Ask for their guess Is it right ? Tell them if it was low/high No Yes

 Boolean logic (True/False)  Can be complex statement  Often uses variables  Structure is: If Then End If  Condition can use “And” and “Or”  Statement block can be as long and complex as needed  Must ALWAYS have an “End If” to match the If

 As long as the left side resolves to True/False: If 3 * 2 = 6 Then MsgBox("It is = 6!!") End If  More complex: If ChkTest.Checked Then MsgBox("Text box is checked") End If  Other logic: If ChkTest.Checked And ChkTest2.Checked Then MsgBox("Text boxes are both checked") Else MsgBox("They are not both checked") End If

 Else clause executes if the “If” clause fails  Else can have any statement(s) included in the block If Not ChkTest.Checked Then MsgBox("Text box 1 not checked") Else If ChkTest2.Checked Then MsgBox("Both are checked!") End If  Results can be understood in a chart

 You can nest If statements as deeply as you want  Each If must have an “End If” to match it  Can nest as deeply as you want If ChkTest.Checked Then MsgBox("Text box 1 is checked") Else If ChkTest2.Checked Then MsgBox("Checkbox 2 is checked") Else If chktest3.checked Then MsgBox("Checkbox 3 is checked") End If

 ElseIf – Can combine both into one keyword  And – Both conditions must be true  Or – Only one condition may be true  Not – True if the condition is False, otherwise true  Xor – Exclusive Or. Only true if 1 (and only 1) of the conditions is true. Otherwise false

Cond1 (A)Cond2 (B)A Or BA And BA Xor BA nand B False True False True

Cond1 (A)Cond2 (B)A Or BA And BA Xor BA nand B False True FalseTrue FalseTrue FalseTrueFalseTrue False

 String.Empty – Is true if a string has no data in it If txtMyText = String.Empty Then…  ToUpper and ToLower change the case of a string strTest = “Hello” strTest = strTest.ToUpper()  IsNumeric - Is a string expresion numeric strTest = "123abc“ MsgBox(IsNumeric(strTest))  Length – Returns the length of a string MsgBox(“Length is: “ & strTest.Length)

 TrimStart, TrimEnd, Trim – Trim whitespace from a string  Substring – Returns a string from the middle of another string  Multiple versions of this function. Check documentation  IndexOf – Returns an integer with the starting location of a substring within a string strTest = “123abc” MsgBox("Start location is:" & strTest.IndexOf("ab"))

 Complex logic with If/Else can get long and complex  Sometimes you just want to find the one case (of many) that is true  You can use the “Select Case” statement for this  It evaluates each of the cases in turn  The first one that is true is executed  It then continues on to the next case and evaluates it  Can exit the Select Case statement with

Select Case CInt(TxtCheck.Text) Case 0 MsgBox("A zero?? Hard to believe") Case 1 To 59 MsgBox("That's a fail :(") Case 60 To 69 MsgBox("D") Case 70 To 79 MsgBox("C") Case 80 To 89 MsgBox("B") Case 90 To 99 MsgBox("A") Case 100 MsgBox("Wow! Perfect!!") Case Else MsgBox("Not a valid score. Try again") End Select

 Can have individual items  Case 0  Can have a range of items  Case 3, 4  Can have a catchall (to get everything else)  Case Else  Select Case can have a variety of types  Integers  Strings  Virtually any other type

 All variables have a scope. That is, the part of the program for which the variable is recognized  Variables declared in a Subroutine are only defined for that Sub/Function  Class variables are declared at the top of the class, and can be accessed anywhere in that class  If you need access to the variable, you can pass the information in one of two ways:  Input/output argument  Class variables

 Declare a class variable Public Class Form1 Dim aVar as Integer ‘ This variable can be used anywhere in the class  Pass an input variable mySub(44) Private Sub mySub(ByVal intMyInt As Integer) MsgBox(intMyInt) End Sub