Professional Documents
Culture Documents
Chapter 1
Chapter 1
Chapter 1
INSY 2031
Introduction to programming
A program is a set of instructions that tell the computer to do.
Programming is the process of creating a set of instructions that tell a computer how
to perform a task.
In C++, the separation between statements is specified with an ending semicolon (;)
In this case the directive #include <iostream> tells the preprocessor to
This specific file (iostream) includes the declarations of the basic standard
The main function is the point by where all C++ programs start their execution,
independently of its location within the source code.
The word main is followed in the code by a pair of parentheses (()). That is
because it is a function declaration
Right after these parentheses we can find the body of the main function enclosed
in braces ({}).
cout is declared in the iostream standard file within the std namespace
Program comments are totally ignored by the compiler and are only intended
for human readers.
These symbols, the reserved words, must not be used for any other
purposes
Example:- bool, case, break, auto, catch, char, delete, int, void, if, ….,
float etc…
05/11/2024 Compiled by Demeke A. 11
Identifiers
An identifier is name associated with a function or data object and used
to refer to that function or data object. An identifier must:
Start with a letter or underscore
Consist only of letters, the digits 0-9, or the underscore symbol _
Not be a reserved word
A variable is a symbolic name for a memory location in which data can be stored
Variables are used for holding data values so that they can be utilized in various
computations in a program.
A type, which is, established when the variable is defined (e.g., integer, float,
character)
You create or define a variable by stating its type, followed by one or more spaces, followed by
the variable name and a semicolon.
The variable name can be virtually any combination of letters, but cannot contain spaces and
the first character must be a letter or an underscore
Example:-
int myAge;
Case sensitive
05/11/2024 Compiled by Demeke A. 14
Creating More Than One Variable at a Time
You can create more than one variable of the same type in one statement by writing the
type and then the variable names, separated by commas.
For example:
The second line declares three individual long variables named area, width, and length.
However keep in mind that you cannot mix types in one definition statement
You can combine these steps and initialize Width when you define it by writing
int width = 5;
Just as you can define more than one variable at a time, you can initialize more than one
When you define a variable in C++, you must tell the compiler what kind of variable
it is: an integer, a character, or what
Basic (fundamental) data types in C++ can be conveniently divided into numeric and
character types
Numeric variables can further be divided into integer variables and floating-point
variables
Integer variables will hold only integers whereas floating number variables can
accommodate real numbers.
05/11/2024 Compiled by Demeke A. 17
C++ data types and their ranges
C++ is rich in built-in operators and provide the following types of operators
Assignment Operators
Arithmetic Operators
Logical operators
Conditional operator
Bitwise Operators
05/11/2024 Compiled by Demeke A. 25
Assignment Operators (=)
The assignment operator assigns a value to a variable.
a = 5;
This statement assigns the integer value 5 to the variable a
Compound assignment
Modulo is the operation that gives the remainder of a division of two values.
For example, if we write:
the increase operator (++) and the decrease operator (--) increase or
reduce by one the value stored in a variable c++;
c+=1;
c=c+1;
They are equivalent to +=1 and to -=1, respectively
That means that it can be written either before the variable identifier
(++a) or after it (a++)
In Example 1, B is increased before its value is copied to A. While in Example 2, the
value of B is copied to A and then B is increased
The Operator ! is the C++ operator to perform the Boolean operation NOT
producing false if its operand is true and true if its operand is false
The operator && corresponds with Boolean logical operation AND
This operation results true if both its two operands are true, and false otherwise
The operator || corresponds with Boolean logical operation OR
This operation results true if either one of its two operands is true,
thus being false only when both operands are false themselves
a b !a A&&b a || b
True True False True True
True False False False True
False True True False True
false False True False False
If condition is true the expression will return result1, if it is not it will return result2
Example:-
7 == 5 ? 4:3 //returns 3, since 7 is not equal to 5.
7 == 5+2 ? 4:3 //returns 4, since 7 is equal to 5+2.
Bitwise OR ( | ) takes two numbers as operands and does OR on every bit of two numbers.
The result of OR is 1 if any of the two bits is 1.
Bitwise XOR (^ ) takes two numbers as operands and does XOR on every bit of two numbers.
The result of XOR is 1 if the two bits are different.
Left shift (<<) takes two numbers, left shifts the bits of the first operand, the second operand decides the number
of places to shift.
Right shift (>>) takes two numbers, right shifts the bits of the first operand, the second operand decides the
number of places to shift.
#include <iostream>
int main()
{
unsigned int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
we may have some doubts about which operand is evaluated first and
which later
• Write a C++ program that print the area of a circle given diameter=6
• Evaluate
int c = 10 * 5 % 4 * 2 + 3 /2*3%2;
cout<<c<<endl;
Control statements are elements in the source code that control the flow of program
execution.
Flow control in a program is typically sequential, from one statement to the next
• .
C++ control
Statement
Break
While loop
information.
the general form is: if (expression)
statement;
Write a c++ program that display the grade equivalent of your score. Accept your score
from the user using if..else..if statement.
05/11/2024 Compiled by Demeke A. 47
Switch
The switch statement provides a way of choosing between a set of alternatives, based on the
value of an expression.
The switch differs from the if in that switch can only test for equality,
whereas if can evaluate any type of relational or logical expression.
No two case constants in the same switch can have identical values. Of
course, a switch statement enclosed by an outer switch may have case
constants that are the same.
If character constants are used in the switch statement, they are
automatically converted to integers.
05/11/2024 Compiled by Demeke A. 49
05/11/2024 Compiled by Demeke A. 50
Example:-
Write a c++ program that display the name of days using switch statement.
1= Monday
2= Tuesday
3= Wednesday
4=Thursday
5=Friday
6=Saturday
7=Sunday
Write a c++ program that checks whether the character is vowel or consonant
using switch statement.
The while statement (also called while loop) provides a way of repeating a
statement while a condition holds
Syntax:- while(Condition )
condition {
evaluated Statement
}
true false
statement
Its functionality is exactly the same as the while loop, except that condition
in the do-while loop is evaluated after the execution of statement instead of
before
Unlike for and while loops, which test the loop condition at the top of the
loop, the do-while loop checks its condition at the bottom of the loop.
The for statement (also called for loop) is similar to the while statement,
except in its syntax
int num;
for (num = 1 ; num <= 3 ; num ++ )
{
cout << num << “ * IS” << endl;
}
Cout<<“end of the loop”;
num ?
int num;
int num;
int num;
true
int num;
int num;
int num;
true
int num;
int num;
int num;
true
int num;
int num;
int num;
false
int num;
false
goto label;
label: