Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.

Slides:



Advertisements
Similar presentations
Programming Funamental slides1 Control Structures Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing Sequencing.
Advertisements

CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)
Chapter 2: Input, Processing, and Output
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Pseudocode.
Chapter 3 Planning Your Solution
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Adapted from slides by Marie desJardins
DCT 1123 Problem Solving & Algorithms
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Developing a Program.
Programming.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Fundamental Programming: Fundamental Programming Introduction to C++
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Bill Tucker Austin Community College COSC 1315
Chapter 3 Program Design
Topics Designing a Program Input, Processing, and Output
1-1 Logic and Syntax A computer program is a solution to a problem.
Completing the Problem-Solving Process
Chapter 2: Input, Processing, and Output
The Selection Structure
Variables, Expressions, and IO
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Data Types, Identifiers, and Expressions
Compound Assignment Operators in C++
Programming Funamental slides
Programming Funamental slides
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Input, Processing, and Output
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing

Control Structures In your algorithm (or program), you can use different types of statements. Each statement starts with a specific keyword (except for the assignment statement (see later)). There are 3 categories of control structures: 1- Sequencing 2- Selection 3- Repetition

1- Sequencing It is a sequential control given in a compound statement or a block. A compound statement (or a block) is a sequence of statements ordered in a way to give a solution: e.g. S1 S2 S3 is a sequence of 3 statements In C++, the ; symbol is used to separate statements.

1- Sequencing (Cont.) The statements that have a sequential control: 1- INPUT/OUPUT statements 2- Assignment statement INPUT/OUPUT statements INPUT statement (in pseudo code): Use Input statement to input data into variables from the standard input device (e.g. a keyboard).

INPUT Statement Syntax: INPUT List of variables where, List of variables contains one or more variables e.g. INPUT x INPUT a, b The semantics (execution) of this statement: You can enter the values you want for the variables in the statement from the keyboard and the computer will assign these values into the variables (stores them in memory).

OUPUT Statement (in pseudo code) The OUTPUT statement has many uses. Use OUTPUT statement to output - The values of variables stored in memory. - A message (i.e. a string of characters). -The value of an expression.

OUPUT Statement (in pseudo code).. cont. Syntax: 1- OUTPUT List of variables where, List of variables contains one or more variables e.g. OUTPUT x OUTPUT a, b The semantics (execution) of this statement: This statement allows the computer to access the locations of the variables mentioned in the statement and displays their contents on an output device (e.g. a screen).

OUPUT Statement (in pseudo code).. cont. 2- OUTPUT message where message may by any string of characters enclosed with double quotas. Use such statement - for making your output clearer - for helping the user of your program to interact easily with it. e.g. OUTPUT “Enter 3 values”

OUPUT Statement (in pseudo code) (Cont.) The semantics (execution) of this statement: This statement will display the message on the screen. 3- OUTPUT expression where expression is any arithmetic expression e.g. OUTPUT OUTPUT x – y The semantics (execution) of this statement: First, the expression is evaluated, then the result will be displayed on the screen. For the first example, it will display 9.

OUPUT Statement (in pseudo code).. cont. NOTE You can mix between the different types of the OUTPUT statements. e.g. OUTPUT “Length = “, length The semantics (execution) of this statement: This statement will display the message Length = on the screen and on the same line it will display the value of the variable length.

Assignment Statement (in pseudo code) Storing a new value in a memory location is called assignment. We use the operator  as assignment operator. Syntax: Variable  Expression The semantics (execution) of this statement: 1- The Expression on the right of  is evaluated 2- The result of the expression is assigned to the variable on the left of . e.g. X  10 (This means that X has 10 now ) Y  X + 5 (This means that Y has 11 now, if X is 6)

Assignment Statement (Cont.) NOTE: The right hand side (RHS) of the assignment statement should be of the same data type of the left hand side (LHS). e.g. 1- T  true This will be correct if T is of Boolean type. 2- A  x + y * 2 This will be correct if A has a numeric data type (e.g. integer, or real) and the value of the expression on (RHS) has the same numeric data type.

