CP1020 Week 5 Selection Continued. CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements.

Slides:



Advertisements
Similar presentations
Logic & program control part 3: Compound selection structures.
Advertisements

30/04/ Selection Nested If structures & Complex Multiple Conditions.
Selection (decision) control structure Learning objective
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Conditional Statements Introduction to Computing Science and Programming I.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
CP Lecture 11 Functions and advanced procedures.
CP Week 10 Modularising programs using Procedures.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
CP Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice.
CP Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation.
Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select.
Chapter 6 Control Structures.
Developing Software Applications Software Life Cycle Specification Design Implementation Testing Maintenance.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
CP Week 9 Working with arrays. What are we doing this week ? zHow to store information in an array zHow to retrieve information from an array zA.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson1 Week 1 - Principles of programming Welcome from the Presenters Steve Garner and Dr Ian.
CP Principles of Programming Steve Garner and Ian Coulson CP Week 8 Arrays.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions.
CP Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Selection Structures (if & switch statements) (CS1123)
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
26/10/ Selection Nested If structures & Complex Multiple Conditions.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
22/11/ Selection If selection construct.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Python Conditionals chapter 5
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Week 9 : Text processing (Reading and writing files)
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
Lesson Objectives Aims To be able to write an algorithm in Pseudo Code
The if…else Selection Statement
Topic: Iterative Statements – Part 1 -> for loop
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
VB.Net Programming Console Application
© 2010 David A Watt, University of Glasgow
Chapter 4 MATLAB Programming
Lecture 9 Shell Programming – Command substitution
Chapter Topics 11.1 Introduction to Menu-Driven Programs
Department Array in Visual Basic
Midterm Review Programming in Fortran
SEASONS.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
If selection construct
If selection construct
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Looping III (do … while statement)
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Python Basics with Jupyter Notebook
Loops and Simple Functions
CS 101 First Exam Review.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Presentation transcript:

CP1020 Week 5 Selection Continued

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements to make selection from a list of possible cases, as seen from the following programme

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else programme DIM iMark AS INTEGER INPUT “Please enter your exam mark ( )”; iMark If iMark >= 0 AND iMark < 40 THEN PRINT “You need to resit your exam again” ELSEIF iMark >= 40 AND iMark < 60 THEN PRINT “You have passed your exam” ELSEIF iMark >= 60 AND iMark <75 THEN PRINT “You have passed with a merit” ELSEIF iMark >= 75 AND iMark <= 100 THEN PRINT “You have passed with Distinction” ELSE PRINT “Your value lies outside the range” ENDIF

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Alternative - Select Case The syntax is as follows: SELECT CASE Variablename CASE value1 statements CASE value2 statements CASE ELSE statement END SELECT Do this If none of the cases are selected then do this Upon the first case being satisfied then program moves to END SELECT

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Select case programme REM A menu selection programme REM Ian Coulson REM 30/10/97 DIM sMenuChoice AS STRING INPUT “Choose menu a,b or c > ”, sMenuChoice SELECT CASE sMenuChoice CASE “a” PRINT “menu a chosen” CASE “b”, “c” PRINT “menu b or c chosen” CASE ELSE PRINT “invalid input!” END SELECT Suppose user enters “b”

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Select case continued INPUT “Please enter your exam mark ( )”; iMark SELECT CASE iMark CASE 0 TO 40 PRINT “You need to resit your exam” CASE 40 TO 60 PRINT “ you have passed your exam” CASE 60 TO 75 PRINT “You have passed with merit” CASE 75 TO 100 PRINT “You have passed with distinction” CASE ELSE PRINT “Your mark must be in the range 0 to 100 END SELECT

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Indenting control structures We can make it easier to follow the flow of a programme when reading it by using a formatting convention. The statements are indented after a control statement:- BEFORE IF iMark >= 0 AND iMark < 20 THEN PRINT “You need to resit your exam again” ELSEIF iMark >= 40 AND iMark < 60 THEN PRINT “You have passed your exam” ELSEIF iMark >= 60 AND iMark <75 THEN PRINT “You have passed with a merit”

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Indentation AFTER: Would be written as: If iMark >= 0 AND iMark < 20 THEN PRINT “You need to resit your exam again” ELSEIF iMark >= 40 AND iMark < 60 THEN PRINT “You have passed your exam” ELSEIF iMark >= 60 AND iMark <75 THEN PRINT “You have passed with a merit” END IF This is much easier to follow

CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Questions 1. Write a Case Select statement to Print the month name given the month number. i.e. 7 - July 2. Write a Case Select statement to Print the words Spring, Summer, Autumn or Winter given the Month name. i.e July - Summer Return to view another lecture