Control Structures: Conditionals, If/Else and Loops David Millard

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Understanding the Three Basic Structures
ITEC113 Algorithms and Programming Techniques
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Program Design and Development
Pseudocode and Algorithms
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Programming Fundamentals (750113) Ch1. Problem Solving
C++ for Engineers and Scientists Third Edition
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
The University of Texas – Pan American
Invitation to Computer Science, Java Version, Second Edition.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
CPS120: Introduction to Computer Science Decision Making in Programs.
© Jalal Kawash Programming Peeking into Computer Science 1.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
CPS120: Introduction to Computer Science Decision Making in Programs.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
CPS120: Introduction to Computer Science Decision Making in Programs.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Sequences, Modules and Variables David Millard
Comp1004: Programming in Java II Computational Thinking.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Algorithms 1: Sequences and Modules Yvonne Howard & Rikki Prince
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.
Building Good Solutions David Millard
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Introduction To Flowcharting
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Programming Fundamentals
ALGORITHMS AND FLOWCHARTS
More Selections BIS1523 – Lecture 9.
Comp1202: Building Better Programs
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Understanding the Three Basic Structures
ALGORITHMS AND FLOWCHARTS
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Chapter 5: Control Structure
Computer Science Core Concepts
ICT Programming Lesson 3:
Programming Fundamentals (750113) Ch1. Problem Solving
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
ICT Gaming Lesson 2.
Class 4: Repetition Pretest Posttest Counting Flowchart these!
Program Flow.
Using Decision Structures
Programming Fundamentals (750113) Ch1. Problem Solving
Presentation transcript:

Control Structures: Conditionals, If/Else and Loops David Millard

Overview Recap ▫Algorithms ▫Modularisation ▫Building Good Solutions Conditions and Logic If / Else structures Loop structures

Recap: Algorithms Algorithms are: ▫A Sequence ▫… of finite steps ▫… to solve a problem They can be characterised in a number of different ways (performance, efficiency, reusability, etc.) Complexity is a measure of how long an algorithm will take (or how many resources it might use)

Recap: Modularisation Pseudocode ▫High level description of algorithm… ▫… intended for human reading… ▫… but structured like a programming language Modules (subroutines/ functions/ methods) ▫Break down bigger algorithms into chunks ▫Improves Clarity and Reuse Variables and Parameters ▫Are named things with a value (like in algebra) ▫Can make algorithms more flexible ▫Can also improve Reuse

Recap: Building Good Solutions From Problem to Solution ▫Identifying Modules Noun Verb Parsing ▫Identifying Noun Verb Phrases ▫Looking for Synonyms ▫Identify Holes (Missing Phrases) Stepwise Refinement – Revise for: ▫Cohesion ▫Coupling ▫Generalisation

Control Flow About making your algorithm behave differently according to conditions ▫Branching ▫Repetition Useful to adapt to different contexts… … and to deal with problems and errors

Definitions Control Flow (n) “In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements… of a… program are executed or evaluated. A control flow statement is a statement whose execution results in a decision being made as to which of two or more control flows should be followed” Wikipedia "(Or "flow of control") The sequence of execution of instructions in a program. This is determined at run time by the input data and by the control structures (e.g. "if" statements) used in the program.” Dictionary.com “Control flow statements… break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.” Java Tutorial

Definitions Control Flow (n) “In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements… of a… program are executed or evaluated. A control flow statement is a statement whose execution results in a decision being made as to which of two or more control flows should be followed” Wikipedia "(Or "flow of control") The sequence of execution of instructions in a program. This is determined at run time by the input data and by the control structures (e.g. "if" statements) used in the program.” Dictionary.com “Control flow statements… break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.” Java Tutorial

Definitions Control Flow (n) “In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements… of a… program are executed or evaluated. A control flow statement is a statement whose execution results in a decision being made as to which of two or more control flows should be followed” Wikipedia "(Or "flow of control") The sequence of execution of instructions in a program. This is determined at run time by the input data and by the control structures (e.g. "if" statements) used in the program.” Dictionary.com “Control flow statements… break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.” Java Tutorial

Definitions Control Flow (n) “In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements… of a… program are executed or evaluated. A control flow statement is a statement whose execution results in a decision being made as to which of two or more control flows should be followed” Wikipedia "(Or "flow of control") The sequence of execution of instructions in a program. This is determined at run time by the input data and by the control structures (e.g. "if" statements) used in the program.” Dictionary.com “Control flow statements… break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.” Java Tutorial

Conditions, Comparison and Logic Comparison ▫Greater than (>) ▫… What else? Logic ▫AND ▫…

