Sie sind auf Seite 1von 33

Submitted To:

COMPUTER
Engr. Zohaib Hassan
Submitted By:
PROGRAMMING
shahzaib
17-ee-123
PRESENTATION
Variables ,DATA TYPES , KEYWORDS & Constants

 VARIABLE:

Variable is simply name given to memory location which acts as placeholder or

container for storing data. It may help to think of variables as a container for a value.

And the value of variable can be changed.

Example:

a, b, c, d …..etc
 DATA TYPES:

We can store variables in our computer memory. Since each type of data takes
different amount of memory, like integer takes 2 bytes, decimal numbers take 4
byte, and single character takes 1 byte. So we have to tell computer about the
type of data we are going to store in our variable.

There are three types of data types;

1. Predefined are those which are inbuilt

e.g. int, float, char, double etc.


2. User defined are those which are created by the user for own purpose
e.g. structure union etc.
3. Derived datatypes are those which are derived from predefined datatypes
e.g. array , pointers etc.

 KEYWORDS:
Keyword is a predefined or reserved word in C++ library with a fixed
meaning and used to perform an internal operation. There are total 32 keyword
in C++. We can’t use keywords as identifiers or variables because if we do so
we are trying to assign a new meaning to the keyword, which is not allowed by
the computer.
Examples:
double, int, float, while, return, etc.
 CONSTANTS:
Constant is the which does not change its value. Once the value is initialized, that
can’t be changed.
Rules for variable declaration

1. All variable names must begin with a letter of the alphabet or an underscore.
2. After the first initial letter, variable names can also contain letters and numbers.
3. Uppercase characters are distinct from lowercase characters. Using all
uppercase letters is used primarily to identify constant variables.
4. Keywords or reserved can not be used as a variable name.
5. Examples:
int grade, float radius … etc.
Difference between compiler & Interpreter

 The compiler takes a program as a whole and translates it, but interpreter translates a program
statement by statement.
 Intermediate code or target code is generated in case of a compiler. As against interpreter doesn’t
create intermediate code.
 A compiler is comparatively faster than Interpreter as the compiler take the whole program at one
go whereas interpreters compile each line of code after the other.
 The compiler requires more memory than interpreter because of the generation of object code.
 Compiler presents all errors concurrently, and it’s difficult to detect the errors in contrast
interpreter display errors of each statement one by one, and it’s easier to detect errors.
 In compiler when an error occurs in the program, it stops its translation and after removing error
whole program is translated again. On the contrary, when an error takes place in the interpreter, it
prevents its translation and after removing the error, translation resumes.
 In a compiler, the process requires two steps in which firstly source code is translated to target
program then executed. While in Interpreter It’s a one step process in which Source code is
compiled and executed at the same time.
 The compiler is used in programming languages like C, C++, C#, Scala, etc. On the other Interpreter
is employed in languages like PHP, Ruby, Python, etc.
 PSEUDO CODE:
Pseudo code is a term which is often used in programming and algorithm-based fields. It is methodology
that allows the programmer to represent the implementation of an algorithm

It is an implementation of an algorithm in form of annotations and informative text written in plain English.
It has no syntax like any of the programming language and thus can not be compiled or interpreted by the
computer.

 ALGORITHM:

It is a computer procedure that is a lot like a recipe and tells your computer preciously what steps to take to
solve a problem or reach a goal.
CONDITIONAL BLOCKS
Conditional blocks allow a program to take a different path depending on some
conditions. These allow a program to perform a test and then take action based on the
result of that test.
Following conditional blocks are offered by C++;

 If statement
 If else statement
 If … elseif statement
 Nested if else statement
 Switch
THE IF STATEMENT:
The if statement is a decision making statement. It is used to control the flow of execution
of the statements and alsousedto test logically whether the condition is true or false.

SYNTAX:

if test expression:
statement(s)

