Basics of Programming
Basics of Programming
Basics of Programming
Fundamentals of Programming in
C++ (CoSc 2031)
BASICS OF PROGRAMMING
Mekonnen K.
(MSc)
Email: mekonnen307@gmail.com
1. Process of compiling and
2
running programs
There are three steps in executing a c++ program: Compiling,
Linking and Running the program.
The c++ programs have to be typed in a compiler.
After typing the program, the file is saved with an extension
.cpp. This is known as source code.
The source code has to be converted to an object code which is
understandable by the machine. This process is known as
compiling the program.
You can compile your program by selecting compile from
compile menu or press Alt+f9. After compiling a file with the
same name as source code file but with extension .obj. is
created.
Cont’d
3
These error are thrown by the compilers and will prevent your
program from running. These errors are most common to
beginners. It is also called as Compile time error or Syntax error.
These errors are easy to debug.
➢ Right after these parentheses we can find the body of the main
function enclosed in braces ({}). What is contained within these
braces is what the function does when it is executed.
➢ Every Left French Brace needs to have a corresponding Right
French Brace.
➢ cout and cin are declared in the iostream standard file within
the std namespace, so that's why we needed to include that
specific file and to declare that we were going to use this specific
namespace earlier in our code.
➢ Cout is an object used for printing data to the screen.
➢ To print a value to the screen, write the word cout, followed by
the insertion operator also called output redirection operator (<<)
and the object to be printed on the screen.
➢ Syntax: Cout<<Object;
➢ The object at the right hand side can be:
• A literal string: “HelloWorld”
• A variable: a place holder in memory
Cont’d
14
➢ E.g.: Cin>>var1>>var2>>var3;
➢ Here three different values will be entered for the three variables.
The input should be separated by a space, tan or newline for each
variable.
➢ Here the values of the three variables will be printed where there
is a “,” (comma) between the first and the second variables and the
“and” word between the second and the third.
Comments on C++ programs
16
Identifiers
The C++ identifier is a name used to identify a variable,
function, class, module, or any other user-defined item. An
identifier starts with a letter A to Z or a to z or an underscore
(_) followed by zero or more letters, underscores, and digits
(0 to 9).
C++ does not allow punctuation characters such as @, $,
and % within identifiers. C++ is a case-sensitive
programming language. Thus, Manpower and manpower are
two different identifiers in C++.
Rules for C++ Identifiers
19
➢ Several other variable types are built into C++. They can be
conveniently classified as integer, floating-point or character
variables.
➢ Floating-point variable types can be expressed as fraction i.e.
they are “real numbers”.
➢ Character variables hold a single byte. They are used to hold 256
different characters and symbols.
➢ The type of variables used in C++ program are described in the
next table, which lists the variable type.
Fundamental Variable types
23
➢ Signed integers are either negative or positive. Unsigned integers are always
positive.
➢ Because both signed and unsigned integers require the same number of bytes,
the largest number (the magnitude) that can be stored in an unsigned integer
is twice as the largest positive number that can be stored in a signed integer.
Declaring Variables.
24
Syntax: DatatypeVariable_Name;
➢ The syntax:
➢ e.g. int a = 0;
Its format is :
➢ #define identifier value
For example:
#define PI 3.14159
#define NEWLINE '\
This defines two new constants: PI and NEWLINE. Once they are
defined, you can use them in the rest of the code as if they were any
other regular constant, for example:
In fact the only thing that the compiler preprocessor does when it
encounters #define directives is to literally replace any occurrence of
their identifier (in the previous example, these were PI and NEWLINE)
by the code to which they have been defined (3.14159 and '\n'
respectively).
defining constants with the const key word
31
➢ E.g. 1: the statement 3+2; returns the value 5 and thus is an expression.
E.g.2: x = a + b;
y = x = a + b;
Operators
34
➢ The assignment operator causes the operand on the left side of the
assignment statement to have its value changed to the value on the right
side of the statement.
➢ Syntax: Operand1=Operand2;
Logical negation (!) is a unary operator, which negates the logical value
of its operand. If its operand is non zero, it produce 0, and if it is 0 it
produce 1.
Logical AND (&&) produces 0 if one or both of its operands evaluate to
0 otherwise it produces 1.
Logical OR (||) produces 0 if both of its operands evaluate to 0
otherwise, it produces 1.
E.g.: !20 //gives 0 which mean false
10 && 5 //gives 1 means true
10 || 5.5 //gives 1 which mean true
10 && 0 // gives 0 which mean false
N.B. In general, any non-zero value can be used to represent the logical true,
whereas only zero represents the logical false.
Increment/Decrement Operators: (++) and (--)
41
E.g.
int a=9;
int b=12;
➢ E.g.
✓ a = = b + c * d is evaluated first because * has a higher
precedence than + and = =.
✓ The result is then added to b because + has a higher precedence
than = = And then == is evaluated.
➢ Precedence rules can be overridden by using brackets. E.g.
rewriting the above expression as: a = = (b + c) * d causes + to
be evaluated before *.
➢ Operators with the same precedence level are evaluated in the
order specified by the column on the table of precedence rule.
E.g. a = b += c the evaluation order is right to left, so the first b
+= c is evaluated followed by a = b.
46