CSC 107 – Programming For Science. Announcements Locations of Macs to use outside of lab time ...

28
LECTURE 2: PROGRAMMING BASICS CSC 107 – Programming For Science

Transcript of CSC 107 – Programming For Science. Announcements Locations of Macs to use outside of lab time ...

Page 1: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

LECTURE 2:PROGRAMMING BASICS

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Announcements

Locations of Macs to use outside of lab time Can find on Library ground floor (6) & main

floor (6) Palisano Pavilion Computer Lab in the

basement(?) Get out and meet people; many dorms

have Mac labs

Page 3: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

History of C

Dennis Ritchie developed C from 1969 – 1973

Based upon B (& other) earlier languages Since its creation, language grown

organically Tradition of adding features beyond standard

as desired

Page 4: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

History of C++

Bjarne Stroustrup created to add “objects” Also included many other improvements to

language Name is inside joke: "++" is increment

operator in C Updated for quick growth

ISO standard adopted in 1998 Recently updated to C++ 201x

Page 5: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

C Versus C++

C++ is designed to be as compatible with C as

possible, thereby providing a

smooth transition from C

Page 6: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

C Versus C++

C++

C

Page 7: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

C Versus C++

C

Page 8: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

C Versus C++

Latest definition of C added most C++ features Not classes & objects, these only found in

C++ For this reason, also not a part of CSC 107 Differences now minimal and easily

avoided Once objects removed, C++ just

“looser” C Removes annoying restrictions that had

been in C Since makes life easier, often supported in

C anyway

Page 9: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
Page 10: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Computers are VERY, VERY stupid

Page 11: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Computers have no common-sense They will only do what you tell them to do NOT what you want them to do, which

often differs

While this is true for every computer does Programming highlights exactly how this

happens As you will see, C++ does nothing to

prevent issues

Page 12: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Computers have no common-sense They will only do what you tell them to do NOT what you want them to do, which

often differs

Page 13: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Case-Sensitivity

Example of computers being very literal And language not helping by fixing what you

say

main, Main, & MAiN treated as different words Case of the letters matters, not just the

words Could be different, so C++ won’t change Main to main

Can help prevent easy mistakes from swapping names

With just a little practice, becomes second nature

Page 14: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

“Whitespace”

One (very small) way C++ actually helps you C++ treats whitespace equally – spaces,

enters, & tabs Whether 1 or 1000000000000 – all will be

ignored Cannot use in symbol, whitespace

splits words Treats these as different “: :” and “::”

Spaces between words needed, but not counted Wecansplitwordsbutthecomputercannot

Page 15: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

“Whitespace”

One (very small) way C++ actually helps you C++ treats whitespace equally – spaces,

enters, & tabs Whether 1 or 1000000000000 – all will be

ignored Cannot use in symbol, whitespace

splits words Treats these as different “: :” and “::”

Spaces between words needed, but not counted Wecansplitwordsbutthecomputercannot

Page 16: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Your First C++ Program

#include <iostream>using std::cout;

int main() { /* Hi, Mom. This is a comment

that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}

Page 17: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

#include Statements

#include <iostream>using std::cout;

/* Hi, Mom. This is a comment that goes over 2 line. */int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}

Nearly every C++ file begins with this directive May add more #include to include other

files Contents of included file usable as if it

were here Easy way to copy ideas across multiple

files Programs can use two types of #include

statements Include system file using #include <filename>

#include “filename” includes a file you wrote

Page 18: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Watch Me Pull a Rabbit

#include <iostream>using std::cout;

/* Hi, Mom. This is a comment that goes over 2 line. */int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}

For now, automatically start each file with this line Details are unimportant – consider it

magic

Page 19: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Watch Me Pull a Rabbit

#include <iostream>using std::cout;

/* Hi, Mom. This is a comment that goes over 2 line. */int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}

For now, automatically start each file with this line Details are unimportant – consider it

magic

Page 20: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Your First C++ Program

#include <iostream>using std::cout;

int main() { /* Hi, Mom. This is a comment

that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}

Page 21: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Using Commands

#include <iostream>using std::cout;

int main() { /* Hi, Mom. This is a comment

that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}

More “magic”, but using has less important purpose Tells compiler we are lazy & save some

typing Two types of using statements to choose

from Specify single shortcut with using std::cout

using namespace std; to get all std shortcuts

Unlike #include statements, using never required Do not worry about it – will not be using

them

Page 22: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Your First C++ Program

#include <iostream>using std::cout;

int main() { /* Hi, Mom. This is a comment

that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}

Page 23: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

main Function

#include <iostream>using std::cout;

int main() { /* Hi, Mom. This is a comment

that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end

}

All C++ programs contain function called main Tells computer where to start running

program Code inside the braces will be what is

executed For the moment, consider this more “magic”

Page 24: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

main Function

#include <iostream>using std::cout;

int main() { /* Hi, Mom. This is a comment

that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end

}

All C++ programs contain function called main Tells computer where to start running

program Code inside the braces will be what is

executed For the moment, consider this more “magic”

Page 25: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Comments

Vital for writing and maintaining any program Not required to run program - only for

human eyes Computer simply ignores anything in a

comment Use to describe code in simple English

Sie konnen auch auf Deutsch screiben o U c%d wrte n txt msg

Should be used liberally I add comments where cannot see what

code does Impossible to have too many comments, if

readable

Page 26: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Comments in C++

Double slash comments continue to line’s enda = a – 4; // Hi, Mom!// This entire line is a comment!

/* … */ comments can be on one or more linesa = a - /* Hi, Mom! */ 4;/* This comment takes an entire line. *//* This is a really long comment that * goes on to multiple lines. The stars on * lines 2 and on are optional, but * makes things easier to read. */

Page 27: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

Your Turn

Get in groups & work on following activity

Page 28: CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)

For Next Lecture

Read sections 4.1 – 4.10 in book for Friday What is a data type? What are variables? How can we use variables in a program? How are literal, constant, & variable

different?

Week #1 weekly assignment due Tuesday Problems available on Angel – covered 1st

two already If problem takes more than 10 minutes,

TALK TO ME!