Assignment Statement (Cont.) How to execute a statement like X  X + 1 ? Suppose we have: X  5 Then to execute X  X + 1, we proceed as follows: X X  5 X  X

Assignment Statement.. cont. Dereferencing: If we want to copy a value from one memory location (say, X) into another location (say, Y), we say that we dereference a variable. e.g. X  5 Y  10 X  Y // now X has the value 10 X Y 10 5

Examples on Simple Algorithms Example 1 Write an algorithm to determine the total cost of apples given the number of kilos of apples purchased and the cost per kilo of apples. First, we have to analyze the problem to understand what is the input, output of the problem, and which formula to use to solve the problem (if any).

Example1.. cont. 1- Analysis stage: Problem Input: - Quantity of apples purchased (in kilos) - Cost per kilo of apples (in dinar/fils per kilo) Problem Output: - Total cost of apples (in dinar/fils) Formula: Total cost = Number of kilos of apples × Cost per kilo

Example1.. cont. 2- Algorithm Design We write the algorithm by using the pseudo code keywords (written in bold type). For algorithm writing, please refer to the attachment given with the syllabus that is handed to you, previously. ALGORITHM apples INPUT quantity, cost total_cost  quantity * cost OUTPUT “Total cost = “, total_cost END apples

Example1.. cont. 3- Testing the algorithm We give a sample data to see whether the algorithm solves the problem correctly or not. To give a sample data, proceed as follows: 1- Prepare a table to contain all variables of the algorithm. 2- Give any data you like as input. 3- Calculate any formula in the algorithm using these data. 4- Show the output e.g. quantitycosttotal_cost The output: Total cost = 2.7

Example 2 ** The problem statement: Write an algorithm that converts a given length from feet and inches into centimeters. 1- Analysis stage: Problem Input: - Length (in feet and inches) Problem Output: - Length in centimeters Formula: Length in centimeters = 2.54 × Length in feet & inches

Example 2.. cont. 2- Algorithm Design ALGORITHM Convert INPUT length centimeters  2.54 * length OUTPUT “Length in centimeters= “, centimeters END Convert

Example 2.. cont. 3- Testing the algorithm length centimeters The output: Length in centimeters = length centimeters The output: Length in centimeters = 1.016

Example 3 The problem statement Write an algorithm that calculates the wage of an employee, if the number of hours that he worked and the rate of each hour are given. 1- Analysis stage: Problem Input: - Hours (the employee has worked) - Rate of money per hour Problem Output: - The wage that the employee will receive. Formula: Wage = Hours × Rate of money per hour

Example 3.. cont. 2- Algorithm Design ALGORITHM Pays INPUT hours, rate wage  hours * rate OUTPUT “Employee wage = “, wage END Pays

Example 3.. cont. 3- Testing the algorithm hours rate wage The output Employee wage = hours rate wage The output Employee wage = 85.1

Example 4 The problem statement Suppose that a store makes discount on its items. Design an algorithm that calculates the amount of money that a customer has to pay on purchasing an item adding to it the required tax, where the following data are given: the price of the item before discount, the discount rate, and the tax rate. 1- Analysis stage: Problem Input: - Original Price of the item - Discount rate for the item - Tax rate

Example 4.. cont. Problem Output: - Price of the item after discount Formulae: discount = original price × discount rate price = original price – discount tax = price × tax rate price = price + tax

Example 4.. cont. 2- Algorithm Design ALGORITHM Purchases INPUT price, discount_rate, tax_rate discount  price * discount_rate price  price - discount tax  price * tax_rate price  price + tax OUTPUT “Price = “, price END Purchases

Example 4.. cont. 3- Testing the algorithm price discount_rate tax_rate discount tax The output: Price = 27.84