Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create...
-
Upload
chloe-oconnell -
Category
Documents
-
view
215 -
download
1
Transcript of Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create...
![Page 1: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/1.jpg)
Lecture 3 -- 1Computer Science I - Martin Hardwick
The Programming Process
Use an editor to create a program file (source file).
contains the text of the program
Use a compiler to convert the source file into a machine code file (object file).
convert from “English” to binary with machine operations
Use a linker to convert the object file into an executable file.
include other machine code that the program requires
In Visual C++ make sure you create an “empty” project or you will get linker errors
Run the executable file.
Iterate from any point as needed.
Editor
Program.cpp
Compiler
Program.objLibraries
Linker
Program.exe
Run theProgram
Done
success
errors
errors
errors
![Page 2: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/2.jpg)
Lecture 3 -- 2Computer Science I - Martin Hardwick
The First C++ Program
// Hello World Program// Author: Martin Hardwick
#include <iostream>using namespace std;
int main (){
cout << “Hello World!” << endl;
return 0;
}
Anything following // is a comment intended to improve the readability of the program.
#include is used to tell the compiler and linker what library resources the program uses.
the <iostream> resource defines everything you need to display messages on the screen
The line “using namespace std;” says you want to use standard c++ names for things
This program contains one function named “main”.
![Page 3: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/3.jpg)
Lecture 3 -- 3Computer Science I - Martin Hardwick
Defining A C++ Function
A C++ function has the following form:
The name of this function is “main”.
The word “int”, means that this functions returns an integer number. usually 0 to indicate that the program ran correctly. this is what the return statement does at the end of the
function
The braces define the beginning and the end of the function.
The first line of the function is called the function header.
int main ( ){ sequence of statements separated by semicolons . . . return 0;}
![Page 4: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/4.jpg)
Lecture 3 -- 4Computer Science I - Martin Hardwick
Displaying Messages On The Screen
To display a message on the computer screen use the cout statement.
The << operator says to display whatever immediately follows it. the << operator applies only to the first thing following it to display two things, use the << operator twice, once
immediately before each
The word “endl” means to start a new line.
Text strings must always be surrounded by double quotes.
More examples:
cout << “This is a message to display” << endl;
cout << “Hello World!” << endl;
cout << “First part of long message” << “Second part of long message” << endl;
![Page 5: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/5.jpg)
Lecture 3 -- 5Computer Science I - Martin Hardwick
C++ Variables
A computer does not remember numbers from one statement in your program to the next.
you must save any number that you will need later in a word of memory (called a variable in C++)
Each variable that a C++ program uses must be declared. specify a name for the variable specify the type of the variable
– valid types include: int -- one word integerdouble -- two
word floating-point (real)
The variable declarations in a C++ function are usually placed at the beginning of that function by convention.
Examplesdouble radius;int quantity;
![Page 6: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/6.jpg)
Lecture 3 -- 6Computer Science I - Martin Hardwick
Assignment Statement
An assignment statement is used to put a value into a variable. The previous value is replaced
Syntax: <variable> = <expression>;
<variable> is any declared variable in the program <expression> is anything that produces a value of
the appropriate type (more on this later) the number produced by the <expression> is
assigned as the new value for the variable
Examples:
size = 10;width = length - 2;count = count + 1;
![Page 7: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/7.jpg)
Lecture 3 -- 7Computer Science I - Martin Hardwick
The cin Object
Used to assign a value typed on the keyboard to a variable.
The >> operator says to assign the next value typed on the keyboard to the variable appearing immediately after the >> operator.
The user must type a value followed by the Enter Key.
An integer number is converted to a real number for assignment to a double variable.
cin >> x;cin >> y;cin >> z;
equivalent to: cin >> x >> y >> z;
If x, y and z are double variables and the user types:
10.2 -5.2 6
x is assigned 10.2y is assigned -5.2z is assigned 6.0
The user can also type:
10.2-5.2 6
![Page 8: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/8.jpg)
Lecture 3 -- 8Computer Science I - Martin Hardwick
Names In C++
Variable names in C++ are called identifiers.
identifiers are used for many other things as well
Rules for constructing valid identifiers in C++:
can contain letters (upper and lower case), digits, and the underscore ( _ ) character
cannot start with a digit cannot be a reserved word can be at most 256
characters long are case sensitive Must not already be used
Reserved words in C++:
asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast delete else enum explicit extern false
floatfor friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast returnshortsigned sizeof
staticstatic_cast switchtemplate this throw true try typedeftypeidtypenameunionunsignedusingvirtualvoidvolatilewchar_twhileunionunsigned
![Page 9: Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.](https://reader036.fdocuments.us/reader036/viewer/2022082700/5513f6e955034646298b6339/html5/thumbnails/9.jpg)
Lecture 3 -- 9Computer Science I - Martin Hardwick
Putting it all together
Lets write a program to convert dollars to euros Find out current exchange rate Get amount in dollars Apply formula to convert from dollars to euros Display amount in euros
int main () {
}
Quiz 1 Monday after Labor Day (September 14)