Chapter 4 The If Then Statement

Similar documents
Repetition Structures

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

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

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Introduction. C provides two styles of flow control:

Programming Logic and Design Sixth Edition

A Fast Review of C Essentials Part II

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

C-LANGUAGE CURRICULAM

Simple Java Programming Constructs 4

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Introduction to Programming

Chapter 2: Functions and Control Structures

Microsoft Visual Basic 2005: Reloaded

CSI33 Data Structures

Key Differences Between Python and Java

DECISION MAKING STATEMENTS

Fundamentals of Programming Session 12

CS302: Self Check Quiz 2

Introduction to C Programming

Information Science 1

Information Science 1

Programming Lecture 4

Programming, numerics and optimization

5. Control Statements

Fundamentals of Programming Session 13

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Chapter 8 Statement-Level Control Structures

Programmers should write code that is self-documenting and split into small sections.

Programming Lecture 4

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Quiz 1: Functions and Procedures

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Lecture 5 Tao Wang 1

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

LECTURE 04 MAKING DECISIONS

Flow Control. CSC215 Lecture

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Python Unit

Chapter 4: Making Decisions

Chapter 8. Statement-Level Control Structures

Skill Area 306: Develop and Implement Computer Program

PYTHON- AN INNOVATION

Chapter 4: Making Decisions

Chapter 4 Lab. Loops and Files. Objectives. Introduction

SOFT 161. Class Meeting 1.6

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

STUDENT LESSON A12 Iterations

Programming Logic & Pseudocode. Python Bootcamp, Day 1 Anna Rosen

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

JML What are model fields? Translation to JavaDL Demo. JML Model Fields. Christian Engel. ITI, Universität Karlsruhe. 08.

Outline. Program development cycle. Algorithms development and representation. Examples.

CT 229 Java Syntax Continued

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Programming Lecture 4

VISUAL BASIC 6.0 OVERVIEW

Java Review. Fundamentals of Computer Science

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Selection Statements. Pseudocode

Chapter 3 Structured Program Development

Module 2: Choice and Iteration

5. Selection: If and Switch Controls

Chapter Two: Program Design Process and Logic

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Program Elements -- Introduction

CS 106 Introduction to Computer Science I

Chapter 12 Variables and Operators

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

SMURF Language Reference Manual Serial MUsic Represented as Functions

Algorithms and Conditionals

Chapter 12 Variables and Operators

CS 1313 Spring 2000 Lecture Outline

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Chapter 4: Control Structures I (Selection)

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu

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

ECE 122. Engineering Problem Solving with Java

Introduction to Programming Using Java (98-388)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Statements and Operators

Text Input and Conditionals

Java Programming Fundamentals. Visit for more.

Chapter 3. More Flow of Control

Higher Computing Science Software Design and Development - Programming Summary Notes

Chapter 7: Javascript: Control Statements. Background and Terminology. Background and Terminology. Background and Terminology

Lesson 04. Control Structures I : Decision Making. MIT 31043, VISUAL PROGRAMMING By: S. Sabraz Nawaz

Transcription:

The If Then Statement Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression For example, the statement If x = 5 Then y = 20 End If assigns the value 20 to y only if x is equal to 5. Slide 1

Relational Operators Operator Meaning = equal to < less than <= less than or equal to > greater than >= greater than or equal to <> not equal to Slide 2

The If Then Else Statement Contains an Else clause that is executed when the If condition evaluates to false. For example, the statement If x = 5 Then y = 20 Else y = 10 End If assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5. Slide 3

Nested If Then Else Statements Should be indented to make the logic clear. Nested statement executed only when the branch it is in is executed. For example, the statement If x = 5 Then y = 20 Slide 4 Else If x > 5 Then y = 10 Else y = 0 End If End If evaluates the nested If Then Else only when x is not equal to 5.

The If Then ElseIf Statement Used to decide among three or more actions. Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement If x < 5 Then y = 20 ElseIf x < 10 Then y = 40 ElseIf x < 15 Then y = 80 End If would give very different results if the conditions were ordered differently. Slide 5

The Select Case Statement The result of an expression determines which statements to execute. The Case Else code is optional and is executed when none of the previous cases are met: Select Case numlegs Case 2 Me.lblMessage.Text = "human" Case 4 Me.lblMessage.Text = "beast" Case 8 Me.lblMessage.Text = "insect" Case Else Me.lblMessage.Text = "???" End Select Slide 6

The Select Case Is Statement Compares the result of an expression to a range of values to determine which statements to execute. For example: Select Case score Case Is < 10 Me.lblMessage.Text = "Nice try." Case Is < 25 Me.lblMessage.Text = "Good." Case Is >= 25 Me.lblMessage.Text = "Great!" End Select Slide 7

The Rnd() Function Uses a formula to generate a sequence of numbers that are each greater than 0 and less than 1 and then returns one number from the sequence. A random integer in a range is generated by using the formula: (highnum lownum + 1) * Rnd() + lownum Random integers are produced by using the Int() function along with the Rnd() function: Int(21 * Rnd() + 10) '10 to 30 The Randomize() statement initializes the random number generator. Slide 8

Algorithms A set of steps that outline how to solve a problem. Can be implemented in plain English or in a mix of English and program code called pseudocode. Algorithms allow a programmer to think through a program before actually typing code, which may reduce errors in logic. Slide 9

Static Variables Declared with the keyword Static. Have a lifetime the duration of the program's running time. Used to extend the lifetime of local variables in a procedure. Should be explicitly initialized when declared. A better choice than a global variable because the scope of the variable can be limited. Slide 10

Compound Boolean Expressions More than one Boolean expression in a single condition. Formed using the And, Or, or Not operators. Slide 11

And Truth Table And Exp1 Exp2 Result True True True True False False False True False False False False Slide 12

Or Truth Table Or Exp1 Exp2 Result True True True True False True False True True False False False Slide 13

Not Truth Table Exp True False Not Result False True Slide 14

The MessageBox Class A predefined dialog box that displays a message to the user. Includes the Show() method for displaying the dialog box. For example: MessageBox.Show(message) Slide 15

Counter Variables A variable that is incremented by a constant value. Used for counting guesses, the numbers of values entered, the number of times a button was clicked, and so on. The value of a counter is updated in a statement similar to: counter = counter + 1 Should be initialized when declared and updated by an unchanging amount. Slide 16

Assignment Operators Operator Operation += addition and then assignment -= subtraction and then assignment Slide 17

The CheckBox Control (Name) should begin with chk. Text is the text displayed next to the box. Checked is set to True if the box should be displayed as checked. An If Then statement is often used to determine if a check box is checked or cleared. Slide 18

Line-Continuation Character The underscore character is the line-continuation character. There must be a space before and nothing after and cannot be within quotation marks. Used for dividing code to make it more readable. Slide 19