Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Objective of this cours is to develop effective computer programming skills in solving complex problems and to learn adequate and operational software organization in developing real life engineering solutions using powerful object oriented paradigm of the language. It includes: Object, Oriented, Programming, Namespace, Identifies, Library, Parenthesis, Function, Compiler, Text, String, Constant
Typology: Slides
1 / 8
Usman Younis
Namespace identifies the part of a program in which certain names are recognized, std namespace includes all entities of standard C++ library Parenthesis follow the function name Function body is surrounded in braces Compiler ignores the whitespaces Text with in “quotation marks” is a string constant \n is an escape sequence (causes following text to be printed in a new line)
Program Basics (contd..)
<< is a left shift bitwise operator!
Program Basics (contd..)
cin serves the similar purpose as scanf () in C cin is also an object in C++ >> is extraction operator
endl manipulator , same as using \n
Data Types
Eliminating the sign of char and integer types, sets the Low value at 0 and High value as twice its default
Keyword Low High Bytes in Memory char −128 127 1 short −32,768 32,767 2 int −2,147,483,648 2,147,483,647 4 long −2,147,483,648 2,147,483,647 4 float 3.4× 10 −38^ 3.4× 1038 4 double 1.7× 10 −308^ 1.7× 10308 8 long double 3.4× 10 −4932^ 3.4× 104932 10
Type conversion
lower-type variables are automatically converted into higher-type variables You can use static_cast
ans = a (^) ***** b
double int float
a
float
ans temp double
Arithmetic operators
6 % 8 will result in 6 8 % 8 will result in 0 10 % 8 will result in 2
A = A + B, can be written as A += B Similar applies for: *, −, /, and %
A = A + 1, can be written as A += 1, or simply ++A Remember the prefix (++A) and postfix (A++) precedence. In first the increment is performed before using the variable; andin second, the increment is performed after.
Relational operators
Operator Meaning
Greater than < Less than == Equal to != Not equal to = Greater than or equal to <= Less than or equal to
Usman Younis
Loops
The for loop
Initialization Expression
Body of Loop
Increment Expression
Test Expression Exit
True
False
Loops (contd..)
The while loop
Body of Loop
Test Expression Exit
True
False
Loops (contd..)
The do loop
Body of Loop
Test True Expression^ Exit
False
Usman Younis
Decisions
Decisions (contd..)
if (condition is true) execute the statement; //single statement else execute this statement; Or if (condition is true) { execute multiple statements; statement ……. 1; statement ……. 2; and so on …. } else { execute multiple statements; }
Body of if
Test Expression
True
False
Body of else
Exit
Decisions (contd..)
if (condition is true) execute the statement; else if (this condition is true) execute this statement; else if (this conditions is true) execute this statement; else execute this statement;
Body of if
Test Expression
True
False
Exit Body of if
Test Expression
True Body of else
False
Remember to match the else with required if. It is a best practice to properly indent your code.
Decisions (contd..)
switch ( integer or character variable ){ case 1: execute statement; execute statement;break; case 2: execute statement; execute statement;break; case 3: execute statement; execute statement;break; .. default: execute statement; } //no break, as it is the only choice left
Switch 1st caseequals
False
True (^) Case Body
Exit
Switch 2nd caseequals False
True (^) Case Body
Default Body
You can use the break statement in a loop as well!
Decisions (contd..)
The conditional operator result = (check a condition)? first expression : second expression; if the condition is true, then first expression is assigned to the result; otherwise, the second expression is assigned e.g., k = ( a < 20)? a : b; (^) Test expression False
Exit
True Value of expression 1 Value of expression 2