Dr.-Ing. A. Diab
Computer Engineering and Automation Research Group
Faculty of Mechanical and Electrical Engineering
Prof. Dr.-Ing. habil. Andreas Mitschele-Thiel
Al-Baath University
Integrated HW/SW Systems Group
Self-Organization
04 March 2014
Page 1
1
Outline
Flow of Programs
Conditional Branching (If Statement)
Repetitions (Loops)
While Repetition Structure
Do/While Repetition Structure
For Repetition Structure
Flow of Programms
Sequencial
No branching in executing the code and no jumbs
Conditional Branching
Executeing certain code blocks based on specific conditions
Statements
If, Switch
Iterations Structures
Iterate some code for
given times (using counters) or
until a given condition gets true
Statements
While, Do/While, For
Jump Structures
Jump to another address and start executing codse from there
Statements
Return, Goto, exit(), Break, Continue
Conditional Branching
IF Statement
Used to execute a set of code when a given condition is true
Syntax
If (expression)
{ Statements }
Example
#include <iostream.h>
void main()
{
int a=100; int b=200;
if (b>a)
{ cout << "B is greater than A"; }
}
IF-Else Statement
Used to execute a set of code when a given condition is true.
Otherwise, the code in the "else" part is executed
Syntax
If (expression)
{ Statements }
else
{ Statements }
Nested IF
If statements inside each other
Syntax
If (expression)
{ Statements
(may include other if statements)
}
else
{ Statements
(may include other if statements)
}
Switch Statement
Compares the value of an expression against a list of integers or
character constants
The list of constants are listed using the "case" statement along with a
"break" statement to end the execution
If no conditions match then the code under the default statement is
executed
Syntax
Switch (expression)
{
case constant1: Statements; break;
case constant2: Statements; break;
..
default: Statements;
}
Iteration Structures
Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
10
True
Statement
Condition
False
Example
int x = 2;
while (x >= 0) {
if ( x == 2) {
cout << Value of x is : << x << endl;
}
x = x 1;
}
11
Pseudocode
Set total_grade and grade_counter to zero
While grade_counter <= 10
Input the next grade
Add the grade into the total_grade
Increment grade_counter
Average = total_grade / 10
Print average
12
13
Example
int counter = 2;
Int ItemsToBy = 10;
For ( counter = 1 ; ItemsToBy; counter ++ )
cout << the item number << counter << ha been purchased;
14
Initialization variable
Condition
True
Statements
Increment variable
False
15
16
17