Computer Programming Principles - I

CE - 102

Teaching Assistants:

Syed Muhammad Taha.
Saad Abbasi.

Content

  • Basic concepts of C++
  • Basic Structure of C++
  • Compiler or interpreter
  • Programming language
  • Data types
  • Header files
  • I/O Commands
  • Loops
  • Decision making (if and else statements).

Programming

Programming Languages

Syntax 

vs

Semantics

Expression 

vs

Statement

Compiler

vs

Interpreter

IDE??

g++ -o output foo.c

Basic Structure of C++

#include <iostream.h>
#include <conio.h>

void main(){
    /*Multi
        Line
            Comment*/
    //Single line comment
    cout << "Hello World!" << endl;
    getch();
}
#include <iostream>

using namespace std;

int main(){
    /*Multi
        Line
            Comment*/
    //Single line comment
    cout << "Hello World!" << endl;
    return 0;
}

C++  97

C++  98, 11, 14 and 17

Header Files

#include <iostream.h>
#include <math.h>
#include <iostream>
#include <cmath>

Namespace

using namespace std;

Main function

void main(){

}
int main () {
    return 0;
}

Output on standard output

cout << "Hello World" << endl;
cout << "Hello World! << endl;

Comments

// Single line comment
/*Multi 
    Line
        Comment*/
// Single line comment
/*Multi 
    Line
        Comment*/

Get single character

getch();

Data Types

Name Range Typical bit width
short -32768 to 32767 ​2 bytes
int -2147483648 to 2147483647 4 bytes
long  -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 8 bytes
char -128-127 or 0-255 1 byte
float +/- 3.4e +/- 38 (~7 digits) 4 bytes
double ​+/- 1.7e +/- 308 (~15 digits)
8 bytes
long double +/- 1.7e +/- 308 (~15 digits) 8 bytes

I/O Commands

cin

cout

Escape Sequences

Escape Sequences

Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

Operators and Operands

Arithmetic Operator

Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ (post-fix and prefix) Increment operator, increases integer value by one A++ will give 11
-- (post-fix and  prefix) Decrement operator, decreases integer value by one A-- will give 9
Operator Description Example
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Relational Operators

Assignment Operators

Assignment x = y x = y
Addition assignment x += y x = x + y
Subtraction assignment x -= y x = x - y
Multiplication assignment x *= y x = x * y
Division assignment x /= y x = x / y
Remainder assignment x %= y x = x % y

Logical Operators

Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. !(A && B) is true.

Bitwise Operators

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111

Tertiary Operator

int a = 10;
int b = 20;
int x;
​x = (x > y) ? a : b;

->.>><<sizeof,?::}[]>?<>

Now Say, This is also an Operator

This is Sparta

Programming

Thank You &
Happy Coding;

bitwise AND

visit
www.github.com/isaadabbasi/cpp/

CPP I

By Syed M. Taha