Week 1 Blog
Week 1 Blog
The end goal is to create something: that could mean anything from a web page, or
a piece of software, or even just a pretty picture. That’s why computer programming
is often described as a mix between art and science; it’s technical and analytical, yet
creative simultaneously.
*debug: detecting bugs and problems within your code that makes it behave unexpectedly.
● It’s fun!
Okay, there’s one more thing you need to know: online judges .
Memory
Every computer has a very fast memory (RAM) which can be accessed by programs.
Whenever you create a program, a portion of the RAM is reserved for your program
to use.
The memory works much like an inventory where each storage unit has a unique
address.
It means:
take the value of 5 and put it in x, But what exactly is x ? It's a variable.
More specifically, it's a place in the memory with a name of x, that holds an integer of
value 5.
Variable types
Variables come in different types and sizes, we'll discuss the integer variable for now.
An integer is a whole number written without its fractional component.
For example: 3, 0, -50 are integers, while 0.6, 1.536 are not.
You can also declare multiple variables of the same type in a single line
int x, y, z;
Now we want to put some value in that variable, this is called Variable Assignment
//Variable_Name = Value;
x = 5;
We can even combine the two processes in one called Variable Initialization
int x;
x = 4;
x is called an L-value (left side of the assignment operator) and 4 is called R-value
(right side of the assignment operator)
It means: put the R-values inside the L-value. And by now we should know that to
put a value in something, it must be a defined variable.
int x, y = 4;
//puts the value of y (4) + 2 inside the variable x
x = y + 2;
//x now equals 6
//puts the value of x(6) + the value of x(6) inside the variable y
y = x + x;
//y now equals 12
Uninitialized variables (Garbage!)
When a variable is declared, it’s given what was previously stored in its memory
location.
It can be something like 5277592 or 4309726. You can never know.
int x;
cout << x; //who knows what we’ll get!
*chars are data types representing characters like ‘a’, ‘Z’ , ‘#’ , ‘@’ or even a white
space ‘ ‘.
But each of these have a number according to the ASCII table ranging from [0,127]
Naming convention
There are some rules and conventions you need to follow when naming your
variables:
1- You can’t use C++ reserved keywords (this, const, friend, case..etc).
2- Your variable names can only start with a letter/ underscore.
int 1stNum; //invalid
3- Your variable names can't contain spaces or special characters.
int my variable name; //invalid
int my@variable#name; //invalid
*But take care, the input stops by a space or a new line, which means:
If for example you have a variable x of type int and you want x to have a value = 123
But then while you enter this value you typed : 12 -> space/enter -> 3
Your input stops at the space/enter , so x now has a value = 12 not 123.
| Cout
The predefined object cout is an instance of ostream class. The cout
object is said to be "connected to" the standard output device, which
usually is the display screen. The cout is used in conjunction with the
stream insertion operator, written as “ << “ which are two less than signs as shown in
the following example.
It is defined in <iostream> header file.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello acmASCIS"<< endl;
}
the insertion operator << may be used more than once in a single statement.
cout << "Hello acmASCIS" << endl;
can be written as
cout << "Hello" << " " << "acmASCIS" << endl;
and endl is used to add a new-line after the last thing written on the console.
Operators
You can use different arithmetic operators with variables
- : Subtraction.
+ : Addition.
* : Multiplication
/ : Division.
% : Modulo operator (finding the remainder after division).
int x = 5;
x = x + 2; //puts the value of x (5) + 2 inside x
//X equals to 7
int y = 4;
y = x * y; //puts the value of x(7)*value of y(4) inside y
// y equals 28
Operators precedence
For example:
int x = 5;
cout << x * 5 + x / 5;
//prints 26
| If Condition
The if keyword is used to execute a statement or block of code, if and only if, a condition is
fulfilled.
if ( TRUE )
{
// Execute all statements inside the braces
}
* in the first example :int test = 5, so think of the if statement that way, is test < 10 ?
Yes, test = 5 < 10, this expression is true, so the piece of code within the braces will be
executed.
Relational Operators:
Logical operators:
We use logical operators to obtain a single relational result out of one or more
expressions.
Example :
}
else // Execute these statements if FALSE
{
}
Let’s consider this example :
int test = 20 ;
if ( test <= 10 )
{
cout << "the condition was true" << endl ;
}
else
{
cout << "the condition was false" << endl ;
}
cout << "done" ;
else if
Another use of “else” is when there are multiple conditional statements that may all evaluate to
true, yet you want only one if-statement's body to execute. You can use an "else if" statement
following an if-statement and its body; that way, if the first statement is true, the "else if" will be
ignored, but if the if-statement is false, it will then check the condition for the “else if” statement.
If the if-statement was true the else-statement will not be checked. It is possible to use many
“else if” statements to ensure that only one block of code is executed.
Example :
if ( testExpression1 )
{
// statements to be executed if testExpression1 is true
}
else if( testExpression2 )
{
// statements to be executed if testExpression1 is false
and testExpression2 is true
}
else if ( testExpression3 )
{
// statements to be executed if testExpression1 and
testExpression2 is false and testExpression3 is true
}
else
{
// statements to be executed if all test expressions are false
}
Example On Comparing using Conditions