Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 201.

Similar presentations


Presentation on theme: "CMPT 201."— Presentation transcript:

1 CMPT 201

2

3 Include Directives #include <iostream> using namespace std;
Preprocessor directive iostream is a library containing definitions of the input and output function Linker Appears at the start of the program, begin with # using namespace std; Tells the compiler to use names in iostream in a “standard” way

4 int main() { //beginning of the main function .... //statements return 0; } //end of the program

5 The output function cout
c(C++ language)out(output) c(C++ language)in(input) cout<<whatever data you want to output; cout<<“Welcome to CMPT201!”; << represents the flow of the data.

6 Okay, let’s write a hello world program in C++
C++ is fun!

7 Note No extra space between < and iostream, or between the end of the file name and > #include<iostream > #include< iostream> #include<iostream> Old C++ compilers: iostream.h Statements end with a semi-colon.

8 Program Layout Compiler accepts almost any pattern of line breaks and indentation Programmers format programs so they are easy to read Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves Indent statements Use only one statement per line

9 .cpp file .exe file

10 More about cout <<: insertion operator
cout<<“Hello World”; is equivalent to cout<<“Hello”<<“ World”; cout<<“Hello ”; cout<<“World”;

11 About cout Start a new line Escape sequence
\n (need to go inside of the quotes) endl Escape sequence \n new line \t tab \” ” \\ \

12 Comments // This is a comment. /* This is a comment.
This is another comment. */

13 Exercise Write a simple program to print to the screen the following. Be sure to add your name in the comment. Hello! I say “C++ is fun!” Good-bye!

14 Variables a place to hold a number or data of other types
Variable names Start with a letter or the underscore Remaining can be letters, numbers or underscore x x1 3X _abc %change data-1 PROG.CPP Big_Bonus C++ is case-sensitive rate RATE Rate

15 Variable Names Variables are often spelled with all lowercase letters.
Avoid keywords/reserved words int, double cin, cout Choose meaningful names! x, y, z distance, speed, time

16 Declare a Variable Syntax Example Where? type name;
type name1, name2, ... ; Example int number; double weight, height; Where? before the variable is used at the start of the main function (preferred)

17 Assign Value to a Variable
Syntax variable = value(or expression); Example a = 2; b = a; c = a + b; count = count + 1 is totally wrong in math, but in C++?

18 Combine Declaration and Assignment
int number = 3; double rate = 0.07, balance = 0;

19 Example here cout<<number; cout<<“number”;

20 Input Function cin cout: output to screen cin: input from keyboard
cout<<whatever you want to output; cin>>variable_name; cin>>variable1>>variable2; Or cin>>variable1; cin>>variable2;

21 Example here Give the user a prompt before the input.

22 Exercise Write a program that reads in two integers and then outputs both their sum and their product.

23 Debug Bug: a mistake in a program Debugging: Eliminating the mistake

24 Errors Syntax errors Run-time errors Logic errors
The compiler may be wrong about the location/error! Start from the first error Warning message Run-time errors Logic errors hardest to diagnose

25 Modify the previous program so that it also outputs their quotient
Modify the previous program so that it also outputs their quotient. Recompile the program.

26 Test It is important to test your program on several data sets!

27 Homework 1 Due next Friday before class Hard copy Source code
Screen shot of test runs


Download ppt "CMPT 201."

Similar presentations


Ads by Google