STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.

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.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
 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.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 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
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 3 Getting Started with C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
 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:
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
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.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
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.
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
C++ First Steps.
© 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
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
Completing the Problem-Solving Process
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
Beginning C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 – Getting Started
Chapter 2 - Introduction to C Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Introduction to C++ Programming
Chapter 2: Introduction to C++.
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:

STRUCTURED PROGRAMMING Complete C++ Program

Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement  String literals  C++ keywords  General C++ syntax rules

Objectives 3  By the end you should be able to:  Identify the main function, Preprocessor directives, and user comments in a C++ program  Recognize some of the C++ keywords  Apply C++ syntax rules in producing output messages using cout stream and iostream library  Use Escape characters properly in a cout statement

Complete Program 4 commentsstatement main function preprocessor directive braces string literal output on screen

C++ Program Structure  Comments: remarks that are ignored by the compiler  Compiler directives: commands for compiler, which are needed to compile and run program effectively  Main function: where every program begins  Braces: mark the beginning and ending code blocks  Statement: a line of C++ code 5

Comments  Explain programs, their purpose, authors names and future modification notes to other programmers  Improve program readability  Ignored by compiler  Single-line comment  Begin with //  Multi-line comment  Start with /*  End with */ 6 // Fig. 2.1: fig02_01.cpp // Text-printing program /* Fig. 2.1: fig02_01.cpp Text-printing program */ // Fig. 2.1: fig02_01.cpp // Text-printing program /* Fig. 2.1: fig02_01.cpp Text-printing program */

Preprocessor Directives  Instructions to the compiler rather than part of C++ language  Processed by preprocessor before compilation  Tells preprocessor to include the input/output stream header file  Begin with #  It tells the compiler to put code from iostream into the program before actually creating the executable  Forgetting to include the file will result in a compilation error 7 #include name of header file

main Function  Block of codes carries out a specific task  Part of every C++ program  ONLY one function in a program must be main  Can or can’t be “return” a value  Returns an integer; once return  Body is delimited by braces { }  return statement  The value 0 indicates the program terminated successfully 8 int main() { return 0; } int main() { return 0; }

Using cout  Namespace  std:: Specifies using a name that belongs to “namespace” std Can be removed through use of using statements  Standard output stream object  std::cout “Connected” to screen Defined in input/output stream header file 9

Using cout (cont.)  Stream insertion operator <<  Value to right (right operand) inserted into left operand  Example std::cout << "Hello"; Inserts the string "Hello" into the standard output then displays to the screen  Escape characters  A character preceded by "\" Indicates “special” character output  Example "\n“  Cursor moves to beginning of next line on the screen 10

Escape Sequences 11

Modifying First Program Multiple stream insertion statements produce one line of output

Modifying First Program Use newline characters to print on multiple lines

Syntax Rules 14  Must include for cout to work properly  You must use the namespace std for cout to work properly  C++ is case sensitive  Make sure you don’t capitalize any of the letters in C++ keywords  Every statement ends with a statement terminator ( ; )  except for function header, function braces and preprocessor directives  String literals must be enclosed in “ ”  Main function must return a value to the OS  Every opening brace { must have an enclosing brace }

Exercise Write a program that display your name, ID, and section in our course. Then, save the source code file as yourName.cpp

Exercise Write a program that print the message of your choice to the screen. Make the message at least four lines long. Save the source file as myMessage.cpp

Included Sections 17 Chapter 2: from section 1 to 3