Karel review and a more complex example
Abstractions in Karel's world:
- Instructions:
move()
, pickBeeper()
, putBeeper()
, turnLeft()
- Conditions:
frontIsClear()
, beeperPresent
- Variables, variable assignment and use
- if-then-else, if-then
- for-loop for counting
- while-loop for looping until a condition becomes false
Example: moveTillClear()
Example: clearAllTowers()
use another abstraction, clearOneTower
to implement this function. Can use either top-down or bottom-up design approach.
We will use C++ examples, why C++?
- C++ is quite a popular language. Provides a lot of choice to the programmer to choose between safe-programming and fast-programming. This is a tradeoff you may appreciate as you go through more CS courses: you can either be very safe (e.g., riding in a slow train, or walking) or you can be very fast (e.g., riding on a figher jet), but it is often hard to achieve both. But I digress.
- We will restrict ourselves to the safe features of C++. Do not care about speed for now.
- But getting introduced to C++ syntax should help you in future CS courses which also discuss speed.
- Keep in mind: the language is not important, concepts are. The same concepts can be applied to any other programming language!
Introduction to C++
- C++: A programming language developed in 1983 by Bjarne Stroustroup
- one of the world's most widely used languages today
- continues to be improved over time. latest version: C++17.
- C++ syntax has many similarities with Java and C
- similar data types:
int, double, char, void
.
- similar operators:
+, -, *, /, %, keywords
.
- use of
{ }
braces for scope.
- comes equipped with a large standard library for you to use.
C++ programs/files
- C++ source code lives in
.cpp
files
- Additional declarations can be put in "header"
.h
files.
- Source code is compiled into binary object files (
.o
)
- Example:
- file1.cpp--compile->file1.o
- file2.cpp--compile->file2.o
- file1.o file2.o library1 library2 --link-> a.out executable
First C++ program
/*
* hello.cpp
* This program prints a welcome message
* to the user.
*/
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" >> endl;
return 0;
}
- Program comments. Inline comments can be written using
// comment
- Include statements. C++ libraries are written with angle brackets. Local libraries have quotes. e.g.,
#include "lib.h"
- Namespaces. Functions and variables are divided (scoped) by namespace.
Normally would refer to them as namespace::symbol. The "using"
keyword removes the need for the namespace (brings those symbols into
the global program scope).
- Main function -- this is the entry point of the program. Should always return an integer (0 = success). Functions do not need to be part of a class in C++.
Code variables
- A "variable" is like a box that holds a value.
- x = 7
- This stores the value
7
into the variable (i.e., box) x
.
- Later
x
in the code retrieves the value from the box.
x
becomes a shorthand for 7
(or whatever is in the box).
- Using
=
in this way is called "variable assignment".
Example usage
- Assign a value into a variable once.
- Then use that variable many times below.
- A convenient shorthand in the code.
- Many languages allow assigning a value to a variable multiple times (mutable variables).
- Some languages allow assigning a value to a variable a single time (immutable variables).
- C++ supports both mutable and immutable variables. Use the
const
keyword in the "type" of the variable to indicate that it is immutable. For our discussion in the class, we will ignore immutable variables and restrict ourselves to mutable variables.
Programming with variables
string name = "Virat";
cout << "happy birthday to " << name << endl;
cout << "wish you a long life " << name << endl;
A modern computer has gigabytes of memory, i.e., it has space for billions of these boxes (or variables).
Variable declarations
type name;
Some example types:
int
: integral values, e.g., how many students in the class.
double
: decimal values, e.g., what is the average age of the students in the class.
char
: characters, e.g., 'a', .., 'z', '0', .., '9', '\n', ...
bool
: boolean values true
and false
.
Variable assignments
name = value;
Would be a compiler error if the variable name
has not been declared already. Would also be an error if the variable name
is declared twice (in the same "scope").
Variable declaration and assignment
Can do both declaration and assignment together.
type name = value;