1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
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.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Basic Elements of C++ Chapter 2.
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)
Chapter 3 Getting Started with C++
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Creating your first C++ program
1 C++ Syntax and Semantics, and the Program Development Process.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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:
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Basic Program Construction
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
 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.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
 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.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Introduction to C Topics Compilation Using the gcc Compiler
C++ First Steps.
Programming what is C++
C++ Programming: Presentation 1
Chapter Topics The Basics of a C++ Program Data Types
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 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
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Basic Elements of C++ Chapter 2.
Chapter 2 – Getting Started
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Functions, Part 1 of 3 Topics Using Predefined Functions
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
COMS 261 Computer Science I
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Programming - 1
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; }

2 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; using std::endl; int main () { cout << "Hello world!\n"; return 0; } C++ comments. Everything between the // and the end of line is ignored by the compiler.

3 Commenting guidelines I Every file should have a comment section at the top, containing: The name of the file A brief description of the file's contents The name of the author The date the file was created If the file has been revised, a revision log describing the date of the revision, who did it and what was revised. File header comments are usually "framed"

4 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; } #include is a preprocessor directive. Preprocessing occurs before compilation. This directive instructs the preprocessor to include the file iostream at that point. iostream is a library containing utilities that perform I/O The angle brackets surrounding iostream tell the preprocessor that the requested file is part of the C++ Standard Library.

5 Namespaces A namespace is a collection of name definitions (e.g. class, variable, function names). Grouping names in namespaces and specifying what namespace we're currently using, reduces the probability of errors due to conflicting names. The std namespace contains the names defined in several standard C++ libraries, including iostream. A using directive tells the compiler that we will be using names defined in a particular namespace.

6 Namespaces using namespace std; This tells the compiler that we will be using names defined in the standard namespace. However, std is a very large collection of definitions. It is a better idea to specify exactly which names we will be using: using std::cout This tells the compiler that we will be using the cout name (more on what this is later) which is defined in the std namespace. What's the difference? Think of a namespace as a collection of available tools. using namespace std; is the equivalent to emptying the whole toolbox on the worktable when you only need a hammer. using std::cout; is the equivalent to taking only the hammer out of the toolbox.

7 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; } a using directive instructs the compiler that we'll be using cout, which is defined in the standard namespace.

8 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; } Program execution always begins and ends in the main function. There should be exactly one main function in a C++ program. You may invoke other functions from within main The body of main(). It consists of a sequence of statements (two of them)

9 Indentation It helps make the code readable. Keep in mind: the compiler ignores white space. int main() { int i; for(i=0; i<10; i++) { cout << "1TBS" << endl; } return 0; } int main() { int i; for(i=0; i<10; i++) { cout << "Allman" << endl; } return 0; } Choose ONE style and STICK with it!

10 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main ( ) { cout << "Hello world!\n"; return 0; } The type of the return value of main (an integer) A list of input arguments for main (currently empty)

11 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; } This line prints the message Hello world! on the screen. 'cout <<' is an instruction that says "write what follows to the standard output" (typically the screen). We will talk more about cout itself later.

12 Statement A statement is a unit of executable code. All statements in C++ are terminated by semicolons Note that a preprocessor directive is NOT a statement The body of a function consists of a sequence of statements.

13 Strings "Hello world!\n" is a string (a sequence of characters) Strings are enclosed in double quotes. \n is a special character. It means "new line". cout << "Hello world!\n"; will print out the message Hello world! and then move the cursor to the next line.

14 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; } A return statement is used to specify the value that should be returned by a function (recall that main is supposed to return an integer). A function terminates immediately after a return statement is encountered. 0 means that at this point the program terminates successfully (with no errors)