0% found this document useful (0 votes)
347 views25 pages

Control Structures & Pointers in C++: by Prof. Manikandan Dept of Computer Application

This document discusses various control structures and pointers in C++. It covers conditional statements like if, if-else and nested if statements. It describes looping statements like for, while and do-while loops and provides examples of each. It also discusses selection statements like switch, break and continue. Additionally, it covers pointers, pointer operators, pointer expressions and provides examples of pointer usage in C++ programs.

Uploaded by

Prof.Manikandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
347 views25 pages

Control Structures & Pointers in C++: by Prof. Manikandan Dept of Computer Application

This document discusses various control structures and pointers in C++. It covers conditional statements like if, if-else and nested if statements. It describes looping statements like for, while and do-while loops and provides examples of each. It also discusses selection statements like switch, break and continue. Additionally, it covers pointers, pointer operators, pointer expressions and provides examples of pointer usage in C++ programs.

Uploaded by

Prof.Manikandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

CONTROL STRUCTURES &

POINTERS IN C++

By

Prof. Manikandan

Dept of Computer Application


[email protected]

Conditional Statements
1.Conditional Execution
The conditional expressions are mainly used for
decision making.
The following statements are used to perform the task
of the conditional operations.
1. if statement
2. if-else statement
3. Nested If statement

LOOPING

A set of statements are to be executed


continuously until certain condition is satisfied.
There are various loop statements available in
C++.

1. for loop
2. while loop
3. do-while loop

For loop

This loop consists of three expressions.

The first expression is used to initialize the index value.

The second to check whether or not the loop is to be


continued again.

The third to change ++ or -- the index value for further


iteration.

Syntax

for (initial_condition; test_condition; inc or dec value)


{
statement_1;
statement_2;
;;
;;
}

Example Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=0; i<=100; i++)
cout<<i<<endl;
getch();
}

WHILE LOOP

After checking whether the initial condition is


true or false and finding it to be true, only then
the while loop will enter into the loop operations.

Syntax
While (Condition)
Statement;

Example Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0; // initialization
clrscr();
while(i<10) //Condition
{
cout<<i<<endl;
i++; // increment
}
getch();
}

DO-WHILE LOOP

The statements are executed at least once before


the conditional expression is tested for its truth
value.

Syntax
do{
Statement_1;
Statement_2;
----------} while (expression);

Example Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0; // initialization
clrscr();
do
{
cout<<i<<endl;
i++;
// increment
}while(i<10);
//Condition
getch();
}

THE COMMA OPERATOR

The comma operator (,) is used in combination


with the for statement.

Syntax:
For(exp-1; exp-2; exp-3a, exp-3b)
{
Statement(s);
}

Example Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
for(a=0, b=10; a<=10; a++, b--)
cout<<"\n"<<a<<"\t" <<b;
getch();
}

SELECTION STATEMENT
1. Switch
2. Break
3. Continue

SWITCH STATEMENT

The switch statement is an extension of if-else


statement.

The switch statement allows the branching to


extend to any number.

Syntax
switch (expression) {
case constant_1 :
Statements;
case constant_2 :
Statements;

;;
;;
;;
case constant _n :
Statements;
default :
Statement;
}

Example Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int grade,hra;
clrscr();
cout<<"Enter the grade:";
cin>>grade;
switch(grade)
{
case 1:
hra=1000;
break;

case 2:
hra=800;
break;
case 3:
hra=500;
break;
default:
hra=0;
}
cout<<"Grade="<<grade<<"HRA="<<hra;
getch();
}

BREAK AND CONTINUE STATEMENT


The break statement is used to terminate the
control from the loop statements of the caseswitch structure.
Syntax
break;

The Continue statement is used to repeat the


same operations once again even if it checks the
error.
Syntax
continue;

UNCONDITIONAL GOTO

This goto statement is used to transfer the control from one


part of the program to other part without checking any
condition.

Example
#include<iostream.h>
void main(){
start:
cout << II BCA;
goto start;
}

POINTER

A pointer is a variable which holds the memory address of another


variable.

Pointer operator

A pointer operator can be represented by a combination of


*(asterisk) with a variable.

For eg.
int *ptr;
float *fp;

The general format of pointer declaration is


data_type *pointer_variable;

Address operator
An address operator can be represented by a combination
of & (ampersand) with a pointer variable.
For Ex:

K = &ptr;
Note:
The address Operator (&)
The Indirection Operator (*)
(or) Value at address and return the value stored at the
specified address.

Example Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a=5;
cout<<"The value of a :"<<a;
cout<<"The address of a :"<<&a;
getch();
}
Output :
The value of a = 5
The address of a = 0x516240e

POINTER EXPRESSIONS
Pointer assignment :

A pointer is a variable data type and hence the general


rule to assign its value to the pointer is same as that of
any other variable data type.

Ex:
int x,y;
int *ptr1, *ptr2;

ptr1 = &x;

The memory address of variable x is assigned to the


pointer variable ptr1.

y = *ptr1;

The contents of the pointer variable ptr1 is assigned


to the variable y, not the memory address.
ptr1 = &x;
ptr2 = ptr1;

Example Program:
#include <iostream.h>
void main()
{
char x,y;
char *pt;
x = k; // assign character
pt = &x;
y = *pt; // * value at address
cout << Value of x = << x << endl;
cout << Pointer value = << y << endl;
}

Thank You

You might also like