Conditions, Comparison and Logic Comparison ▫Greater than (>) ▫Less than (<) ▫Equal to (==) ▫Not Equal to (!=) ▫Greater or equal to (>=) ▫Less than or equal to (<=) Logic ▫AND ▫OR ▫XOR ▫NOT

Given that the sky is blue, there are lots of birds and not many clouds, which of these is true? ▫Sky == Blue AND Birds > Clouds ▫Clouds == Birds OR Sky == Green ▫Sky != Blue OR Clouds <= Clouds ▫Birds < Clouds OR NOT( Sky == Pink ) ▫Sky != Yellow XOR Clouds <= Birds Conditions, Comparison and Logic = = = = =

If / Else Statements Manage branching in your algorithm Do different things according to conditions Example: ▫“If we have a electric kettle then fill it up, plug it in and switch it on. However, if we have a stove kettle then fill it up, put it on the hob and switch the hob on.”

If / Else Statements Branching ▫Use conditions to implement choices If (Sky == Blue) then go outside and play EndIf

If / Else Statements Branching ▫Use conditions to implement choices ▫Could be alternatives If (Sky == Blue) then go outside and play Else stay in and watch TV EndIf

If / Else Statements Branching ▫Use conditions to implement choices ▫Could be alternatives ▫Can be multiple statements If (Sky == Blue) then go outside and play Else drink coffee stay in and watch TV EndIf

If / Else Statements Branching ▫Use conditions to implement choices ▫Could be alternatives ▫Can be multiple statements Can string together for many alternatives If (Sky == Blue) then go outside and play Else If (Sky == Black) then go to bed Else drink coffee stay in and watch TV EndIf

If / Else Statements Branching ▫Use conditions to implement choices ▫Could be alternatives ▫Can be multiple statements Can string together for many alternatives This nesting can be more complex – creating multiple alternative branches If (Sky == Blue) then If (Birds > 0) then go bird watching Else go outside and play EndIf Else If (Sky == Black) then go to bed Else drink coffee stay in and watch TV EndIf

Loop Structures Manage repetition in your algorithm Repeat or not depending on conditions Example: ▫“While the tea is not sweet enough, add a lump of sugar to the cup and stir.”

Loop Structures While loops ▫Go around zero or more times while (birds > 0) take a photo flap arms End while

Loop Structures While loops ▫Go around zero or more times Do while loops ▫Go around 1 or more times while (birds > 0) take a photo flap arms End while Do blow duck whistle While (birds == 0) Take Photo

Loop Structures While loops ▫Go around zero or more times Do while loops ▫Go around 1 or more times For loops ▫Go around n times while (birds > 0) take a photo flap arms End while Do blow duck whistle While (birds == 0) Take Photo For (n = 1 to 10) take photo of sky End for

Nested Loops Loops can be nested like if/else statements Different loop types can be nested within one another Loops can nested within other nested loops For (n = 1 to 10) Do blow duck whistle While (birds == 0) While (birds > 0) take a photo flap arms End while End for

Nested Loops Loops can be nested like if/else statements Different loop types can be nested within one another Loops can nested within other nested loops ▫And so can if/else statements! For (n = 1 to 10) Do blow duck whistle While (birds == 0) While (birds > 0) If (have camera) take a photo Else watch for a moment EndIf flap arms End while End for

‘While’ != ‘in parallel’ In English we sometimes use while to mean at the same time ▫While Alice wrapped the presents, Bob wrote out the labels In Algorithms we use while to mean do something while a condition is true ▫While there are gifts to wrap, Alice wrap a present, Bob write the label It is common to get these two uses confused, if in doubt remember that the while must contain a condition that evaluates to either true or false

Break

Exercise “A cinema has an automatic system that sends out news and offers to repeat customers. It goes through the list of customers and checks how many visits they have made in the last few months. They get a 20% voucher if they have visited more than 5 times, or a 40% voucher if they have visited more than 10 times. All customers get a copy of the news, but only customers who are 18 or older get a voucher. The news and vouchers are sent out by , SMS or standard post depending on the information available for that customer.” Task: Write the algorithm that behaves as described in the text above. Try and write neat Pseudocode. Assume that the cinema can invoke the algorithm at an appropriate time, and that the algorithm takes the number of months as a parameter.

Summary Control Flow: ▫The conditional ▫… sequence of execution ▫… of statements Conditions and Logic ▫Comparison: such as equals (==) and greater than (>) ▫Logic: such as AND, OR and NOT ▫Can be combined into powerful statements Using If/Else and Loop Structures ▫If/Else and Loops can be nested ▫Three types of loops  While, Do… while, and For