Lecture 1-Week1 - Structured Programming (Revision)
Lecture 1-Week1 - Structured Programming (Revision)
(CS1143)
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
4
Input and Output Statements
5
Input and Output Statements Cont..
6
Comments
Multiple-line comments
Enclosed between /* and */
7
A Choice Statement
Syntax
if(condition)
false
condition
action
8
Another Choice Statement
Syntax
if (condition)
true false
Action_A condition
else
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
15
Using the Shorthand Notation
16
Arrays Cont..
Example
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
26
27
Example: Pointers
28
Example: Pointers and Arrays
29
Example: Pointers as Function Parameters
30
Example: Passing Arrays to Functions
31
Dynamic Memory allocation: new and delete
32
That is all for Week 1
33