Download presentation
Presentation is loading. Please wait.
Published byHelen Stokes Modified over 9 years ago
1
M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk
2
#include void main() { //This is my first C++ Program /* This program will display a string message on the screen */ cout << “Hello to C++”; } Header file Preprocessor directives Main method Console output stream Insertion operator String constant or string literal Comments White spaces Program statements
3
#include void main() { int var1; int var2, var3; var1 = 5; int var4 = 10; cout << var1 + var4 << endl; } Variable names Type Declaration Initialization Arithmetic's / Comparisons End line manipulator
4
#include int numb = 15; void main() { int var1; int var2, var3; { int var 4; int var 5; } var1 = 5; int var6 = 10; cout << var1 + var4 << endl; }
5
#include void main() { int var1; int var2, var3; { static int var4 = 9; } var1 = 5; int var5 = 10; const int var6 = 20; cout << var1 + var4 << endl; }
6
Float Double Char Bool Short long
7
#include void main() { cout << “ Things are \a getting \n better in \t c++”; } \a beep \b backspace \n new line \t tab \\ \’ \” etc
8
#include void main() { int score; cout << “Please enter the score value\n”; cin >> score; cout << “Score is: \t” << score; } Extraction operation / get from operator
9
#include void main() { int height = 7; float length = 4.5; double sum = height + length; }
10
#include void main() { int var1 = 5; char var2 = static_cast (var1); } Static_cast (Variable)
11
#include void main() { int a = 5; int b = 6; cout << a % b; } +, -, %, * /
12
#include void main() { int a = 5; int b = 6; b += a; cout << a % b; } =, +=, -=, *=, /=
13
#include void main() { int a = 5; cout << a << endl; cout << a++ << endl; cout << ++a << endl; cout << a-- << endl; cout << --a << endl; } Prefix, postfix
14
#include void main() { int a = 5; int b = 6; cout << (a <= b); }, ==, !=, =
15
For Loop While Loop Do While Loop
16
#include void main() { for(int i = 0; i < 5; i++) cout << i*2; } Declaration and initialization expression Test expression Increment expression
17
#include void main() { int a; for( a =0; a<5; a++) { a = a*2; cout << a; } Blocks
18
#include void main() { for(int i=0; i<2;i++) { for(int j=0; j<3; j++) cout << i+j <<endl; }
19
#include void main() { int a = 4; while (a > 1) { cout <<a <<endl; a--; }
20
#include void main() { int a = 5; do{ cout <<a <<endl; a--; }while(a>2); }
21
#include void main() { int a = 5; int b = 6; if(a > b) cout << a % b; } if
22
#include void main() { int a = 5; int b = 6; if(a > b) cout << a % b; else cout << b % a; }
23
#include void main() { int a = 5; int b = 6; if(a < 3) if( a > b) cout << a % b; else cout << b % a; }
24
#include void main() { int num; cout > marks; switch(marks) { case 0: cout << “low”; break; case 1: cout << “medium”; break; case 2: cout << “high”; break; default: cout << “very high”; }
25
#include void main() { int var1 = 5; int var2 = 6; if(var1>3 && var1>var2) cout << “var1 is a high number” <<endl; } &&, ||, !
26
Unary !, ++, -- Arithmetic *, /, %, +, - Relational, =,==,!= Logical&&, || Conditional?: Assignment=, +=, -=, *=, /=, %=
27
#include void display(); void main() { cout << “main body” <<endl; display(); cout << “main body” <<endl; } void display() { cout << “Display function called” <<endl; } Declaration Definition Calling
28
#include int sum(int v1, int v2); void main() { int a = 5; int b = 6; cout << sum(a, b); } int sum(int v1, int v2) { int temp = v1 + v2; return temp; } By value Return statement
29
#include int sum(int& v1, int& v2) { return v1 + v2; } void main() { int a = 5; int b = 6; cout << a % b; } Passing objects
30
#include int sum(int v1, int v2) { cout << “function 1” <<endl; return v1 + v2; } int sum(int v1, int v2, int v3) { cout << “function 2” <<endl; return v1 + v2 + v3; } float sum (float v1, float v2) { cout << “function 3” <<endl; return v1 + v2; } void main() { int a = 5; int b = 6;int c = 10;float d = 4.5;float e = 6.4; cout << sum(a, b); cout << sum(a, b, c); cout << sum (d, e); }
31
#include Int fact(int n) { if(n > 1) return n * fact(n – 1); else return 1; } void main() { fact(3); }
32
#include inline int getTwice(int a) { return a*2; } void main() { cout << getTwice(4); }
33
Local Variables Global Variables Static Local Variables
34
#include int a = 5; int someFunction() { return a; } void main() { int b = a; cout << a % b; }
35
#include int a = 5; int someFunction() { static int halfValue = a/2; return a; } void main() { int b = a + halfValue; cout << a % b; }
36
#include int& sum(int a, int b) { return a+b; } void main() { int a = 4; int b = 5; sum(a, b); }
37
#include int someFunction(const int b) { if(b < 2) cout << “B < 2” <<endl; } void main() { int b = 5; cout <<someFunction(b); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.