Csone
From Js
|
csone or Computer Science One; is an introduction to the ideas and concepts of programming a computer.
View the course syllabus here: C++ 386 Syllabus
Computer Science
csone is how you do stuff on a computer. In the beginning (of computing) computers were used to make computations, that is computers are tools to facilitate computation. In more recent times computers are being used as communication tools for things like the internet, facebook, and instant messaging. This course is about how one goes about making the software of computers. That is to say facebook is not m a g i c, it works because somebody (a computer programmer) instructed the device to perform the indicated operations to give end users a desirable experience.
In this course we will use the language C++ to explore these ideas. C++ is not the only language that exists to program computers, it is not the first, and not the last, but it occupies a major space in the software world. Every language has its place; PHP is great for web programming and Java is great for toaster ovens, and C++ is great for everything.
So we will start by learning C++.
Hello
It is typical for a beginning student to learn how to make the computer say hello to the world (and all the boys and girls). To do this we have to know the syntax, the rules, of instructing the computer. For C++ the syntax is like this.
Open a file in a text editor and save it as helloWorld.cpp.
|
Hello World program in C++:
|
|
|
line 1: include additional functionality from an external C++ library file; namely cout to output information to the screen. |
|
Hello a history of hellos.
Compiler
Now that we have a program we have to tell the computer to run it. The code that you type into the helloWorld.cpp file is human readable, that makes it easy to type and understand, but it is not computer readable. So we need to use a compiler to turn the C++ source code into an executable file that the computer can understand and execute.
The compiler that we will use in this text is the gcc - GNU project C and C++ compiler.
If you are using windows ... well I cannot protect you from yourself can I; but here is a version of g++ for windows: g++ for win
Running Your First Program
Type/Copy and paste the helloWorld.cpp program into a text editor.
Save the file as helloWorld.cpp
Open a terminal.
type: g++ -o hello helloWorld.cpp
hit the enter key.
There will be no output to the terminal if the program compiles correctly. You will just be returned to a new command prompt. If you issue an ls command you will see a new file is created called hello.
Type:
./hello
at the command prompt. This will execute the program. You should see an output of:
Hello World>
cout
cout is part of the iostream C++ standard library. It allows the functionality of printing to the screen (the standard output).
cin
cin is also part of the iostream library; it allows us to obtain input from the user from the keyboard.
Write the following program and compile it; it will ask the user to input their name: name.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "\nEnter your name please: \n";
cin >> name;
cout << name << " welcome to C++!\n";
return 0;
} // end of the main function.
The output of name.cpp depends on user input. This leads us to the concept of a variable.
Variables
A variable in a program is space allotted in physical RAM that can change throughout the execution of the program. For instance in the above program name.cpp, the statement string name; defines a variable called name with data type string.
During the course of execution of name.cpp initially name is not given any value, then the statement cin >> name; is encountered. When the user types something in on the keyboard followed by enter it is stored in RAM with the name name.
Data Types
In C++ a variable must be defined with a data type at the time of declaration. This tells the compiler what type of data we expect to store in a variable, i.e. numeric, characters or strings etc.
Fundamental data types in C++ include:
int: integer values negative, zero or positive.
float: integers or decimal numbers like -2.3.
double: same as float but takes more RAM and can hold larger numbers.
char: hold character values like 'a' or 'B'.
Control Structures
Control Structures are a mechanism by which programmers control the execution of a program. Control structures depend on the boolean value of some condition, i.e. is the condition true or false.
In C++ we test conditions with the following operators:
== : Test for equality, i.e. 1 == 2 isfalse!= : Test for inequality, i.e. 1 != 2 evaluates totrue|| : logical or. && : logical and.
if
The if statement is a block of code that executes only if some predetermined condition is met.
if syntax:
if(1 == 1)
{
cout << "This will print since 1 is equal to 1\n";
} // end if block.
if else
The if/else structure is used when the program should do one thing if a condition is true and something else if the condition is false.
if/else syntax:
if(1 == 2)
{
cout << "This will NEVER print, since 1 is not equal to 2\n";
} // end if block.
else
{
cout << "This will print\n";
} // end else block.
switch
while
do while
for loop
A for loop is used when you want to do the same thing a certain number of times. Like if you want to send the same message to 10 students or add up some numbers.
for loop syntax:
for(int i = 0; i < 10; i++)
{
cout << i << endl;
} // end for loop.
This code block will output 10 numbers to the screen:
code output:
0 1 2 . . . 9
Functions
We can now put these tools into packages or chunks of code called functions. A function is a set of code that accepts input and creates some output. In mathematics functions accept numbers and do some calculations and output some numbers. The function:
accepts real numbers and manipulates them by multiplying the input by 2 and subtracting 3 from the result. For example:
The function has a name
a parameter
and a rule
, but this is all notation and the actual work (the computations) are performed by a human being.
In a computer program the computer will perform the work or the tasks for us. It is the programmers' (your) job to write the code to perform the operations. So far all of our programs have contained exactly one function; the main function, and all of our code/instructions have been contained within it. We will now create our own function that we can call as many times as we like to do some stuff for us.
Classes
Glossary
CPU - Central Processing Unit
RAM - Random Access Memory
ROM - Read Only Memory
