C++ Lecture Four
C++ Lecture Four
Programming Fundamentals
Control Structures
(Selections)
x = x+1; x += 1 x++
x = x-1; x -= 1 x--
◼ Example
◼ Compute the following expression where: z=3, x=2, y=2
احسب التعبير التالي حيث ◼
z+= ++x - y--;
Solution:
z=4?
Condition false
true
statements
continue
2- Algorithm Design
ALGORITHM Double
INPUT n
IF ( n < 50 ) THEN
OUTPUT “The double value is “ , n * 2
END IF
OUTPUT “ Finished”
END
#include <iostream>
using namespace std;
void main ( )
{
int n ;
cin >> n ;
if (n < 50)
cout << “The double value is “ << n * 2 << endl ;
cout << “ Finished “ << endl;
}
1- Analysis stage:
◼ Problem Input:
- Two integers, say num1 and num2
◼ Problem Output:
- The smaller number
◼Criteria المعايير
Print the smaller number using only one output statement
.50 إذا كان المجموع أكبر من، اكتب خوارزمية تأخذ ثالثة أعداد صحيحة وتطبع متوسطها
1- Analysis stage:
◼ Problem Input:
- Three integers, say n1, n2, n3
◼ Problem Output:
- The average of the three numbers, or nothing
◼Criteria
Calculate the average only if the summation of the numbers is over 50.
false true
condition
statements2 statements1
continue
1- Analysis stage:
◼ Problem Input:
- Two integers, say num1 and num2
◼ Problem Output:
- The smaller number
#include <iostream>
using namespace std;
void main ( )
{ int num1, num2 ;
cin >> num1 >> num2 ;
if ( num1 < num2 )
cout << “The smaller value is “ << num1 << endl ;
else
cout << “The smaller value is “ << num12 << endl ;
}
◼ Problem Output:
- The total price of the two items
◼ Criteria
check whither the total prices is over 100 or not.
True
Condition1 Statements1
False
True
Condition2 Statements2
Rest of
False algorithm
True
Condition3 Statements3
False
Statements4
◼ Example 4:
Write an algorithm that inputs a student mark and outputs the
corresponding grade, where grades are as follows:
: حيث تكون الدرجات كما يلي، اكتب خوارزمية تُدخل عالمة الطالب وتخرج التقدير المقابل
mark grade
90-100 A
80-89 B
70-79 C
60-69 D
< 60 E
◼ Problem Output:
- grade
◼ Criteria
- according to the previous grade table
b<c? c<b?
Note here, that the result of the in-line condition, should match the data
type of the variable at the left-hand side of the assignment statement
switch (grade)
Display
“Excellent”
Display
“Good”
…
Default :
“……..”
}
Programming Fundamentals (Lecture Four) 60
The End