Sunteți pe pagina 1din 17

Control Structures in C++

Dr.-Ing. Ali Diab

Programming I Electronics and Communication

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 }

Example (Prog 2_1)


#include <iostream.h>
void main()
{
int a=10; int b=20;
if (a>b)
{ cout << "A is greater than B"; }
else
{ cout << "B is greater than A"; }
}

Nested IF
If statements inside each other
Syntax
If (expression)
{ Statements
(may include other if statements)
}
else
{ Statements
(may include other if statements)
}

Example (Prog 2_2)

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;
}

Example (Prog 2_3)


8

Iteration Structures

While Repetition Structure


Repetition structure
Programmers specify an action to be repeated while a given condition
remains true
Psuedocode
While there are more items on my shopping list, purchase next item and
cross it off my list

While loop repeated until condition becomes false

Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;

10

While Repetition Structure


Flowchart of While loope

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

While Repetition Structure


Example
A class of ten students took a quiz. The grades (integers in the range
0 to 100) for this quiz are available. Determine the class average on
the quiz. The user types in the grades one after one.

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

While Repetition Structure


The code in C++ (Prog2_4)
#include <iostream.h>
int main()
{
int grade_counter = 10; int total_grade = 0; int grade = 0;
float average_grade = 0;
cout << "The program calculates the average grade of 10 students.\n";
while (grade_counter > 0)
{
cout << "Type in a grade: ";
cin >> grade;
cout << "\n";
total_grade += grade;
grade_counter--;
}
average_grade = total_grade/10;
cout << "The average grade of the student class is: " << average_grade << endl;
}

13

For Repetition Structure


Repetition structure
Programmers specify an action to be repeated for a certain amount of
times
Psuedocode
Start from the first item on my shopping list and purchase next item, cross
it off my list and repeat the action until you by 10 items

For loop induces a condition to define the loope endpoint

Example
int counter = 2;
Int ItemsToBy = 10;
For ( counter = 1 ; ItemsToBy; counter ++ )
cout << the item number << counter << ha been purchased;

14

For Repetition Structure


Flowchart of For loope

Initialization variable

Condition

True

Statements

Increment variable

False

15

For Repetition Structure


Example
Write a program with similar output

16

Hope you will enjoy learning during the


course

17