Download presentation
Presentation is loading. Please wait.
Published byΝύξ Αλεβιζόπουλος Modified over 6 years ago
1
Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5
Department of Computer Science & Engineering Air University Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5 By: Saqib Rasheed
2
A First Program - Greeting.cpp
// Author Saqib Rasheed #include <iostream> using namespace std; int main() { cout << "Hello world!"; return 0; } By Saqib Rasheed
3
Greeting Output By Saqib Rasheed
4
Whitespace #include <iostream> using namespace std; int main () { cout << “Hello word!\n” ; return 0;} By Saqib Rasheed
5
C++ Programming Basics by Robert Lafore
Reference Chapter # 2: C++ Programming Basics by Robert Lafore By Saqib Rasheed
6
Escape Sequence Escape sequences are used to represent certain special characters within string literals and character literals. \n new line \t horizontal tab (8 spaces) \’ single quote \” double quote \? question mark \v vertical tab \a audible bell By Saqib Rasheed
7
Example # 2 #include <iostream> using namespace std;
int main ( ) { cout << “\t Welcome to Air University \n“; cout<<“ \n\t School of Engineering”; cout<<“it can be\n used \t between\n the string”; return 0; } New Line Tab By Saqib Rasheed
8
Variables A storage box, its type defines the kind of stuff you store in it E.g. shoe box used for shoes Jewelry box used for jewelry CD kit used for CDs By Saqib Rasheed
9
Variable Variable is a box
In computer, a storage box is a memory location on RAM By Saqib Rasheed
10
Each little box is a memory location
Memory in Computer Each little box is a memory location First you ask computer to give you a box and you also have to specify what type of box you want Occupied Memory Spaces Free Memory Spaces By Saqib Rasheed
11
Asking for Memory Type of Box box name;
Type is a bit messy, it can’t be shoe box or jewelry Could be anything, alpha, bravo, charlie Some rules apply integer character double float bool (etc) By Saqib Rasheed
12
Rules for Variable name
Rules to Write Variable. First Letter is always Character Underscore _ (Not Recommended) Can’t use real numbers (Syntax Errorr) int num1; By Saqib Rasheed
13
Rules for Variable name
In a program a variable has: Name Type Size Value int num = 10; Assignment Operator By Saqib Rasheed
14
Rules of variables int n1; int n1,n2,n3; int n1; int n2; int n3;
float a1; int b1; double c1; By Saqib Rasheed
15
Type Size (bytes) Range-Minimum Range- Maximum
Character Unsigned Char Signed Char Integer ,147,483,648 2,147,483,647 Signed Int ,4294,967,295 Unsigned Int ,147,483,648 2,147,483,647 Short Int ,768 32,767 Signed short Int ,535 Unsigned Short Int ,768 32,767 Long Int ,147,483,648 2,147,483,647 Signed Long Int ,294,967,295 Unsigned Long Int 8 -2,147,483,648 2,147,483,647 Float E E+38 Double E E+308 Long Double E E+308 Bool 1 True/False By Saqib Rasheed
16
How To get Value from User
How to get value from user we use cin>>x; Also defined in <iostream> library file
17
Displaying age #include<iostream> using namespace std; int main (){ float a1,a2; cout<<“Enter age for student 1=”; cin>>a1; cout<<“Enter age for student 2=”; cin>>a2; cout<<a1; cout<<a2; return 0; } By Saqib Rasheed
18
Displaying age #include<iostream> using namespace std; int main (){ float a1,a2; cout<<“Enter age for student 1=”; cin>>a1; cout<<“Enter age for student 2=”; cin>>a2; cout<<“\nAge of Student 1 = ”<<a1; cout<<“\nAge of Student 2 = ”<<a2; return 0; } By Saqib Rasheed
19
Displaying age #include<iostream> using namespace std; int main (){ float a1,a2; cout<<“Enter age for student 1=”; cin>>a1; cout<<“Enter age for student 2=”; cin>>a2; cout<<“\nAge of Student 1 = ”<<a1; cout<<“\nAge of Student 2 = “ <<a2; return 0; } By Saqib Rasheed
20
Displaying age #include<iostream> using namespace std; int main (){ float a1,a2; cout<<“Enter age for student 1=”; cin>>a1; cout<<“Enter age for student 2=”; cin>>a2; cout<<“\nAge of Student 1 = ”<<a1; cout<<“\nAge of Student 2 = “; cout <<a2; return 0; } By Saqib Rasheed
21
Average age Example #include<iostream> using namespace std; int main (){ float a1,a2,a3,a4,a5; cout<<“Enter age for student 1=”; cin>>a1; cout<<“Enter age for student 2=”; cin>>a2; : float sum, avg; sum = a1+a2+a3+a4+a5; Avg = sum / 5; cout<<“Average age = ”<<avg; return 0; } By Saqib Rasheed
22
Arithmetic Operators Common Addition + Subtraction - Multiplication *
Division / Mod % Write m*x + b not mx + b By Saqib Rasheed
23
Integer Division Integer division produces an integer result Examples
Truncates the result Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3 By Saqib Rasheed
24
Mod Produces the remainder of the division Examples
5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4 By Saqib Rasheed
25
Operators and Precedence
Consider mx + b Consider m*x + b which of the following is it equivalent to (m * x) + b m * (x + b) Operator precedence tells how to evaluate expressions Standard precedence order () Evaluate first, if nested innermost done first * / % Evaluate second. If there are several, then evaluate from left-to-right + - Evaluate third. If there are several, then evaluate from left-to-right By Saqib Rasheed
26
Operator Precedence Examples 20 - 4 / 5 * 2 + 3 * 5 % 4 (4 / 5)
/ 5 * * 5 % 4 (4 / 5) ((4 / 5) * 2) ((4 / 5) * 2) (3 * 5) ((4 / 5) * 2) ((3 * 5) % 4) (20 -((4 / 5) * 2)) ((3 * 5) % 4) (20 -((4 / 5) * 2)) + ((3 * 5) % 4) By Saqib Rasheed
27
Write Code for this equation
Precedence 4 3 X = * 6 ( 10 – 2 ) / 10 Y = 10 / 5 * 2 ( ) + 6 Write Code for this equation 5 2 1 By Saqib Rasheed
28
Quadratic Equation In algebra In C y = ax2 + bx + c
y = a*x*x + b*x + c By Saqib Rasheed
29
#include <iostream> using namespace std int main ( ) { int x ;
int y ; int z ; x = 10 ; y = 20 ; z = x + y ; cout << " x = " ; cout << x ; cout<< " Y = " <<y; cout << " z =x + y " << x+y ; } By Saqib Rasheed
30
Declaration of Variables
int x, y, z ; int x; int y; int z ; char n1, n2 = ‘a’; float num1, num2 = 30.8, n3; double n3; By Saqib Rasheed
31
Calculator Code #include <iostream> using namespace std int main ( ) { int num1, num2, res; cout<<“Enter Number 1 =”; cin>>num1; cout<<“Enter Number 2 =”; cin>>num2; res = num1 + num2; cout<<“Addition Result = ”<<res; res = num1 - num2; cout<<“Subtraction Result = ”<<res; res = num1 * num2; cout<<“Multiplication Result = ”<<res; res = num1 / num2; cout<<“Division Result = ”<<res; res = num1 % num2; cout<<“Modulation Result = ”<<res; }
32
Assignment # 1 Get five equations and write code for each equation
The depth of equation should be at least 6 numbers Get these numbers from user (Key board) Display the equation properly (Before ) Display the Result Instructions Individual assignment Due date is next class Copied assignments will be marked zero Late assignments not accepted Submit hard copies along with out put (Screen Shots) By Saqib Rasheed
33
C++ Programming Basics by Robert Lafore
Reference Chapter # 2: C++ Programming Basics by Robert Lafore By Saqib Rasheed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.