High-Level Programming Languages: C++

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Introduction to Programming CS 115 Introduction to Computing PART I : Computer Basics PART II: Introduction to Computing/Programming.
Advertisements

Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
True or false A variable of type char can hold the value 301. ( F )
COSC 120 Computer Programming
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
Three types of computer languages
High-Level Programming Languages
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 8 High-Level Programming Languages Nell Dale John Lewis.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Guide To UNIX Using Linux Third Edition
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 8 Introduction to High-Level Language Programming.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
COMPUTER SCIENCE I C++ INTRODUCTION
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
Chapter 8 High-Level Programming Languages. 2 Compilers High-level language A language that provides a richer (more English like) set of instructions.
CIS Computer Programming Logic
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
C++ Programming: Basic Elements of C++.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 8 High-Level Programming Languages. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
CPS120: Introduction to Computer Science Variables and Constants.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Software Engineering Algorithms, Compilers, & Lifecycle.
CPS120 Introduction to Computer Science High Level Language: Paradigms.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1: Introduction to computers and C++ Programming
Completing the Problem-Solving Process
Basic Elements of C++.
Engineering Problem Solving with C++, Etter/Ingber
Basic Elements of C++ Chapter 2.
Chapter 8: Introduction to High-Level Language Programming
Programming Funamental slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
High Level Programming Languages
Low Level Programming Languages
Programs written in C and C++ can run on many different computers
Chapter 8 Introduction to High-Level Language Programming.
Presentation transcript:

High-Level Programming Languages: C++ Chapter 9 High-Level Programming Languages: C++

Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation, and execution Name four distinct programming paradigms and name a language characteristic of each Define the concepts of a data type and strong typing Describe the structure of a C++ program

Chapter Goals Construct valid numeric and string expressions Construct input and output expressions Implement selection statements in C++ Implement loop structures in C++ Implement void and value-return functions Define subprogram statements and their use Use of nested logic

Compilers High-level languages provide a richer set of instructions that makes the programmer’s life even easier C++, Java, Lisp, Prolog are high-level languages Compiler A program that translates a high-level language program into machine code C++ is a compiled language

Compilers Figure 8.1 Compilation process

Interpreters Interpreter An interpreter translates a statement and then immediately executes the statement Lisp and Prolog are interpreted languages Java is first compiled in an intermediate language (called Bytecode) and the Bytecode is interpreted.

Java Introduced in 1996 and swept the computing community by storm Java is compiled into a standard machine language called Bytecode A software interpreter called the JVM (Java Virtual Machine) takes the Bytecode program and executes it Portability was of primary importance

C++ Portability Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode

Java Portability Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode

Programming Language Paradigms What is a paradigm? A set of assumptions, concepts, values, and practices that constitute a way of viewing reality

Programming Language Paradigms Imperative or procedural model FORTRAN, COBOL, BASIC, C, Pascal, Ada Functional model LISP, Scheme (a derivative of LISP), and ML

Programming Language Paradigms Logic programming PROLOG Object-oriented paradigm SIMULA and Smalltalk C++ is as an imperative language with some object-oriented features Java is an object-oriented language with some imperative features

A simple C++ program

A C++ program template

Introduction to C++ Some components of a C++ template program Comments Give information to human readers of code Include directive The linker includes object code from a library Using directive Tells compiler to look in a namespace for definitions not mentioned in the program

Virtual Data Storage Identifiers: names in a programming language Keywords: have special meanings in C++ C++: case-sensitive, free-format language Data items can be constants or variables

Some C++ Standard Data Types A declaration of a data item tells Whether the item is a constant or a variable The identifier used to name the item The data type of the item Example of a variable integer declaration int numb;

Other Data Types An array Groups together a collection of memory locations, all storing data of the same type Example: int Hits[12];

Other data types Strings Example: A string is a sequence of characters Example: string s=“Intro to CS”; creates a string s that contains “Intro to CS”

Statement Types Input/output statement Input statement Collects a specific value from the user for a variable within the program Output statement Writes a message or the value of a program variable to the user’s screen or to a file

Control Statements Types of control mechanisms Sequential Instructions are executed in order Selection or Conditional Choice of which instructions to execute next depends on some condition Looping Group of instructions may be executed many times

Input Statements Example Pseudocode C++ cin: input stream Get value for Radius C++ cin >> Radius; cin: input stream Code for extraction operator (>>) and the definition of the cin stream come from the iostream library and std namespace

Output Statements Example Pseudocode C++ cout: output stream Print the value of Circumference C++ cout << Circumference; cout: output stream Code for the insertion operator (<<) and the definition of the cout stream come from the iostream library and std namespace

Input and Output Cin reads from the keyboard and cout writes on the screen Cin skips the whitespace Function get reads one character from an input stream Stores the character read in a variable of type char, the single argument the function takes cin.get(character);

The Assignment Statement General form Pseudocode Set the value of “variable” to “arithmetic expression” C++ variable = expression; Expression on the right is evaluated The result is written into the memory location named on the left

Selection Statements Default mode of execution: sequential Evaluation of a Boolean condition (also called a Boolean expression) Which programming statement to execute next is decided based on the value of the Boolean condition (true or false)

