Você está na página 1de 26

1

Chapter 2:
C++ FUNDAMENTALS
(Part 2)
DCS5088 :: Chapter 2 (part 2) 2
Objectives
At the end of this lecture, students should
be able to :
Implement repetition structures,
Declare, initialize and implement arrays
and pointers in C++
DCS5088 :: Chapter 2 (part 2) 3
2.9 Looping/repetition statements
The control structure for performing
iteration is called a loop.
3 primary types of loops:
while loops
do-while loops
for loops
DCS5088 :: Chapter 2 (part 2) 4
2.9.1 while loop
initalization
while (expression)
{
statement1
statement2
. . .
}
DCS5088 :: Chapter 2 (part 2) 5
Example (while)
#include <iostream>
using namespace std;
int main()
{
int i;
i=1;
while (i <=5)
{ cout<<"Loop number "<<i<<" in the while loop\n";
i++;
}
return 0;
}
DCS5088 :: Chapter 2 (part 2) 6
2.9.2 for loop
for (initial condition; condition; increment)
{
statement1
statement2
. . .
}
DCS5088 :: Chapter 2 (part 2) 7
2.9.3 do while loop
Initalization
do
{ statement1;
statementN;
} while (expression);
DCS5088 :: Chapter 2 (part 2) 8
2.10 Address Operator (&)
Recall that a variable declaration is a
request to allocate a certain number of
bytes of storage.
Example:
int number;
Each memory location has an address
DCS5088 :: Chapter 2 (part 2) 9
Address Operator(&) (cont)
#include<iostream>
using namespace std;
void main() {
int student_id;
float cgpa;
student_id=10401222;
cgpa=3.96 ;
cout<<student_id<<" "<<cgpa<<endl;
cout<<endl<< &student_id<<" "<<&cgpa<<endl;
}
DCS5088 :: Chapter 2 (part 2) 10
2.11 Pointers
A pointer variable is one that can hold
addresses.
Use pointer variables to indirectly
reference another variable
Useful when working with large data
structures or classes
DCS5088 :: Chapter 2 (part 2) 11
2.11.1 Creating a pointer
Syntax:
Point_type *Pointer_name

We declare pointers using the * operator in the
declaration, as in:
int *p;
Type: int*
Name: p
Address: FF80
value
DCS5088 :: Chapter 2 (part 2) 12
Creating a pointer (cont)
This says that p is a pointer to an integer,
in other words, p can hold the address of
another integer variable.
To assign a variable to p, we do the
following:
int x;
p = &x; // Give p xs address
DCS5088 :: Chapter 2 (part 2) 13
2.11.2 Memory structure
FF84
Type: int* int
Name: p x
Address: FF80 FF84
value
int x;
p = &x;
p = FF84
DCS5088 :: Chapter 2 (part 2) 14
2.12 Arrays
A list of values, each having the same
data type and the same name.
Access to individual elements is done
through an index or subscript
Example (to declare 100 elements) :
int x [100];
DCS5088 :: Chapter 2 (part 2) 15
2.12 Arrays
Array Numbering: first element is x[0], followed
by x[1], etc., up to x[99]
Store data using the assignment operator or
from user input, as shown:
x[33] = 17;
cin >> x[5];
You may initialize the array when you first
create it . Example:
int scores[3] = {92,48,40};
DCS5088 :: Chapter 2 (part 2) 16
2.12.1 Initialize array with loop

int scores[3];
for (int i=0; i<3; i++)
{
cout<<enter score<<endl;
cin>>scores[i]
}
DCS5088 :: Chapter 2 (part 2) 17
2.13 Character Arrays
One of the most common uses of an
array is a character array. A character
array is simply a string of characters that
makes up one or more words.
It is common to place such input into
arrays of characters.
Lets look at two examples.
DCS5088 :: Chapter 2 (part 2) 18
Example 1
#include<iostream>
using namespace std;
int main()
{
char message[25];
cout << "Please enter your message\n";
cin >> message;
cout << "Message : " << message << endl;
return 0;
}
Character array here is
capable of storing a
string of 24 characters
and a null terminator
character
DCS5088 :: Chapter 2 (part 2) 19
cin >> message;
(does not accept blank space, characters after
blank space will be ignored)
It can also take in any number of characters
regardless of array size, however this can result in
loss of data or other serious run time errors
DCS5088 :: Chapter 2 (part 2) 20
Example 2
#include<iostream>
using namespace std;
int main()
{
char message[25];
cout << "Please enter your message\n";
cin.getline(message,24);
cout << "Message: " << message << endl;
getchar();
return 0;
}
DCS5088 :: Chapter 2 (part 2) 21
cin.getline(message,24);
(only takes maximum 24 characters including blank spaces)
DCS5088 :: Chapter 2 (part 2) 22
2.13.1 Assignment on character
arrays
Need to use strcpy() function.
Example to store the value Hello World to an
array of characters:
char message[25];
strcpy(message, "hello world");

need to include string header file whenever we
use string functions
#include<string>
DCS5088 :: Chapter 2 (part 2) 23
2.13.2 String Type
Represents a string of characters
A string is actually an object of the C++
standard library class string.
Defined in header file <string>
Example: string name;
We can assign values using assignment
operator:
name = Helmi;
DCS5088 :: Chapter 2 (part 2) 24
Example (create variable of type string)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string message;
cout << "Please enter your message\n";
getline(cin, message);
cout << "Message : " << message << endl;
return 0;
}
Reads a string from user and
assign it to the variable message of
type string.
Possible to use cin>>message. (but
this method will read characters until
the first blank space character is
reached)
DCS5088 :: Chapter 2 (part 2) 25
2.14 Pointers and Arrays
Pointers are related to Arrays
The Array name is an address
Example, the below is valid
int num[10] = {0};
int *p = num;
DCS5088 :: Chapter 2 (part 2) 26
Example: pointers and arrays
#include<iostream>
using namespace std;
void main() {
int num[7] = { 1,2,3,8,11,10,14}, *p;

p = num;
for(int i=0; i < 7; i++)
cout<<p[i]<<endl;
}

Você também pode gostar