You are on page 1of 24

Computer Programming

06 - LOOPING (2)
Basic Control Structure: Sequential
Statement in computer
programming is an
instruction which will be
evaluated and executed by
a computer
The body of a method
contains one or more
statements
Sequentially: computer
executes each statements
one after another
Basic Control Structure: Conditional
Sequential Control Structure
Computer executes each statements one after another
Conditional Control Structure
Allow computer to choose between (two) alternative executions
(C statement: if, if-else, multiway-if-else, and/or switch)
Basic Control Structure: Conditional
Syntax for if statement:
Basic Control Structure: Double Selection
Statement
Choose between two statements/block statements,
depend on a given condition
Basic Control Structure: Double Selection
Statement
Choose between two statements/block statements,
depend on a given condition
Basic Control Structure: Multiple
Selection Statement
Choose among many statements/block statements,
depend on a given condition
Basic Control Structure: Multiple
Selection Statement
Example for nested if:
Basic Control Structure: Multiple
Selection Statement
Example for multiway if-else:
Basic Control Structure: Repetition
Two types of iteration/repetition in computer
programming:
You know exactly how many times you have to repeat
You know the specific condition when to stop the
repetition, but you do not know how many times to
repeat the action to achieve that particular condition
Iterative control structure: allow computer to
repeat execution of statement.
C provides: while, do..while, for
Basic Control Structure: Repetition
Syntax for while statement:

Statement(s) inside the block will be executed


while the expression returns true
Basic Control Structure: Repetition
Basic Control Structure: Repetition
Syntax for do..while statement:

Statement(s) inside the block will be executed


while the expression returns true
Basic Control Structure: Repetition
Basic Control Structure: Repetition
Syntax for for statement:

Initialization: one or more statements which are executed only one time in the
beginning of the iteration. The counter/iterator usually initialize in the
initialization
Expression: decide wether the block statement will be executed or not. If the
expression returns false, then the whole for statement is terminated and the
program will evaluate the next statement.
Updater: statement which is executed after the block statement is executed.
The counter/iterators value usually updated in the updater
Advanced Control Structure: Introduction
Basically, different control structures can be combined
in order to create a complex program, which is used to
solve difficult problems
Advanced Control Structure: Putting It All Together
Nested Conditional Structure:
Given 3 numbers, find the max
number of them!
Advanced Control Structure: Putting It All Together
Combination between conditional and repetition:
Given an integer , 101 < < 101:
If is a positive number, display the output string "1 2 3
If is a negative number, display the output string "N N1 N2
2 1"
If is zero, display the output string 0
Advanced Control Structure: Putting It All Together
Combination between conditional and repetition:
Given an integer , 0 < :
If is an even number, display the output string "2 4 6 N"
If is an odd number, display the output string "1 3 5 "
Advanced Control Structure: Putting It All Together
Combination between conditional and repetition:
Write a program to find wether a number is a prime number or a
composite number.
Advanced Control Structure: Putting It All Together
Nested Repetition Structure:
Write a program to draw a pattern like:
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
Advanced Control Structure: Putting It All Together
Nested Repetition Structure:
Write a program to draw a pattern like:
@
@@
@@@
@@@@
@@@@@
Advanced Control Structure: Putting It All Together
Nested Repetition Structure:
Write a program to draw a pattern like:
11111
22222
33333
44444
55555
Advanced Control Structure: Putting It All Together
Nested Repetition Structure:
Write a program to draw a pattern like:
55555
4444
333
22
1

You might also like