Control Structures (B) Topics to cover here: Sequencing in C++ language.

Slides:



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

Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Introduction to C Programming
Chapter 2: Basic Elements of C++
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
 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++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
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 
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++
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
Recap……Last Time [Variables, Data Types and Constants]
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Chapter 3: Input/Output
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
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:

Control Structures (B) Topics to cover here: Sequencing in C++ language

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. First, we have to see the C++ language elements and syntax that correspond to what we have studied in the algorithmic language. Note that most of the syntax used in the pseudo code is similar to that of C++, with miner differences. Then, we convert all previous given algorithms into C++ programs.

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 }

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. Usually, the first line of comment is the one that contains the file name.

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 The #include statement instructs the compiler to insert the indicated C++ instructions (of the filename) into the program in place of the 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 on objects consisting of streams of characters that are external to the program. The standard I/O stream objects, cin and cout are already defined in iostream.h

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. An identifier is associated with each data element. The identifiers in C++ follow the same rules that build identifiers in the algorithmic language. 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.

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 ;

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.

A) 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 ;

B) 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.

C) 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.

Operator Precedence OperatorDescription () brackets !, +, - 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

Examples on C++ Programs Now, we will code the previous examples given in the algorithmic language into C++ language. Try to run the C++ programs in your next lab hour. In the lab, we will use Visual C++ environment.

Examples on C++ Programs Example 1: (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 ; }

Examples on C++ Programs Example 2: (refer to the algorithm of Example 2 on slide number 20) //File: convert.cpp /*This program converts a given length from feet and inche into centimeters */ #include void main ( ) { float length, centimeters ; cin >> length ; centimeters = 2.54 * length ; cout << “ Length in centimeters= “ << centimeters << endl; }

Examples on C++ Programs Example 3: (refer to the algorithm of Example 3 on slide number 23) //File: Pays.cpp /*This program calculates the wage that an employee will receive */ #include void main ( ) { int hours ;//number of worked hours float rate, //rate per hour given to the employee wage ; //wage that the employee will earn cin >> hours >> rate ; wage = hours * rate ; cout << “ Employee wage = “ << wage << endl; }

Examples on C++ Programs Example 4: (refer to the algorithm of Example 4 on slide number 27) //File: Purchases.cpp /*A program calculates the price to pay for purchasing an item including the tax after some discount. */ #include void main ( ) { float price, discount_rate, //original price, discount rate for an item tax_rate, discount, //tax rate on the purchased items and the // amount of discount on purchasing an item tax ; //the calculated tax that a customer will pay cin >> price >> discount_rate >> tax_rate ; discount = price * discount_rate ; price = price - discount ; tax = price * tax_rate ; price = price + tax ; cout << “ Price = “ << price << endl ; }