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

Slides:



Advertisements
Similar presentations
Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 2: Introduction to C++.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
High-Level Programming Languages: C++
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Instructor - C. BoyleFall Semester
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming: Basic Elements of C++.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Fundamental Programming: Fundamental Programming Introduction to C++
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Completing the Problem-Solving Process
Programming Fundamental
Basic Elements of C++.
The Selection Structure
Basic Elements of C++ Chapter 2.
Compound Assignment Operators in C++
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Chapter 3: Input/Output
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Review of C++ Language Basics
Presentation transcript:

Programming Funamental slides1 Control Structures Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing Sequencing in C++ language

Programming Funamental slides2 Control Structures In your algorithm (or program), you can use different types of statements. There are 3 categories of control structures: 1- Sequencing 2- Selection 3- Repetition

Programming Funamental slides3 1- Sequencing 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

Programming Funamental slides4 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).

Programming Funamental slides5 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).

Programming Funamental slides6 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.

Programming Funamental slides7 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).

Programming Funamental slides8 OUPUT Statement (in pseudo code).. cont. 2- OUTPUT message where message may by any string of characters enclosed with double quotas. e.g. OUTPUT “Enter 3 values”

Programming Funamental slides9 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.

Programming Funamental slides10 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.

Programming Funamental slides11 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)

Programming Funamental slides12 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.

Programming Funamental slides13 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

Programming Funamental slides14 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

Programming Funamental slides15 Examples on Simple Algorithms Example 1 Write an algorithm to Compute and print the summation of two numbers. 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).

Programming Funamental slides16 Example1.. cont. 1- Analysis stage: Problem Input: - num1 - num2 Problem Output: - summation of two numbers Formula: sum=num1+num2

Programming Funamental slides17 Example1.. cont. 2- Algorithm Design We write the algorithm by using the pseudo code ALGORITHM Summation INPUT num1, num2 sum  num1+ num2 OUTPUT “sum=“,sum END Summation

Programming Funamental slides18 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. num1 num2 sum The output: sum= 63

Programming Funamental slides19 Flow Chart Input num1,num2 Sum = num1+ num2 Output sum

Programming Funamental slides20 Examples on Simple Algorithms Example 2 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).

Programming Funamental slides21 Example2.. 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

Programming Funamental slides22 Example2.. cont. 2- Algorithm Design We write the algorithm by using the pseudo code ALGORITHM apples INPUT quantity, cost total_cost  quantity * cost OUTPUT “Total cost = “, total_cost END apples

Programming Funamental slides23 Example2.. 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. quantity cost total_cost The output: Total cost = 2.7

Programming Funamental slides24 Flow Chart INPUT quantity, cost total_cost  quantity * cost OUTPUT “Total cost

Programming Funamental slides25 Example 3 The problem statement: Write an algorithm that Compute and print the average of three numbers 1- Analysis stage: Problem Input: - n1,n2,n3 Problem Output: - average Formula: sum= n1 + n2 + n3 average = sum / 3

Programming Funamental slides26 Example 3.. cont. 2- Algorithm Design ALGORITHM Avg INPUT n1, n2, n3 sum  n1 + n2 + n3 average  sum / 3 OUTPUT average END Avg

Programming Funamental slides27 Example 3.. cont. 3- Testing the algorithm n1 n2 n3 sum average The output: average=

Programming Funamental slides28 Flow Chart Input n1,n2,n3 sum = n1+n2+n3 Average = sum / 3 Output average

Programming Funamental slides29 Coding (Writing a Program) After testing your algorithm, you can code it in any programming language. In our lab, we are going to use C++ language.

Programming Funamental slides30 C++ Language Elements The general form of a C++ program // File: filename // Program description # include compiler directives void main ( ) //main function of the program { declarations section executable statement section }

Programming Funamental slides31 1- Comments in Programs In C++, the two symbols // are used to denote a program comment. If comments need more than one line, then you can use the symbols /* to begin comment and */ to end it.

