Review of Program Structures

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

UNIT

1 Review of Program Structures

IT 105 Computer Programming 2


LESSON 1
Program Structures – Sequential
Steps are performed in strictly sequential manner, each step being executed exactly
once.

IT 105 Computer Programming 2


LESSON 2
Program Structures – Selection
Conditional statements are features of programming language to
construct a selection structure. These are statements used to
perform a certain task which depend on whether the conditional
expression specified evaluates to true or false.

C++ supports two types of conditional statements:


if and switch statements

IT 105 Computer Programming 2


LESSON 2
Program Structures – Selection
Conditional statements are features of programming language to
construct a selection structure. These are statements used to
perform a certain task which depend on whether the conditional
expression specified evaluates to true or false.

C++ supports two types of conditional statements:


if and switch statements

IT 105 Computer Programming 2


LESSON 2
Program Structures – Selection
if statement if -else statement
(one option) (two options)

if (conditional expression) if (conditional expression)


{ {
statement 1;
. statement;
.
.
. }
statement N; else {
statement;
}
}

IT 105 Computer Programming 2


LESSON 2
Program Structures – Selection
Ladderized if - else statement
(three or more options)

if (conditional expression1)
{
statement;

}
else if (conditional expression2)
{
statement;

}
else {
statement;

} IT 105 Computer Programming 2


LESSON 2
Program Structures – Selection
Nested if/ if - else statement
if (conditional expression1) if (conditional expression1)
{ {
if (conditional expression 1.1)
if (conditional expression 1.1) {
{ statement;
statement;
}
} else {
else { statement;
statement; }
}
}
} else
{
if (conditional expression 2.1)
{
statement;

}
else {
statement;
}
} IT 105 Computer Programming 2
LESSON 2
Program Structures – Selection
switch (variable_expression)
switch statement {
case constant1:{
statement sequence_1;
break;
}
case constant2:{
statement sequence _2;
break;
}
default: {
statement sequence _3;
break;
}
}
IT 105 Computer Programming 2
LESSON 3
Program Structures – Repetition

Repetitive /Iterative statements (loops) allow a set of instructions to be


repeated or carried out several times until certain conditions have been
met.
Iterative statements (loops) allow a set of instructions to be repeated or
carried out several times until certain conditions have been met. It can
be predefined(count controlled) as in in the for loop, or open
ended(condition controlled) as in while and do while.

IT 105 Computer Programming 2


LESSON 3
Program Structures – Repetition
Count-controlled loop

for statement

for (initialization; condition; increment)


{
statement(s);
}

IT 105 Computer Programming 2


LESSON 3
Program Structures – Repetition

Condition-controlled loop

while statement do-while statement

while (condition) do
{ {
statement; statement;
}
}while (condition);

IT 105 Computer Programming 2

You might also like