 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
Introduction Kingdom of Saudi Arabia Shaqra University
Three types of computer languages
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Getting Started with C++
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Week 1 Algorithmization and Programming Languages.
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.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC201: Computer Programming
CS-103 COMPUTER PROGRAMMING
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Introduction to Scripting
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Introduction to C++ Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.  Most of the C++ programs you’ll study in this book process information and display results. ◦ This makes the process of development and correction much easier.

In Chapter 1, we discussed the six steps of program development using C++. They were: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute

 Of the six steps, you actually only do one. The other steps are done by the compiler. 1. Edit – is the only one you do. So it becomes important, when you develop code: 1. You first develop an algorithm.

 In its most general sense, an algorithm is any set of detailed instructions which results in a predictable end-state from a known beginning.  Algorithms are only as good as the instructions given, however, and the result will be incorrect if the algorithm is not properly defined.  A common example of an algorithm would be instructions for assembling a model airplane. Given the starting set of a number of marked pieces, one can follow the instructions given to result in a predictable end-state: the completed airplane. Misprints in the instructions, or a failure to properly follow a step will result in a faulty end product.

 Understand the problem.  Develop an algorithm.  Develop the source code using C++.  Review the code, line by line, for errors.  Compile the code.  Edit as necessary, then recompile.

 It is not easy if one is not familiar with the C++ language itself.  Perhaps the best way and one that has proven to be effective is to understand and analyze a program that is already written.

Page 40 in the text

 // indicates that the remainder of each line is a comment. ◦ You insert comments to document your programs and to help other people read and understand them. ◦ Comments are ignored by the C++ compiler and do not cause any machine-language object code to be generated.  A comment beginning with // is called a single-line comment because it terminates at the end of the current line.  You also may use C’s style in which a comment— possibly containing many lines—begins with /* and ends with */. 1// Fig 2.1: fig02_01.cpp

 A preprocessor directive is a message to the C++ preprocessor.  Lines that begin with # are processed by the preprocessor before the program is compiled.  #include notifies the preprocessor to include in the program the contents of the input/output stream header file. ◦ Must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output. 2//Text printing program // Same as line 1 3#include

 You use blank lines, space characters and tab characters (i.e., “tabs”) to make programs easier to read. ◦ Together, these characters are known as white space. ◦ White-space characters are normally ignored by the compiler. 4

 main is a part of every C++ program.  The parentheses after main indicate that main is a program building block called a function.  C++ programs typically consist of one or more functions and classes.  Exactly one function in every program must be named main.  C++ programs begin executing at function main, even if main is not the first function in the program.  The keyword int to the left of main indicates that main “returns” an integer (whole number) value. ◦ A keyword is a word in code that is reserved by C++ for a specific use. ◦ For now, simply include the keyword int to the left of main in each of your programs. 6int main()

 A left brace, {, must begin the body of every function.  A corresponding right brace, }, must end each function’s body.  A statement instructs the computer to perform an action.  A string is sometimes called a character string or a string literal.  We refer to characters between double quotation marks simply as strings. ◦ White-space characters in strings are not ignored by the compiler.  A statement normally ends with a semicolon ( ; ), also known as the statement terminator. ◦ Preprocessor directives (like #include ) do not end with a semicolon. 7{7{

 When a cout statement executes, it sends a stream of characters to the standard output stream object— std::cout —which is normally “connected” to the screen.  The std:: before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include. ◦ The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std. ◦ The names cin (the standard input stream) and cerr (the standard error stream) also belong to namespace std.  The << operator is referred to as the stream insertion operator. ◦ The value to the operator’s right, the right operand, is inserted in the output stream. 8std::cout << “Welcome to C++\n”; // display message

 The characters \n are not printed on the screen.  The backslash ( \ ) is called an escape character. ◦ It indicates that a “special” character is to be output.  When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence.  The escape sequence \n means newline. ◦ Causes the cursor to move to the beginning of the next line on the screen.  When the return statement is used at the end of main the value 0 indicates that the program has terminated successfully.  According to the C++ standard, if program execution reaches the end of main without encountering a return statement, it’s assumed that the program terminated successfully—exactly as when the last statement in main is a return statement with the value 0. 8std::cout << “Welcome to C++\n”; // display message 10 return 0; // indicates program ended successfully 11} // close bracket indicating the end of function main.

Must be enclosed in quotation marks.

 Welcome to C++! can be printed several ways.

 A single statement can print multiple lines by using newline characters.  Each time the \n (newline) escape sequence is encountered in the output stream, the screen cursor is positioned to the beginning of the next line.  To get a blank line in your output, place two newline characters back to back.

 Each program must be syntactically correct.  The necessary preprocessor directives must be included.  There must be a function main().  Internal documentation of the source code is a good thing.  All C++ functions begin and end with a brace {}