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.

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.
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.
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.
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)
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.
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:
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 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
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.
 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 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
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.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
UMBC CMSC 104 – Section 01, Fall 2016
CSCE 206 Structured Programming in C
Programming what is C++
Chapter Topics The Basics of a C++ Program Data Types
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
Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
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
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.
Beginning C++ Programming
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C++ October 2, 2017.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Hello World 2 What does all that mean?.
Chapter 2 – Getting Started
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Capitolo 1 – Introduction C++ Programming
COMS 261 Computer Science I
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
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:

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 time will be determined in today's class.

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

A simple C++ program C++ comments. Everything between the // // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> 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.

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"

A simple C++ program #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 // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main () { cout << "Hello world!\n"; return 0; } The angle brackets surrounding iostream tell the preprocessor that the requested file is part of the C++ Standard Library.

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.

Namespaces using namespace std; using std::cout What's the difference? 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.

A simple C++ program a using directive instructs // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> 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.

A simple C++ program Program execution always begins and ends in the // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> 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)

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!

A simple C++ program The type of the return value of main (an integer) // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> 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)

A simple C++ program // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> 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.

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.

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.

A simple C++ program A return statement is used to specify // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> 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)

A more complex example What do we need to know? Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number of values remaining to be entered. A typical input sequence might be 5 100 6 189 3 2 where the 5 indicates that the subsequent five values are to be summed. What do we need to know?

A more complex example What do we need to know? Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number of values remaining to be entered. A typical input sequence might be 5 100 6 189 3 2 where the 5 indicates that the subsequent five values are to be summed. What do we need to know? How to read numbers from the keyboard How to store their values An algorithm to compute their sum How to display a message on the screen