1
0
EXAMPLE1:
CODE:
 // Program to print positive number entered by the user
 // If user enters negative number, it is skipped

 #include <iostream>
 using namespace std;
 int main()
 {
 int number;
 cout << "Enter an integer: ";
 cin >> number;
 // checks if the number is positive
 if ( number > 0)
 {
 cout << "You entered a positive integer: " << number << endl;
 }
 cout << "This statement is always executed.";
 return 0;
 }
 EXAMPLE 2:
CODE:

 #include <iostream>
 using namespace std;

 int main()
 {
 int a = 15, b = 20;

 if (b > a) {
 cout << "b is greater" << endl;
 }
 system("PAUSE");
 }

IF ELSE STATEMENT:
Theif…else statement is called alternative execution, in which there are two possibilities and the condition
determines which one gets executed.

SYNTAX:

if test expression:
Body of if
else:
Body of else
EXAMPLE 1:
CODE:
 #include <iostream>
 using namespace std;

 int main()
 {
 int a = 15, b = 20;

 if (b > a) {
 cout << "b is greater" << endl;
 } else {
 cout << "a is greater" << endl;
 }
 system("PAUSE");
 }
EXAMPLE 2:
CODE:
 #include <iostream>
 using namespace std;

 int main()
 {
 char name;
 int password;

 cout << "Enter the name: "; cin >> name;


 cout << " Enter your password: "; cin >> password;
 if (name == 'GG') {
 if (password == 1346) {
 cout << "Login successful";
 }
 else {
 cout << "Incorrect PASSWORD, Try again.";
 }
 }
 else {
 cout << " Incorrect Login Details, Try again.";
 }
 }
IF ELSE IF STATEMENTS:

Sometimes you have a condition and you want to execute a block of cade if condition is true and execute another piece of code if the same condition is false

Example 1:

 #include <iostream>

 using namespace std;

 int main () {

 // local variable declaration:

 int a = 100;

 // check the boolean condition

 if( a == 10 ) {

 // if condition is true then print the following

 cout << "Value of a is 10" << endl;

 } else if( a == 20 ) {

 // if else if condition is true

 cout << "Value of a is 20" << endl;

 } else if( a == 30 ) {

 // if else if condition is true

 cout << "Value of a is 30" << endl;

 } else {

 // if none of the conditions is true

 cout << "Value of a is not matching" << endl;

 }

 cout << "Exact value of a is : " << a << endl;

 return 0;

 }
EXAMPLE 2:
CODE:
 int main()

 {
 int age;

 cout<< "enter your age; ";


 cin >> age;
 cin.ignore();

 if (age <100) {
 cout<< "true\n";
 }
 else if (age>100) {
 cout<< "false\n";
 }

 else if (age == 100); {


 cout<<"unknown\n";
 }

 cin.get();

 }
Switch
A switch statement allows a variable to be tested for equality againt a list of values each value is called a
case and the variable being switched on is checked for each case

Switch(expression){
Case constant-expression:
Statement(s);
Break;
Case constant- expression:
Statement(s);
Break;
Statement(s)
}
Example 1:
 Example of Switch Case

 #include <iostream>

 using namespace std;

 int main(){

 int num=5;

 switch(num+2) {

 case 1:

 cout<<"Case1: Value is: "<<num<<endl;

 case 2:

 cout<<"Case2: Value is: "<<num<<endl;

 case 3:

 cout<<"Case3: Value is: "<<num<<endl;

 default:

 cout<<"Default: Value is: "<<num<<endl;

 }

 return 0;

 }
ALGORITHM

 ALGORITHM
 An algorithm is a well-defined procedure that allows a computer to solve a
problem. Another way to describe an algorithm is a sequence of unambiguous
instructions. ... In fact, it is difficult to think of a task performed by your
computer that does not use algorithms.
Example of algrithm
 example of algorithm

 The algorithm would look something like this:

 Step 1: Let Largest = L1

 This means you start by assuming that the first number is the largest number.

 Step 2: For each item in the list:

 This means you will go through the list of numbers one by one.

 Step 3: If the item > Largest:

 If you find a new largest number, move to step four. If not, go back to step two, which means you move on to the next number in the list.

 Step 4: Then Largest = the item

 This replaces the old largest number with the new largest number you just found. Once this is completed, return to step two until there are no more