Selection Statements Selection statements if-else statement if (Boolean condition) S1; else S2; if variation of the if-else statement

Loops Looping (iteration) The loop body may be executed repeatedly based on the value of the Boolean condition while statement while (Boolean condition) S1;

Putting the Pieces Together At this point, we can: Perform input and output Assign values to variables Direct the flow of control using conditional statements or looping For a complete program, we need to: Assemble the statements in the correct order Fill in the missing pieces

Simple example in C++ Problem: Write a program that evaluates the arithmetic expression A = B + C – 7. Assign an arbitrary value to B and C inside the program. Print A. This corresponds to the pseudo code instruction: Set A to B plus C minus 7 Print A The C++ program: //simple example #include <iostream> using namespace std; int main() { int B=10; int C=5; int A=B+C-7; cout << “the value of A is “ << A << endl; return 0; } 30

Modification 1 of the simple example in C++ MODIFICATION 1: Now I want to ask the user to provide for me the values of B, and C. Then I want to output the value of A. This corresponds to the pseudo code: Get the value of B and C Print the value of A cin and cout are the commands for getting data in input from the keyboard and printing a data in output on the screen Note that in computer numbers are converted in characters in order to be printed since the screen displays ASCII codes! 31

Simple example in C++: Modification 1 The C++ program: //simple example: Modification 1 #include <iostream> using namespace std; int main() { int B, C; cout << “Insert the value of B: “; cin >> B; cout << “Insert the value of C: “; cin >> C; int A=B+C-7; cout << “the value of A is “ << A << endl; return 0; } 32

Modification 2 of the simple example in C++ MODIFICATION 2: The second change consists in checking if B and C are positive (negative values are not accepted). If B and C are not positive, force them to be positive (i.e. If B receive -5 turn B into +5). Then set A=B+C-7 This corresponds to the pseudo code: If (B < 0) then Set B to B*(-1) If (C < 0) then Set C to C*(-1) Set A to B+C-7 33

Simple example in C++: Modification 2 The new C++ program: //simple example: Modification 2 #include <iostream> using namespace std; int main() { int B, C; cout << “Insert the value of B: “; cin >> B; if (B < 0) B=B*(-1); cout << “Insert the value of C: “; cin >> C; if (C < 0) C=C*(-1); int A=B+C-7; cout << “the value of A is “ << A << endl; return 0; } Use of IF 34

Modification 3 of the simple example in C++ Nested Logic MODIFICATION 3: Repeat the previous program 10 times and each time get new values in input for B and C. This corresponds to the pseudo code: Set count to 0 while (count < 10) execute the program Increment count endwhile IF within a WHILE 35

Simple example in C++: Modification 3 //simple example: Modification 3 #include <iostream> using namespace std; int main() { int B, C; int count=0; while (count <10) cout << “Insert the value of B: “; cin >> B; if (B < 0) B=B*(-1); cout << “Insert the value of C: “; cin >> C; if (C < 0) C=C*(-1); int A=B+C-7; cout << “the value of A is “ << A << endl; count ++; } return 0; The new C++ program: IF within a WHILE 36

Modification 4 of the simple example in C++ MODIFICATION 4: Instead of repeating 10 times I want the user to decide if the execution of the program must be repeated or not. I will ask the user to insert a 0 to stop the program. Any other value will force the program to be re-executed. This corresponds to the pseudo code: Set the answer to “yes”; While (answer is “yes”) repeat the program Ask the user if he/she wants to repeat Get the answer endwhile 37

Simple example in C++: Modification 4 //simple example: Modification 4 #include <iostream> using namespace std; int main() { int B, C; char answer=‘y’; while ((answer == ‘y’) || (answer ==‘Y’)) cout << “Insert the value of B: “; cin >> B; if (B < 0) B=B*(-1); cout << “Insert the value of C: “; cin >> C; if (C < 0) C=C*(-1); int A=B+C-7; cout << “the value of A is “ << A << endl; cout << “Do you want to repeat this program? (y/n) “ << endl; cin >> answer; } return 0; The new C++ program: 38

This Exercise is for you to complete! A new problem in C++ This Exercise is for you to complete! Problem Read in a sequence of non-negative numbers, one number at a time, and compute a running sum. When you encounter the first negative number print out the sum of all the non- negative values received and stop. 39

The pseudocode

Subprogram Statements We can give a section of code a name and use that name as a statement in another part of the program When the name is encountered, the processing in the other part of the program halts while the named code is executed It is implemented by using functions. Remember?

Simple example in C++: Modification 5 //simple example: Modification 5 #include <iostream> using namespace std; int check(int x); // function for subprogram {if (x < 0) x=x*(-1); } int main() { int B, C; char answer=‘y’; while ((answer == ‘y’) || (answer ==‘Y’)) { cout << “Insert the value of B: “; cin >> B; B=check(B); cout << “Insert the value of C: “; cin >> C; C=check(C); int A=B+C-7; cout << “the value of A is “ << A << endl; cout << “Do you want to repeat this program? (y/n) “ << endl; cin >> answer; return 0; Break the problem in smaller parts – use subprograms The new C++ program: Functions 42