1 8/31/05CS150 Introduction to Computer Science 1 Hello World!

Slides:



Advertisements
Similar presentations
Introduction to Programming in C++ John Galletly.
Advertisements

CSE202: Lecture 1The Ohio State University1 Introduction to C++
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.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 2: Your First Program.
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.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
COS120 Software Development Using C++ AUBG Fall semester 2010
Programming is instructing a computer to perform a task for you with the help of a programming language.
Exposure C++ Chapter IV Introduction to C++ Programs.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
Hello World 2 What does all that mean?.
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.
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++
1 CS161 Introduction to Computer Science Topic #3.
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
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
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 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
What Actions Do We Have Part 1
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.
Chapter 2 part #1 C++ Program Structure
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Hello World 2 What does all that mean?.
CS150 Introduction to Computer Science 1
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Life is Full of Alternatives
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Arithmetic Operations
What Actions Do We Have Part 1
Capitolo 1 – Introduction C++ Programming
Reading from and Writing to Files
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

1 8/31/05CS150 Introduction to Computer Science 1 Hello World!

2 8/31/05CS150 Introduction to Computer Science 1 Today  In today’s lecture we will o Write our first C++ program o Analyze the different components of C++ programs

3 8/31/05CS150 Introduction to Computer Science 1 Problem  Programs are written to solve problems  Imagine that you have been asked to solve the following problem o Your summer surveying job requires you to study some maps that give the distance in kilometers and some that use miles. You and your co-workers prefer to deal in metric measurements. Write a program that performs the necessary conversion

4 8/31/05CS150 Introduction to Computer Science 1 Your First C++ Program //*********************************************************** // File name: miles.cpp // Author: Shereen Khoja // Date: 09/01/2004 // Purpose: This program converts distances from miles to // kilometers //*********************************************************** #include #include “stdafx.h” using namespace std; int main() { const double KM_PER_MILE = 1.609; // Conversion rate double miles; // Distance in miles from user double kms; // Distance in kms //Get the distance in miles cout << “Enter the distance in miles” << endl; cin >> miles; //Convert the distance to kilometers kms = KM_PER_MILE * miles; //Display the distance in kilometers cout << “The distance in kilometers is ” << kms << endl; return 0; }

5 8/31/05CS150 Introduction to Computer Science 1 Output of the Program Enter the distance in miles 34 The distance in kilometers is  The line in blue is typed in by the user, everything else is output by the program

6 8/31/05CS150 Introduction to Computer Science 1 Program Components  The C++ program on the previous slide consists of the following elements: o Comments o Preprocessor directives o Standard namespace o main function o Declaration statements o Executable statements

7 8/31/05CS150 Introduction to Computer Science 1 Comments  Comments are o How you explain in English what the different parts of your program do o Ignored by the compiler o Very important  The editor in Visual Studio will colour code your comments. They will be green

8 8/31/05CS150 Introduction to Computer Science 1 Comments  There are two ways to write comments o // I am a comment  Anything after // till the end of the line will be a comment o /* I am another comment */  You must start the comment with /* and end it with */ in this style of comment

9 8/31/05CS150 Introduction to Computer Science 1 Preprocessor directives  #include  # signifies preprocessor directive  Processed before program translation  #include tells the preprocessor to look for libraries  <> signifies part of standard C++ libraries  We’ll see other examples of preprocessor directives later

10 8/31/05CS150 Introduction to Computer Science 1 Preprocessor directives  iostream is the input/output stream library  It is needed to output data to the screen and read in data from the keyboard  #include takes the contents of the library file and places them in the current program

11 8/31/05CS150 Introduction to Computer Science 1 Namespace std  using namespace std;  Indicates that we will be using objects ( cout & cin ) that are named in a region called std  The statement ends in a semicolon  The statement appears in all our programs

12 8/31/05CS150 Introduction to Computer Science 1 Namespace std  You could omit the statement using namespace std; from the top of your program  If you do, then every time you need to use an object from the standard namespace you will need to place std:: before it o std::cout << “Hello World!”;

13 8/31/05CS150 Introduction to Computer Science 1 main Function int main() { // program statements return 0; }  Every program must have a main function  It is where the start of your program execution begins  return 0; ends the main function and indicates that the program terminated successfully  Everything within the double braces {} should be indented

14 8/31/05CS150 Introduction to Computer Science 1 Program Statements  There are two types of statements that you can write inside the main (or any other) function o Declaration statements  Specify the data that is needed by the program o Executable statements  Perform operations  All statements must end with a semicolon;

15 8/31/05CS150 Introduction to Computer Science 1 Program Statements  Declaration statements o const double KM_PER_MILE = 1.609; o double miles; o double kms;  Executable statements o cout << “Enter the distance in miles” << endl; o cin >> miles; o kms = KM_PER_MILE * miles; o cout << “The distance in kilometers is” << kms << endl;

16 8/31/05CS150 Introduction to Computer Science 1 Program Skeleton  All programs in C++ should have the following skeleton //*********************************************************** // File name: filename.cpp // Author: Your Name // Date: 09/01/2004 // Purpose: Description about what the program does //*********************************************************** #include using namespace std; int main() { // declaration statements // executable statements return 0; }

17 8/31/05CS150 Introduction to Computer Science 1 Problem  Write a program that asks the user to enter the radius of a circle and then computes and displays the circle’s area  Write the basic skeleton of this program

18 8/31/05CS150 Introduction to Computer Science 1 Summary  Today we o Wrote our first C++ program o Introduced the basic components of a C++ program  To see the program in action you should test it in Visual Studio.NET.  We covered p from your textbook