Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

ITEC113 Algorithms and Programming Techniques
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
true (any other value but zero) false (zero) expression Statement 2
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Module 10: Simple Inheritance #1 2000/01Scientific Computing in OOCourse code 3C59 Module 10: Simple Inheritance In this module we will cover Inheritance.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CPS120: Introduction to Computer Science Decision Making in Programs.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 3: Decisions and Loops
Sequence, Selection, Iteration The IF Statement
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Expressions and Control Flow in JavaScript
Control Structures.
11/10/2018.
Selection CSCE 121 J. Michael Moore.
Coding Concepts (Basics)
Chapter 7 Conditional Statements
Computer Science Core Concepts
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Programming
Arrays Introduction to Arrays Reading for this Lecture:
REPETITION Why Repetition?
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated constants For While and Do In this module we will cover:

Module 3: Steering&Arrays #2 2000/01Scientific Computing in OOCourse code 3C59 Aims of the module The previous modules have concentrated on programming philosophy. As such they have until now only touched upon very basic C++ language components. In this module we extend this to cover C components which are in practice needed in almost all programming situations. These are the constructs needed to make branching decisions in programs. Nono of this has anything sprecific to do with OO programming, but you cant do many more useful exercises until this material is covered.

Module 3: Steering&Arrays #3 2000/01Scientific Computing in OOCourse code 3C59 One of the most common operations you want to do is take different actions as a result of some test. We have already seen examples of this in the previous modules. The simplest example is the if construct shown here: // Example of if usage if( a > b ) {.. any code you like } 1.1 if, else if, else Here is the test The a > b form is fairly obvious. We will cover more forms in a moment If the test is passed then the code in {..} is executed

Module 3: Steering&Arrays #4 2000/01Scientific Computing in OOCourse code 3C59 This can be made more useful by extending it with the optional else if, else // Example of if usage if( a > b ) {.. any code you like } else if( a < c ) {.. any code you like } else {.. Any code you like } Only if the first test fails will this second (optional) test be performed. If it passes the following code in {..} is executed. You can have many of these Optionally you can specify this “catch all” to be executed if all the other tests fail.

Module 3: Steering&Arrays #5 2000/01Scientific Computing in OOCourse code 3C59 Finally we need to look at some of the forms of test which can appear: // Examples of tests in if() // Greater or less than are obvious if( a > b ) {..} if( a < b ) {..} // also greater than or equal to.. if( a >= b ) {..} if( a <= b ) {..} // Equals to is a funny one if( a = = b ) {..} // Not equal to if( a != b ) {..} // You can AND or OR two tests if( (test1) && (test2) ) {..} if( (test1) || (test2) ) {..} Private Study: Familiarise with the forms of expression which can appear inside an if(..)

Module 3: Steering&Arrays #6 2000/01Scientific Computing in OOCourse code 3C59 In fact it is more general than it looks. The rule is that anything can appear in the test which evaluates to a logical true or false conclusion. // use of general logical value int valid ; bool valid ;.. some code which sets a value of valid.. if( valid ) { // this will be executed if valid is true } // or you might see if( !valid ) { // executed if valid is false } What you don’t see in the previous examples is that expressions like a > b actually evaluate inside C++ to true or false

Module 3: Steering&Arrays #7 2000/01Scientific Computing in OOCourse code 3C59 This means that any function which returns true or false can be used in the test directly. For example, lets invent a Particle class in our minds. Suppose this has a method which returned true if the particle was massless, and false otherwise. Lets call this method isMassless( ) Then you could write the following code: // Use of a function in a test Particle p ;.. Some code which sets the attributes of p to something if( p.isMassless() ) { // This code will be executed if p is massless }

Module 3: Steering&Arrays #8 2000/01Scientific Computing in OOCourse code 3C59 Another construct which allows branching is via use of the SWITCH / CASE statements. However first you have to be introduced to a new data type known as an “enumerated constant” [It will probably take you a few goes to get your head around this one - so expect to cover it in private study and in surgery] 1.2 SWITCH

Module 3: Steering&Arrays #9 2000/01Scientific Computing in OOCourse code 3C59 // Example of setting up a list of enumerated constants enum ParticleType { ELECTRON, MUON, TAU, QUARK }; // Now we can declare variables which can only be set to the symbolic values in the list enum ParticleType myParticleType ; // This is allowed myParticleType = ELECTRON ; // These are not allowed myParticleType = GRAVITON ; myParticleType = 10 ; “enumerated constants” This makes a variable called: myParticleType which can only be set to the values specified in the list associated with enum ParticleType

Module 3: Steering&Arrays # /01Scientific Computing in OOCourse code 3C59 switch( myParticleType ) { case ELECTRON: std::cout << “The particle was an electon“ ; break ; case MUON: std::cout << “The particle was an muon “ ; break ; case TAU: std::cout << “The particle was an tau “ ; break ; case QUARK: std::cout << “The particle was an quark “; break ; case DEFAULT: std::cout << “I havnt a clue what it was “; break ; } The switch looks at the value of this Now we can see the switch/case statements in action using the enumerated list on the last slide The possible cases are all in the {..} A stupidity - you have to explicitly break at the end of what you want to do for each case Catch-all in case none match.

Module 3: Steering&Arrays # /01Scientific Computing in OOCourse code 3C59 Now we move to constructs used to repeat a piece of code until some condition is satisfied. One way to do this is with the for statement: 1.3 FOR // Use of for in C++ int index ; int numberOfThings = 10 ; for( index = 0; index < numberOfThings; index++ ) { // some code executed until test fails } Initial action Test at end of each iteration. The loop continues until this fails Action at end of each loop

Module 3: Steering&Arrays # /01Scientific Computing in OOCourse code 3C59 This can be made arbitrarily obtuse and complicated !!!!!!! All you need to note at this stage is that for loops are not limited to simple integer indicies. You can do anything in the three fields which satistfy the requirements of the previous slide, I.e. ( initial action ; logical test ; action at end of loop)

Module 3: Steering&Arrays # /01Scientific Computing in OOCourse code 3C59 Another way to repeat code is to use the while or do statements: 1.5 do / while // Use of while in while( any test ) { // some code executed until test fails } Test at beginning of each iteration. The loop continues until this fails

Module 3: Steering&Arrays # /01Scientific Computing in OOCourse code 3C59 // Use of do in C do { // some code executed until test fails } while( any test ) Test at end of each iteration. The loop continues until this fails

Module 3: Steering&Arrays # /01Scientific Computing in OOCourse code 3C59 Summary of Module 3: Algorithm steering elements The if statement if { } else if { } else{ } The switch statement enumerated constants switch / case The for staatment for(... ;... ;... ) { } The do and while statements while( test ) { } do { } while( test )