Lecture 02 - Control structures
Lecture 02 - Control structures
Outline
• Introduction
• Control structures
• if single-selection statement
• if…else double-selection statement
• while repetition statement
• Counter-controlled repetition
• Sentinel-controlled repetition
• Nested control statements
• Compound assignment operators
• Increment an decrement operators
• for repetition as counter-controlled repetition
• do…while repetition
• switch multiple-selection statement
• break…continue statement
• logical operators
Control structures
• Sequential execution
– Statements execute one after another
• Transfer of control
– Can specify the next element to execute
– Not necessarily to be the next one in sequence
• Bohm and Jacopini’s work demonstrated that all
apps could be written in terms
– Sequence structure
– Selection structure
– Repetition structure
Activity diagram
• Activity diagram help you to develop and
represent algorithms
– It can shows clearly how control structures work
• The main components are
– Start: solid circle
– Finish: solid circle surrounded by hollow circle
– Action states (contains action expression): rounded
rectangle
– Transition arrows (flow of activity): arrows
– Branching: diamonds
Sequence structure
Selection structures
• Selection structures
– Allow to run a block of statements depending on
whether a condition is true or false
• There are three main types of selection
statements
– if statement: single-selection statement
– if…else statement: double-selection statement
– switch statement: multiple-selection statement
Repetition structures
• Repetition structures (also called iteration
structures or loop structures)
– Perform statements repeatedly depending on a
condition
• There are three main repetition structures
– while
– do…while
– for (and foreach)
Structured programming
• All app can be built from set of control structures
described
– With control statement stacking
– with control statement nesting
• Control statement stacking
– Single-entry/single-exit control statements make it easy to
build apps
– Control statements are “attached” to one another by
connecting the exit point of one to entry point of anther
• Control-statement nesting
– One control statement appears inside another
if single-selection statement
if…else Double-selection statement
Conditional operator (?:)
• A ternary operator, can be used in place of
if…else
– (conditional expression ? true expression : false
expression)
– e.g., gradeStr = (grade >= 60 ? “Passed” : “Failed”);
if…else if ladder
• Instead of nesting many levels of if…else, we
could use if…else if ladder
while Repetition
Counter-controlled repetition
• Counter-controlled repetition
– Uses a counter (control variable) to control the
number of times statements will execute
– It is definite repetition (# of repetitions are known
in advanced)
Activity: Average grade
Add a method DetermineClassAverage() to enter and calculate the average of the grades
Sentinel-control Repetition
• The number of loops is controlled by a sentinel
value (signal value, dummy value, flag value)
• This is also called indefinite repetition
– The number of repetitions is not known in advanced
• Sentinel value should be chosen so it is not
confused with the input value
– E.g., repeatedly input the grade until inputting -1
– -1 is the sentinel value, different from normal grades
Activity: Sentinel-controlled repetition
• Develop the class average program
– However, allows user to input the grade until user
input -1 to finish
– Calculate the class average
Nested control statements: Activity
• Create a program to analyze results of exam
– Input 10 test results, each as 1: passed or 2: failed
– Count number of test results of each type
– Display a summary (total of passed, failed)
– If more than eight students passed, display a
bonus message
Nested control statements: Activity
Nested control statements: Activity
Compound assignment operator
• Compound assignment operators
– For abbreviating assignment expressions
• Any expression of form
– variable = variable operator expression;
• Can be written
– variable operator= expression;
Arithmetic compound assignment
operators
Increment and Decrement operators
• There are two main unary operators
– Adding 1, increment operator, ++
– Subtracting 1, decrement operator, --
• They can be used as
– Prefix increment/decrement operator
– Postfix increment/decrement operator
Increment and Decrement operators
General form of Switch statement
switch(expression){
case item1:
//one or more statements;
break;
case item2:
//one or more statements;
break;
case itemn:
//one or more statements;
break;
default:
//one or more statements;
break;
}
for statement
• It is counter-control statement
– While loop is more natural
– For loop can implement this in single line
General form of a for loop
• General form
for (expression1; expression2; expression3 ){
// one or more statements;
}
• Expression1
– Initializes counter/index variable
– Is usually an assignment statement
– Is executed once at the beginning (only once)
• Expression 2
– Sets terminating condition
– Is evaluated at the beginning of every iteration
– If true statements inside the loop is executed; if false exit the loop
• Expression 3
– Is the loop variant/modifier (increment/decrement)
– Is evaluated at the end of every iteration
For statement
?
for (initialization ; condition ; increment / decrement ){
true
}
For statement
?
for (initialization ; condition ; increment / decrement ){
true
}
For statement
?
for (initialization ; condition ; increment / decrement ){
true
}
For statement
?
for (initialization ; condition ; increment / decrement ){
false
// one or more statements;
}
For loop example - requirement
• Make a program to display this message, using for
loop
This is a
nice
nice
nice
nice
nice
nice
world!
Flow chart
Activity: Code prev. example
The “do-while” loop
• Syntax
do {
//one or more statements;
} while (condition is true);
– Program will always execute the loop body first
– The body is always executed at least once
– At the end of every loop, condition is evaluated
– If it’s true, the loop continues
– If it’s false, the loop finishes
– The do-while statement must be terminated with ‘;’
Activity: do-while loop
• Using do-while loop, please:
– Make a program to display odd integers in the
range 1 to 10
– Make another program to display even integers in
the range 1 to 10
The break statement
• The break statement can appear in the switch
statement and the loop statement
– It causes the execution of the current enclosing
switch case or the loop to terminate
– When it is encountered, all the rest of the codes
after the break inside the code block are skipped
– General syntax: break;
Activity – break example
• Write a program to display numbers from 1 to
10 and break before 5
The continue statement
• The continue statement can only appear in
the the loop statement
– It’s used to terminate the current iteration only
– It skips the rest of the statements in the body of
the loop and begins next iteration
– General syntax: continue;
Activity – continue example
• Write a program to display numbers from 1 to
10 and skip at 5
Activity: factorial – requirements
(Homework)
• Make a program to find factorial number from
an input integer
• Specified as
– If n is < 0 display invalid input
– If n = 0 or 1, n!=1
– Otherwise, n!= 1*…*i; where i<=n
Activity: factorial - requirements
• Input:
– A positive integer
• Process:
– If n = 0 or 1, n!=1
– Otherwise, n!= 1*…*n;
• Output:
– The factorial of the input integer
Flowchart
Find factorial number
algorithm
Activity: factorial - code
• Now write code for this activity (find factorial
number)
References
Deitel, P. & Harvey, D., 2013. C# 2012 for
Programmers. 5th ed. Prentice Hall.