numbers left in the list.

 Step 5: Return Largest


While loop

 In most computer programming languages, a while loop is a control flow statement


that allows code to be executed repeatedly based on a given Boolean condition.
The while loop can be thought of as a repeating if statement.

 Syntax of while loop


 while(condition)
 {
 statement(s);
 }

How while Loop works?

 In while loop, condition is evaluated first and if it returns true then the
statements inside while loop execute, this happens repeatedly until the
condition returns false. When condition returns false, the control comes out
of loop and jumps to the next statement in the program after while loop.
Example of while loop
 include <iostream>
 using namespace std;
 int main(){
 int i=1;
 /* The loop would continue to print
 * the value of i until the given condition
 * i<=6 returns false.
 */
 while(i<=6){
 cout<<"Value of variable i is: "<<i<<endl; i++;
 }
 }
Example 2
 #include <iostream>
 using namespace std;

 int main ()
 {
 /* local variable Initialization */ int n = 1,times=5;

 /* while loops execution */ while( n <= times )


 {
 cout << "C++ while loops: " << n <<endl;
 n++;
 }
 return 0;
 }

nested loop

 A nested loop is a loop within a loop, an inner loop within the body of an
outer one. How this works is that the first pass of the outer loop triggers the
inner loop, which executes to completion. Then the second pass of the outer
loop triggers the inner loop again. This repeats until the outer loop finishes.
Example
 Below is an example of nested while loop

 #include <iostream>

 using namespace std;

 int main ()

 {

 int i = 0;

 while(i < 3)

 {

 int j = 0;

 while(j < 5)

 {

 cout << "i = " << i << " and j = " << j << endl;

 j++;

 }

 i++;

 }

 return 0;

 }


Nested for loop

 Nested for loop is used to calculate the sum of two 2-dimensional matrices. The program
consists of three for nested loops where the outer loop runs equal to size of row and inner
loop runs equal to size of column.
 Syntax
 The syntax for a nested for loop statement in C++ is as follows −

 for ( init; condition; increment ) {


 for ( init; condition; increment ) {
 statement(s);
 }
 statement(s); // you can put more statements.
 }

Example
 example 11
 #include <iostream>
 #include <conio.h>
 using namespace std;

 int main()
 {
 int a[10][10], b[10][10], s[10][10];
 int i,j,row, column;
 cout <<"Enter size of row:";
 cin >> row;
 cout <<"Enter size of column:";
 cin >> column;
 cout <<"Enter elements of matrix A" << endl;
 for(i=0;i<row;i++)
 {
 for(j=0;j<column;j++)
 {
 cin >> a[i][j];
 }

Contineous example 1
 }
 cout <<"Enter elements of matrix B" << endl;
 for(i=0;i<row;i++)
 {
 for(j=0;j<column;j++)
 {
 cin >> b[i][j];
 }
 }
 cout << "Sum of A and B" << endl;
 for(i=0;i<row;i++)
 {
 for(j=0;j<column;j++)
 {
 cout << a[i][j] + b[i][j] << " ";
 }
 cout << endl;
 }
 getch();
 return 0;
 }
C++ do..while loop

 The codes inside the body of loop is executed at least once. Then, only the test
expression is checked.
 If the test expression is true, the body of loop is executed. This process continues
until the test expression becomes false.
 When the test expression is false, do...while loop is terminated.

 The syntax of do..while loop is:


 do {
 // codes;
 }
 while (testExpression);
Example
 // C++ program to add numbers until user enters 0
 #include <iostream>
 using namespace std;
 int main()
 {
 float number, sum = 0.0;

 do {
 cout<<"Enter a number: ";
 cin>>number;
 sum += number;
 }
 while(number != 0.0);
 cout<<"Total sum = "<<sum;

 return 0;
 }
THE
 END

Das könnte Ihnen auch gefallen