Controlling Program Flow with Decision Structures.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

5.04 Apply Decision Making Structures
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
C++ for Engineers and Scientists Third Edition
Microsoft Visual Basic 2008: Reloaded Fourth Edition
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
CIS 115 Lecture 7 Selection / Decisions.
Chapter 4 The If…Then Statement
Computer Science Selection Structures.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Flow of Control Part 1: Selection
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
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.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Controlling Program Flow with Looping 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.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Chapter 6 Controlling Program Flow with Looping Structures.
Computer Science Up Down Controls, Decisions and Random Numbers.
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.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 4: Decisions and Conditions
5.04 Apply Decision Making Structures
A variable is a name for a value stored in memory.
Chapter 4: Decisions and Conditions
Chapter 4 The If…Then Statement
Visual Basic 6 (VB6) Data Types, And Operators
UNIT 4 Lesson 13 If statements.
Chapter 4 – Control Structures Part 1
CHAPTER FIVE Decision Structures.
Making Decisions in a Program
Lesson 8: Boolean Expressions and "if" Statements
Chapter 3: Introduction to Problem Solving and Control Statements
CIS16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Presentation transcript:

Controlling Program Flow with Decision Structures

If…Then Statement Is a Decision Structure that executes a set of statements when a condition is true. Takes the form: If condition then statements End if Ex: in the following If..Then statement intguess = 7 is the condition If intguess = 7 then lblmessage.text = “You guessed it” End If

The condition of an If…Then statement is a Boolean expression, which evaluates true/false or yes/no Relational Operators: Operator Meaning = Equal to < Less than < = Less than or equal to > Greater than >= Greater than or equal to <> Not equal to

If…Then… Else Statement This includes and optional Else clause that is executed when the If condition evaluates to False. This is a two way decision statement Takes the form: If condition then statements Else statements End If The End if must be at the end of every if…then statement regardless of how many decisions

Example: If..Then..Else If inttemp <= 32 then lblcomment.text = “Freezing” Else inttemp > 80 then lblcomment.text = “Hot” End If

If…Then…ElseIf This statement is used to decide between three or more actions Takes the form: If inttemp <= 32 then lblcomment.text = “Freezing” Elseif inttemp > 80 then lblcomment.text = “hot” Else lblcomment.text = “Moderate” End If

If…Then Decision Statements One Way Decision- If…Then Two Way Decision- If…Then…Else Multi-Way Decision- If…Then…ElseIf

Message Box A message box is a PREDEFINED DIALOG box. PREDEFINED means is the form is already created, you just add the text A message box is used to DISPLAY information to the user

Message Box cont. The message box includes a Show( ) method for displaying a Message Box –MessageBox.Show(message) Example: If intguess 50 then MessageBox.Show(“Guess out of Range”) Elseif intguess = insecretnumber then MessageBox.Show(“You guessed it”) End if

Nested If…Then…Else Statements An If..then…else statement can hold another If…then..else statement That is called a nested If statement If intguess = intsecretnumer then Lblmessage.text = “you guessed it” Else If inguess < intsecretnumber then Lblmessage.text = “too low” End If

Select…Case Statement Is a decision statement structure that uses the result of an expression to determine which block of code to execute Takes the Form: Select expression Case value End Select

Select… Case Is Compares the result of an expression to a range of values when a relational operator is part of the value Example: Select Case intscore Case Is <10 lblmessage.text = “nice Try” Case Is <25 lblmessage.text = “good” End Select

Random Numbers RND( )- generates random numbers greater than or equal to 0 and less than 1 To generate numbers in a greater range, Rnd( ) is multiplied by the upper limit of the range

Random Numbers This creates numbers from 0 to the upper limit. For example to generate numbers that are greater than or equal to 0 and less than 10 a statement is as such: lblrandom1.text = Rnd( ) * 10

To find random numbers in a lower range: (High number – Low number + 1) * Rnd( ) + Low Number High Number is the maximum value desired Low Number is the minimum value Looking for numbers greater than or equal to 10 and less than 30 lblrandom1.text = (30 – ) * Rnd( ) + 10 = 21 * Rnd( ) + 10

Random Numbers cont. Numbers generated by Rnd( ) are real numbers with decimal portions. To produce random integers (whole numbers) the Int( ) function can be used to return integer portion without rounding lblrandnum1.text = Int(21 * Rnd( ) ) + 10

Algorithms A set of steps that tell how to solve a problem An algorithm can be further refined by writing it in pseudocode, which is both English and program code Creating an algorithm and then writing pseudocode allows a programmer to think through the program before actually typing code.

Static Variables Along with variables having scope(the set of statements that can use the variable) they have a lifetime Lifetime: lifetime in which memory exists Lifetime of local variable: duration of the procedure in which it was declared Lifetime of global variable: duration of the program

Static Variables cont. The lifetime of a local variable can be extended by using a Static variable, which takes the form: Static variable_name As type A static variable is declared using the keyword Static instead of Dim A static variable’s scope is local to the procedure in which it was declared, but its lifetime is the duration of the program

Static Variables cont. Static variables are necessary in event procedures with variables that should be retained in memory throughout the program execution. A variable declared in a click event is redeclared and reinitialized each time the click event occurs unless the variable is declared as static

Logical Operators Boolean expressions can also be formed using logical operators. A logical operator joins two expressions to create an expression that evaluates True/False Example: If intguess 50 Then lblmessage.text = “Invalid Guess” Elseif intguess = intsecretnumber then lblmessage.text = “You Guessed It” End If

Counter Variables A counter is a variable storing a number that is incremented by a constant value. Counters are useful in keeping track of how many a user guesses, types a password, uses a program. The statement looks like: counter = counter + 1