0% found this document useful (0 votes)
21 views

Lecture 1-Week1 - Structured Programming (Revision)

This document provides an overview of structured programming concepts taught in the first week of an Object Oriented Programming course. It covers basic input/output statements, comments, selection statements like if/else, repetition statements like while and for loops, functions, arrays, pointers, and dynamic memory allocation. Examples are provided to demonstrate how to get user input, print output, sort arrays, pass variables by value and reference to functions, use pointers with arrays, and allocate memory dynamically using new and delete.

Uploaded by

Nnoor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lecture 1-Week1 - Structured Programming (Revision)

This document provides an overview of structured programming concepts taught in the first week of an Object Oriented Programming course. It covers basic input/output statements, comments, selection statements like if/else, repetition statements like while and for loops, functions, arrays, pointers, and dynamic memory allocation. Examples are provided to demonstrate how to get user input, print output, sort arrays, pass variables by value and reference to functions, use pointers with arrays, and allocate memory dynamically using new and delete.

Uploaded by

Nnoor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Object Oriented Programming

(CS1143)

Department of Computer Science


Capital University of Science and Technology (CUST)
Structured Programming
(Revision)
Week 1

2
Example:

3
#Include
 # Include
 The #include directive tells the compiler to include some already existing C++ code
in your program
 The included file is then linked with the program
 There are two forms of #include statements:
#include <iostream> //for pre-defined files

#include "my_lib.h" //for user-defined


files

4
Input and Output Statements

 cin >> variable-name;


Meaning: read the value of the variable called <variable-name> from
the user
Example:
cin >> a;
cin >> b >> c;

5
Input and Output Statements Cont..

 cout << variable-name;


Meaning: print the value of variable <name> to the user
 cout << “any message”;
Meaning: print the message within quotes to the user
 cout << endl;
Meaning: print a new line
Example:
cout << a;
cout << b << c;
cout << “This is my character: “<<endl << my-character<< endl;

6
Comments

 Part of good programming is the inclusion of comments in the


program
 There are two type of Comments
 Single-line comments
 // Welcome to C++ Programming

 Multiple-line comments
 Enclosed between /* and */

7
A Choice Statement

 Syntax
if(condition)
false
condition
action

 if the condition is true then execute the


true
action.
action
 action is either a single statement or a
group of statements within braces.

8
Another Choice Statement

 Syntax
if (condition)
true false
Action_A condition
else
Action_B

 if the condition is true Action_A Action_B

execute Action_A
else
execute Action_B

9
A Loop Statement

 Syntax
while (condition)
action
 How it works: condition
false
 if condition is true then execute action
 repeat this process until condition
evaluates to false
true
 action is either a single statement or a
group of statements within braces. action

10
Another Loop Statement

 Syntax
for (initialization; condition; update)
action initialization
 How it works:
 execute initialization
false
statement condition
 while condition is true
true
 execute action
 execute update action

update

11
Yet Another Loop Statement

 Syntax
do action
while (condition)
action
 How it works:
 execute action
 if condition is true then execute action again
 repeat this process until condition evaluates to false. true
condition
 action is either a single statement or a group of
statements within braces. false

12
Example: Get 5 numbers from the user and
add them

13
Example: Diamond Pattern

14
Arrays

 Array is a data structure that represents a collection of


variables of the same data type
 Once an array is created, its size is fixed
 The following will create an array of size 5
int c[5];
We can directly initialize the same array as
int c[]={1,2,3,4,5};

15
Using the Shorthand Notation

double[] myList = {1, 9, 3, 5};


This shorthand notation is equivalent to the following statements:
double[] myList = new double[4];
myList[0] = 1;
myList[1] = 9;
myList[2] = 3;
myList[3] = 5;

16
Arrays Cont..

 Example

double[] myList = new double[10];

myList reference myList[0]


myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]

17
Initializing Arrays

 Using a loop:
for (int i = 0; i < 5; i++)
myList[i] = i;

18
Example Sorting

19
String array

20
Functions

21
Pass by Value Example

22
Pass by Reference Example A reference variable is an alias
for another variable. To declare a
reference variable, place the
ampersand ( & ) in front of the
variable or after the data type for
the variable

23
Pointers

24
Using & to get address of a variable

25
Pointers

 Pointer variables, simply called pointers, are


declared to hold memory addresses as their values.
 Although we can have different pointer types, what
is stored in a pointer variable is a 4-byte address.
 In other words, the size of a pointer variable is fixed
in C++, which means that a pointer variable can
have an address from 0x00000000 to 0xFFFFFFFF.

26
27
Example: Pointers

28
Example: Pointers and Arrays

29
Example: Pointers as Function Parameters

30
Example: Passing Arrays to Functions

Are arrays passed by value or


by reference?

31
Dynamic Memory allocation: new and delete

32
That is all for Week 1

33

You might also like