C++ Lecture 1 PPT
C++ Lecture 1 PPT
Raghav Garg
Today’s checklist
#include<iostream>
int main(){
cout<<“hello world”;
return 0;
}
How to move in next line?
Example :
cout<<“Hello PW”;
cout<<“Hello CW”;
Output will be ?
Use of escape sequence ‘\n’ and endl
Example :
cout<<“Hello PW”;
cout<<“\n”; or cout<<endl;
cout<<“Hello CW”;
Output will be ?
Use of escape sequence ‘\n’
cout<<"nn\n\nnn\n";
cout<<"nn/n/nnn/n";
}
Printing Numbers (what computer thinks is a number
and what is a number)
Examples :
cout<<4;
cout<<4+3;
cout<<“4+3”;
Variables and their Declaration
Let us focus on int data type as of now.
1) Variables as containers :
Printing Variables in C++ & Updation of Variables
int x = 5;
cout<<x;
x = 7;
cout<<x;
x = x + 6;
cout<<x;
x = x - 20;
cout<<x;
Arithmetic operations on int data type
int x = 5;
int y = 2;
cout<<x+y;
cout<<x-y;
cout<<x*y;
cout<<x/y;
Increment - Decrement operators and Comments
int x = 5;
x<+;
cout<<x;
x<-;
cout<<x;
<+x;
cout<<x;
–-x;
cout<<x;
Example : Take two integers input, a and b : a>b, and find the
remainder when a is divided by b.
Modulus Operator(%)
int a=10;
int b=4;
int Remainder = a%b
Float data type
float x = 3.1;
Arithmetic operations on float data type
float x = 5;
float y = 2;
cout<<x+y;
cout<<x-y;
cout<<x*y;
cout<<x/y;
Example : Calculating percentage of 5 subjects
float x1 = 90; </ x1 can be physics
float x2 = 91; </ x2 can be chemistry
float x3 = 92; </ x3 can be maths
float x4 = 93; </ x4 can be english
float x5 = 94; </ ohh wait comments ke baare me to bataya hi nahi xD
float percent = (x1 + x2 + x3 + x4 + x5)/5;
cout<<percent;
</ change the marks and run each time
Example : Calculating Area of a Circle
float radius = 5;
float pi = 3.1415;
cout<<area;
Boolean data type
bool x = true;
Example : Find the output
#include<iostream>
void main(){
bool a = true;
cout<<a<<endl;
}
Example : Find the output
#include<iostream>
using namespace std;
int main(){
int a = 0,b = 5;
</cout<<b<<endl;
cout<<a<<endl;
}
Variable Naming rules
1) Variables can start from an alphabet or underscore _ or $.
2) Special characters except _ and $ are not allowed.
3) Some particular keywords are not allowed.
4) Commas or blanks are not allowed.
#include <iostream>
int main(){
return 0;
}
Compilation Process
MCQ Time !
MCQ 1
Which of the following statements is false
(1) Each new C++ instruction has to be written on a separate line
(2) Usually all C++ statements are entered in small case letters
(2) 3
(3) 2
(4) 0
MCQ 3
What will be the value of d if d is a float after the operation
d = 2 / 7.0?
(1) 0
(2) 0.2857