Selection Using IF THEN ELSE CASE Introducing Loops.

Slides:



Advertisements
Similar presentations
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.1 Chapter 5 Loops.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
 Control structures  Algorithm & flowchart  If statements  While statements.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Control Structures: Part 1. Introduction Control Structures If / Then Selection Structure If / Then / Else Selection Structure While Repetition Structure.
Chapter 1 Program Design
ALGORITHMS AND FLOWCHARTS
Adapted from slides by Marie desJardins
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Programming Logic and Design Fifth Edition, Comprehensive
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Overview Go over parts of quiz? Another iteration structure for loop.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
while Repetition Structure
Chapter 5: Control Structure
Ch 7: JavaScript Control Statements I.
ALGORITHMS AND FLOWCHARTS
Chapter 4 Control structures and Loops
Control Structure Senior Lecturer
Control Structure Senior Lecturer
MSIS 655 Advanced Business Applications Programming
ALGORITHMS AND FLOWCHARTS
3 Control Statements:.
Chapter 5 Loops.
Introduction to Problem Solving and Control Statements
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
CHAPTER 4 Iterative Structure.
Flowcharts and Pseudo Code
EPSII 59:006 Spring 2004.
Presentation transcript:

Selection Using IF THEN ELSE CASE Introducing Loops

Making a choice or deciding something. Conditions can be true or false Age=18 (simple condition) Month>=1 AND Month <=12 (complex condition) AND, OR, NOT

Algorithm IF age=18 THEN I can vote ELSE I can ’ t vote END IF IF Month >=1 AND Month<=12 THEN process date ELSE Display error message END IF

Algorithm An Algorithm is a sequence of instructions that can be used to solve a problem.

Grade algorithm using IF – IF pupil’s test mark is greater than or equal to 80 THEN Grade is grade 1 ELSE IF pupil’s test mark is greater than or equal to 60 THEN Grade is grade 2 ELSE IF pupil’s mark is greater than or equal to 40 THEN Grade is grade 3 ELSE IF pupil’s mark is greater than or equal to 20 THEN Grade is grade 4 ELSE IF pupil’s test mark is greater than or equal to 10 THEN Grade is grade 5 ELSE Grade is grade 6 END IF

CASE pupil’s test mark OF WHEN greater than or equal to 80 Grade is grade 1 WHEN greater than or equal to 60 Grade is grade 2 WHEN greater than or equal to 40 Grade is grade 3 WHEN greater than or equal to 20 Grade is grade 4 WHEN greater than or equal to 10 Grade is grade 5 OTHERWISE Grade is grade 6 END CASE

Repetition Unconditional loops Conditional Loops

Unconditional Loops Repeats a set of program statements for a predetermined number of times Controlled by a variable called a loop counter Loop counter counts the number of times the loop structure is to be repeated between the two limits set at the start of the loop

FOR counter equals start number TO finish number do something NEXT counter Eg: Private Sub Command1_Click() Dim word As String word = Txtwordin.Text For Counter = 1 To 5 PicDisplay.Print "The word is "; word Next End Sub

Algorithm to display a name five times Start loop FOR one to five times display a name End loop

Algorithm to display a name a number of times Ask use how many times name is required Take in the number of times name is required Start loop FOR one to number of times Display name End loop

Algorithm showing an unconditional loop with steps Start loop FOR two to thirty times STEP 2 display loop counter End loop

Algorithm showing nested loops Start down loop FOR one to five times start across loop FOR ten to twenty times display a star character end across loop End down loop

Conditional Loops Manages situations where the number of times repetition must take place is not known in advance Calculation can continue until an answer is found More than one exit condition may be used Test at start Test at end

While loop – test at start WHILE condition is true do something END WHILE

REPEAT – Test at end REPEAT do something UNTIL condition is TRUE

Examples Loop REPEAT ask a user to enter a word take in a word UNTIL the word “end” is entered

Loop REPEAT display message “Press Space Bar” take in a character UNTIL the character “ ” is entered

Sum numbers algorithm using a running total Set total to zero Set number to zero Loop REPEAT make total equal to total+number ask user to enter a number take in a number UNTIL number equals –999 Display total

Quiz Question Algorithm Get question Get user’s answer Loop REPEAT ask question take in response UNTIL response equals correct answer

Algorithm with test at start Take in a word Loop WHILE the word is NOT “end” take in a word End loop

Input Validation with test at start Ask user for data input Take in the data Loop WHILE data input is out with range prompt user to re-enter data take in the data End loop

Recursion Recursion, like looping, is a programming technique that causes an operation or set of operations to be repeated. Looping requires the programmer to specify, among other things, how many times to repeat a step and how much space to reserve in memory for the results of each operation – a level of detail that can be inefficient if not impossible to provide. In looking for relationships in the large varying databases characteristic of artificial intelligence programs for example, the programmer often cannot declare all conditions in advance. Recursion lets the programmer focus on finding the solution to the problem rather than the mechanics of implementing the solution. More on this when we look at PROLOG programming language.