Programming Funamental slides32 2- The include compiler directive The line begins with #include represents a compiler directive. A compiler directive is processed at compilation time. C++ syntax: # include

Programming Funamental slides33 2- The include compiler directive e.g. #include iostream.h is the name of a C++ library header file whose contents are inserted in place of the #include line during compilation. iostream.h is used to manipulate input/output operations The standard I/O stream objects, cin and cout are already defined in iostream.h

Programming Funamental slides34 3- Declaration Section The declaration section tells the compiler what data are needed in the program. Declarations are based on the problem data requirements identified during the problem analysis. All identifiers must be declared before they are used. Every identifier associated with a problem data element must be declared only once in the declaration section.

Programming Funamental slides35 3- Declaration Section Syntax: List-of-identifiers where, type is any predefined type in C++, and List-of-identifiers is a list that contains one or more identifiers. 1- Declaring identifiers of integer type : int x ; int x, y; 2- Declaring identifiers of character type: char c1, c2 ; 3- Declaring identifiers to hold real numbers: float sum ; double total ;

Programming Funamental slides36 4- Executable Statements Section Execution statements cause some kind of action to occur when a program is executed. We have seen three executable statements in the algorithmic language: INPUT, OUTPUT, and assignment statements. 1- The Input statement in C++ Syntax: cin >> identifier >> identifier ; This will cause any data value, typed by the user from the standard input devise (usually the keyboard), to be read into the identifiers mentioned in the statement. e.g.cin >> length ; cin >> x >> y ;

Programming Funamental slides37 2- The Output Statement in C++ 1- Syntax:cout << identifier << identifier ; This statement causes the values of the identifiers to be displayed to the standard output device (e.g. a screen). e.g. cout << length ; 2- Syntax:cout << “any message” ; This statement causes the message between quotation to be displayed to the standard output device. e.g.cout << “ Enter three integer values “ ; 3- Syntax:cout << endl ; This statement causes a new line to be displayed in the output. Use this for output clarity. Note: You can mix between all type of cout.

Programming Funamental slides38 3- The Assignment Statement in C++ Syntax: variable = Expression ; This statement causes the value of Expression to be assigned into the variable on the LHS of the assignment statement. e.g. x = y ; z = t * 2 ; w = 6 ; w = w + 1 ; NOTE: Expression has the same operations with the same precedence rules as with the algorithmic language with some different symbols.

Programming Funamental slides39 3- The Assignment Statement in C++ Operator Precedence OperatorDescription !, +, - not, unary plus, unary minus *, /, % multiplication, division, mod +. - binary plus, binary minus, >= less than, less or equal, greater than, greater or equal ==, != equal, not equal && and || or = assignment

Programming Funamental slides40 Examples on C++ Programs Now, we will code the previous examples given in the algorithmic language into C++ language. In the lab, we will use Visual C++ environment.

Programming Funamental slides41 Examples on C++ Programs Example 1: (refer to the algorithm of Example 1 on slide number 17) /*This program Compute and print the summation of two numbers */ #include void main() { int num1, num2, sum; cout >num1>>num2; sum = num1 + num2; cout<<"sum="<<sum<<endl; }

Programming Funamental slides42 Examples on C++ Programs Example 2: (refer to the algorithm of Example 1 on slide number 17) // File: apples.cpp /*This program calculates the price of purchasing some kilos of apples */ #include void main ( ) { int quantity ; float cost, total_cost ; cin >> quantity >> cost ; total_cost = quantity * cost ; cout << “Total cost = “ << total_cost << endl ; }

Programming Funamental slides43 Examples on C++ Programs Example 3: (refer to the algorithm of Example 1 on slide number 17) /*This program Compute and print the average of three numbers */ #include void main() { int n1, n2, n3; float s, average; cout >n1>>n2>>n3; s = n1 + n2 + n3; average = s / 3; cout<<"\n Average = \t"<<average<<endl; }