Programming For Engineers Lecture 02
Programming For Engineers Lecture 02
6 {
preprocessor directive
7 std::cout << "Welcome to C++!\n"; Message to the C++ preprocessor
Lines beginning with # are preprocessor directives
8
#include <iostream> tells the preprocessor to
9 return 0; C++
// indicate that program programs
include
ended the contain of
contents
successfully onetheorfile
more functions, exactly
<iostream>, which
one of which must be main
includes input/output operations (such as printing to
10 } Parenthesis
the screen).used to indicate a function
int means that main "returns" an integer value.
More in Functions topics
Welcome to C++!
Prints the string of characters contained between the
return is one a way toquotation
exit a marks.
function. Left brace { begins the body of every function
return 0, in this case,The entire
means line, including std::cout, the <<
that and a right brace } ends it
the program terminatedoperator,
normally.the string "Welcome to C++!\n" and
the semicolon (;), is called a statement.
• <<
• stream insertion operator
• value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
std::cout << " Welcome to C++!\n"
•\
• escape character
• indicates that a “special” character is to be output
A Simple Program:
Printing a Line of Text (II)
Escape Sequence Description
3 #include <iostream>
4 1. Load <iostream>
5 int main()
6 { 2. main
7 std::cout << "Welcome ";
2.3 newline
Welcome to C++!
Unless new line '\n' is specified, the text continues 2.4 exit (return 0)
on the same line.
4 1. Load <iostream>
5 int main()
2. main
6 {
10 }
2.3 Print "to"
Welcome
2.4 newline
to
C++!
2.5 newline
2.7 newline
2000 Deitel & Associates, Inc. All rights 2.8 exit (return 0)
using statements
• eliminate the need to use the std:: prefix
• allow us to write cout instead of std::cout
• at the top of the program write
using std::cout;
using std::cin;
using std::endl;
to use those functions without writing std::
The keyword using technically means, use this whenever you can.
This refers, in this case, to the std namespace. So whenever the
computer comes across cout, cin, endl or anything of that matter, it
will read it as std::cout, std::cin or std::endl.
How do addressing works in a housing society ?
Memory Concepts
• Variable names correspond to locations in the
computer's memory.
• Every variable has a name, a type, a size and a value.
• Whenever a new value is placed into a variable, it replaces the
previous value - it is destroyed
• reading variables from memory does not change them
• A visual representation
integer1 45
C++ Variable Types
1 bool
Stores either value true or false.
2 char
Typically a single octet (one byte).
3 int
The most natural size of integer for the machine.
4 float
A single-precision floating point value.
5 double
A double-precision floating point value.
typedef Declarations
• You can create a new name for an existing type using typedef. Following is the simple
syntax to define a new type using typedef −
• feet distance;
Enumerated Types
• An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each
enumerator is a constant whose type is the enumeration.
• Creating an enumeration requires the use of the keyword enum. The general form of an enumeration type is −
• For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value
"blue".
• int main() {
• cout << "Size of char : " << sizeof(char) << endl;
• cout << "Size of int : " << sizeof(int) << endl;
• cout << "Size of short int : " << sizeof(short int) << endl;
• cout << "Size of long int : " << sizeof(long int) << endl;
• cout << "Size of float : " << sizeof(float) << endl;
• cout << "Size of double : " << sizeof(double) << endl;
•
• return 0;
•}
• = (assignment operator )
• assigns value to a variable
• binary operator (has two operands)
sum = variable1 + variable2;
Addition operator
Program
Code
Write a program to add three numbers
And show its output on screen
Solve following
• 2+3*4 = 2+12 = 14. Note that the multiplication is carried out before the addition.
• (2+3)*4 = 5*4 = 20. You use parentheses to specify that the addition be carried out first.
• 10 - 4 - 2 = 6 - 2 = 4. You carry out the subtractions working from left to right.
• 10 - (4-2) = 10-2 = 8. You use parentheses to carry out the rightmost subtraction first, which
alters the answer.
• 12/3+3 = 4+3 = 7. Division comes first.
• 12/(3+3) = 12/6 = 2. The parentheses indicate that you want the addition to be carried out first.
• 2^3*3 = 2*2*2*3 = 24. The power is evaluated first, and the result is multiplied with 3.
• 2^(3*3) = 2^9 = 2*2*2*2*2*2*2*2*2 =512. The parentheses indicate that the exponent is the
product 3*3=9.
• 12/2*3 = 6*3 = 18. Multiplication and division have the same precedence, and we work from left
to right.
• 12/2/3 = 6/3 = 2. Again, we work from left to right.
• 18/3^2 = 18/9=2. The power has a higher priority than the division, and is evaluated first. Note
that 3^2 means 3*3, but if we replace 3^2 with 3*3 we get a different result: 18/3*3 = 6*3 = 18.
• (18/3)^2 = 6^2 = 36. The parentheses indicate that the base equals 18/3=6.
Arithmetic
• Arithmetic operators:
C ++ o p e ra tio n Arith m e tic Alg e b ra ic C ++ e xp re ssio n
o p e ra to r e xp re ssio n
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s
• if structure - decision based on truth or falsity of condition. If condition met execute, otherwise ignore.
• Equality and relational operators:
Standard algebraic equality C++ equality Example Meaning of
operator or or relational of C++ C++ condition
relational operator operator condition
Relational operators
Name of the
Operators Type
Operator
AND
&& Binary
Operator
|| OR Operator Binary
! NOT Operator Unary
C++ Program Logic
ELIGIBILITY TO BE A VOTER in
Pakistan
• Are you a citizen of Pakistan?
• is your age not less than 18 years on the first day of January of the
year in which the rolls are prepared or revised,
• Are you declared by a competent court to be of unsound mind
• Are you a resident of an electoral area,
• Are you registered on the Electoral Rolls are eligible to cast their
votes.
• You are enrolled as a voter in that electoral area ?
Complete code to be written on your lecture pad and personalize all
code