Introduction Simplest Program Basic Output: ITP 165 - Fall 2015 Week 1, Lecture 1
Introduction Simplest Program Basic Output: ITP 165 - Fall 2015 Week 1, Lecture 1
Introduction Simplest Program Basic Output: ITP 165 - Fall 2015 Week 1, Lecture 1
WRONG!
It's a 2 player
game
???
???????
There
is a grid of 9 squares
??????????
?????
First
player is Xs and Second player is Os
????????????????????????
When
it's a player's turn, they draw 1 X????
or O
??????????????????????
First
player to get 3 ??????????????
symbols in a line wins.
???????????
(If
you play perfectly you always will draw).
??????????????????????????
Computers Understand
Basic numbers
Basic arithmetic
Basic logic
Thats it!
BUT
They can do billions of such operations every second
Machine Code
Computers actually only understand machine code
Compiler
Behind the scenes converts our C++ code into machine code
Before you start writing one line of code, you should plan out stepby-step what your program must do
A simple problem
Given a deck of cards
You could write flawless code for algorithm #1, but because
algorithm #1 is a terrible algorithm, the program will be terrible
Before you write one line of code, figure out the logical steps you
will need to follow in order to solve the problem
Statement
A statement in C++ is a single command
Basic statements must end with a semicolon.
So in our first example, we have the following statement:
return 0;
Return statement
The return statement says okay the program is finished*
Your program must always end in a return statement
Syntax (eg. the grammar) for return statement:
return 0;
End of
statement
return keyword Return code
0 = OK
1 = Error
Syntax Errors
The syntax of C++ is very rigid; make a mistake and you will get an
error
Visual Studio and Xcode will try to help you find errors
Build Error
If you make a mistake in the program, when you try to run in
Visual Studio it will say:
Comment
A comment is a note you can leave in the program
It is not code that runs
Comment Syntax
Its pretty simple:
Must begin
with:
//
Anything can
go here
#include Directive
C++ has a lot of different things in the language
#include is required to specify what parts of the programming
#include <iostream>
#include keyword
C++ Libraries
There are a lot of different libraries we might want to #include
Required <<
End of
statement
std::cout Chaining
We can chain multiple phrases by adding on additional << prior to
the semicolon
For example, these two std::cout statements would both output
the same thing:
// Outputs "Hello world!"
std::cout << "Hello world!";
// Also outputs "Hello world!"
std::cout << "Hello " << "world!";
Chain this!
std::endl
#include <iostream>
int main()
{
std::cout << "Hello!" << std::endl;
std::cout << "Goodbye!" << std::endl;
return 0;
}