CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Programming Funamental slides1 Control Structures Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing Sequencing.
Types and Arithmetic Operators
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 2 C++ Basics. 2 Overview Variables, Constants and Assignments (2.1) Input and Output (2.2) Data Types and Expressions (2.3) Simple Flow of Control.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to C Programming
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Week 1 Algorithmization and Programming Languages.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 C++ Basics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
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.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
Chapter 2 Variables.
Data Types and Expressions
Data Types and Expressions
BASIC ELEMENTS OF A COMPUTER PROGRAM
Assignment and Arithmetic expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Lecture 3 Expressions Richard Gesick.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Chapter 2 Variables.
Chapter # 2 Part 2 Programs And data
Expressions and Assignment
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 Variables.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)

CONTENTS 10/14/2014 Input & output statement Variables, constant & literals Data Types Assignment statement

CONTROL STRUCTURES 10/14/2014 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

SEQUENCING 10/14/2014 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. Example

SEQUENCING (CONT.) 10/14/2014 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 10/14/2014 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) 10/14/2014 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. 10/14/2014 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. 10/14/ 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.) 10/14/2014 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. 10/14/2014 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 10/14/2014 An assignment statement is the mechanism for initializing or changing the value stored in a designated segment of memory during the execution of a program. An assignment statement consists of: - A variable that has been previously declared appearing alone on the left side of the statement. - The assignment operator = - A constant, variable, or mathematical expression that is located to the right of the assignment operator.

ASSIGNMENT STATEMENT 10/14/2014 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 10/14/2014 Assigning an immediate constant to a variable. int x; x = 24; The declaration allocates 4 bytes of memory to the variable x, and assigns the default value 0. The assignment statement changes the value stored in x to 24. Note! Declaration and assignment can be combined in the single statement. int x = 24; An assignment statement is used to move (copy) a value from one memory location to another. int x, y; x = 24; y = x; In the statement y = x; the identifier x appears on the righthand side of the assignment, and the contents of variable x (24) is assigned to y.

ASSIGNMENT STATEMENT (CONT.) 10/14/2014 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.) 10/14/2014 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. 10/14/2014 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

ARITHMETIC 10/14/2014 Arithmetic is performed with operators  + for addition  - for subtraction  * for multiplication  / for division Example: storing a product in the variable total_weight total_weight = one_weight * number_of_bars; Example

RESULTS OF OPERATORS 10/14/2014 Arithmetic operators can be used with any numeric type An operand is a number or variable used by the operator Result of an operator depends on the types of operands  If both operands are int, the result is int  If one or both operands are double, the result is double

DIVISION OF DOUBLES 10/14/2014 Division with at least one operator of type double produces the expected results. double divisor, dividend, quotient; divisor = 3; dividend = 5; quotient = dividend / divisor;  quotient = …  Result is the same if either dividend or divisor is of type int

DIVISION OF INTEGERS 10/14/2014 Be careful with the division operator!  int / int produces an integer result (true for variables or numeric constants) int dividend, divisor, quotient; dividend = 5; divisor = 3; quotient = dividend / divisor;  The value of quotient is 1, not 1.666…  Integer division does not round the result, the fractional part is discarded!

INTEGER REMAINDERS 10/14/2014 % operator gives the remainder from integer division int dividend, divisor, remainder; dividend = 5; divisor = 3; remainder = dividend % divisor; The value of remainder is 2

ARITHMETIC EXPRESSIONS 10/14/2014 Use spacing to make expressions readable  Which is easier to read? x+y*z or x + y * z Precedence rules for operators are the same as used in your algebra classes Use parentheses to alter the order of operations x + y * z ( y is multiplied by z first) (x + y) * z ( x and y are added first)

OPERATOR PRECENDENCE 10/14/2014 All expressions inside of parentheses are evaluated first Multiplication and division from left to right Addition and subtraction from left to right

OPERATOR SHORTHAND 10/14/2014 Some expressions occur so often that C++ contains to shorthand operators for them All arithmetic operators can be used this way  += count = count + 2; becomes count += 2;  *= bonus = bonus * 2; becomes bonus *= 2;  /= time = time / rush_factor; becomes time /= rush_factor;  %= remainder = remainder % (cnt1+ cnt2); becomes remainder %= (cnt1 + cnt2);

References 10/14/ Pearson Education, Inc.

10/14/2014