CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

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
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
 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.
Introduction to C Programming
Introduction Kingdom of Saudi Arabia Shaqra University
 2007 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
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.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
© 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
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.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
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.
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
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.
 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 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
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.
Bill Tucker Austin Community College COSC 1315
© 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 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
Completing the Problem-Solving Process
Introduction to C Language
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
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to Programming - 1
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Rabab Almobty, Fatimah Alakeel, Noor Alhareqi CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1st semester 1436H

Outline C++ Program structure First Program in C++: Printing a Line of Text Comments Preprocessor Directive Main Output Statement Exit Printing a Single Line of Text with Multiple Statements Printing Multiple Lines of Text with a Single Statement C ++ program compilation

C++ Program Structure #include <iostream> // Preprocessor Commands int main( ) // main function { // Declaration section – Declare needed variables …... // Input section – Enter required data ….. // Processing section – Processing Statements // Output section – Display expected results ….... return 0; }// end main

First Program in C++: Printing a Line of Text // Preprocessor Commands #include <iostream> int main( ) // main function { // Output section std::cout << “Assalamo Alaikom \n";// Display a message return 0; }// end main A:Comments B:Preprocessor directives C:Main D:Output statement E:Exit The output Assalamo Alaikom

A - Comments Comments are used to describe what your code does and aid reading your code. C++ compiler ignores them. Comments are made using //  for single line comment, which comments to the end of the line. or /* */  for multi line comment, everything inside of it is considered a comment . The comment begins after the first /*. It ends just before the first */. Examples: /* This comment begins at this line. This line is included in this comment It ends at this line. */ // This comment starts here and ends at the end of this line

B – Preprocessor Directive #include < iostream > is a directive to the C++ preprocessor. Lines beginning with # are processed by the preprocessor before the program is compiled. #include < iostream > tells the preprocessor to include the contents of the input/output stream header (< iostream >) in the program. This header must be included for any program that outputs data to the screen or inputs data from the key board using C++’s stream input/output.

C – main function It is a part of every C++ program. int main() It is a part of every C++ program. The parentheses after main indicate that main is a program building block called a function. C++ programs contain one or more functions, one of which must be main. Every program in C++ begins executing at the function main. 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. The left brace ,{ , must begin the body of every function . A corresponding right brace ,} , must end each function’s body.

D – Output Statement std::cout<<”Assalamo Alaikom \n”; This instructs the computer to perform an action, namely to print on the screen the string of characters marked by the double quotation marks; ” and ” . The entire line, including std::cout, the<<operator, the string “Assalamo Alaikom \n“ and the semicolon(;), is called a statement. The statement is a specification of an action to be taken by the computer as the program executes. Each statement in C++ needs to be terminated with semicolon(;)

D – Output Statement Preprocessor directives (like #include) do not end with a semicolon. The << operator is referred to as the stream insertion operator. When this program executes, the value to the operator’s right, the right operand, is inserted in the output stream. The right operand’s characters normally print exactly as they appear between the double quotes. However, 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.

D – Output Statement 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 new line. It causes the cursor (i.e., the current screen position indicator) to move to the beginning of the next line on the screen.

D – Output Statement The std:: before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include<iostream>. The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std. We can avoid writing the std:: by writing using namespace std; as shown in following:

Printing a Line of Text // Preprocessor Commands #include <iostream> using namespace std; int main( ) // main function { // Output section cout << “Assalamo Alaikom \n"; // Display a message return 0; }// end main Assalamo Alaikom The output

E – Exit is included at the end of every main function. return 0; is included at the end of every main function. The keyword return is one of several means we’ll use to exit a function. When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully.

Printing a Single Line of Text with Multiple Statements // Preprocessor Commands #include <iostream> using namespace std; int main( ) // main function { // Output section cout << “Assalamo “ cout << “Alaikom \n"; // Display a message return 0; }// end main Assalamo Alaikom The output

Printing Multiple Lines of Text with a Single Statement // Preprocessor Commands #include <iostream> using namespace std; int main( ) // main function { // Output section cout << “Assalamo \n Alaikom \n"; // Display a message return 0; }// end main Assalamo Alaikom The output

C++ Program Compilation

Steps to Write, Compile, and Run a Program Editor Source code or file (.cpp) Compile file Object code or file (.obj) Linker Executable code or file (.exe) Loader Executable code is loaded into memory ready for execution

Reference : C++ How to Program (8th Edition) , By: Deitel& Deitel