شاهد المحاضرة في فيديو مدته حوالي ٤ دقائق مع أمثلة تشتغلها بنفسك واختبار قصير، أو افتح «المحاضرة المكتوبة»: كل شريحة في صفحة بنفس نص المحاضرة الأصلية وترتيبها، وتنقّل بالأسهم أو بالسحب.
شرح صوتي مبسّط باللهجة المصرية مع المصطلحات التقنية بالإنجليزية، ومشاهد متحركة تلخص كل أفكار المحاضرة بالترتيب.
المدة: ٣ دقائق و٤٥ ثانية. المحاضرة بنفس ترتيب الشرائح الأصلية، وللنص الكامل حرفيًا افتح تبويب «المحاضرة المكتوبة».
أمثلة الفيديو
أمثلة الكود — عدّل وشغّل بنفسك
أمثلة المحاضرة جاهزة للتجربة هنا. عدّل أي مثال واضغط «شغّل الكود» ليُترجم ويعمل مباشرة عبر خدمة Wandbox المجانية (مترجم GCC 13). يُرسل الكود إلى تلك الخدمة عند التشغيل.
المثال ١: مرتب موظف بالساعة (if-else)
جرّب ساعات ١٠ وسعر الساعة ٥٠٠ (كل قيمة في سطر)، ثم جرّب ٥٠ ساعة وسعر ١٠٠ ولاحظ فرق الوقت الإضافي.
The output appears here after you run the code.
Runs on the free Wandbox service (wandbox.org), GCC 13.2.0. No account needed.
المثال ٢: High أو Low (عوامل منطقية)
يدخل الـ score وعدد الـ red cards ويطبع High لو الدرجة أكبر من ١٠٠ والكروت أقل من ٣. جرّب ١٢٠ ثم ٢.
The output appears here after you run the code.
Runs on the free Wandbox service (wandbox.org), GCC 13.2.0. No account needed.
المثال ٣: تقدير الطالب (else if)
سلم else if كامل: يدخل الدرجة ويطبع التقدير من A لـ F. جرّب ٧٧ ثم غيّرها.
The output appears here after you run the code.
Runs on the free Wandbox service (wandbox.org), GCC 13.2.0. No account needed.
اختبار قصير
اختبر فهمك بعد المشاهدة
اختبر نفسك
What does a branch let a program do?
count = count + 2; can be written shorter as:
What does cout << (10 > 5); print?
In an if-else statement, when is the else statement executed?
Declare a variable exist, and initialize it to true.
Declare an int constant, week, whose value is 7.
الملخص بالعربيةمراجعة على المحاضرة السابقة: عدد الأسطر التي تطبعها جملة cout مع الـ escape sequences، ثم ثلاث مهام إعلان: اسم موظف، ومتغير exist بقيمة ابتدائية true، وثابت int اسمه week قيمته ٧.
اختبر نفسك
How many lines does that cout statement print?
الشريحة ٣
Outlineخطة المحاضرة
Designing branch
if-else statements
Comparison operator
Logical operator
Nested if-else statement
Switch statement
الملخص بالعربيةخطة المحاضرة: تصميم الفروع، ثم جمل if-else، وعوامل المقارنة، والعوامل المنطقية، والجمل المتداخلة، وأخيرًا جملة switch.
الشريحة ٤
Operator Shorthandاختصارات العمليات الحسابية
Some expressions occur so often that C++ contains to shorthand operators for them
All arithmetic operators can be used this way
+= count = count + 2; becomescount += 2;
*= bonus = bonus * 2; becomesbonus *= 2;
/= time = time / rushFactor; becomestime /= rushFactor;
الملخص بالعربيةبعض التعبيرات تتكرر كثيرًا فوفّرت لها ++C عوامل مختصرة: count += 2 بدل count = count + 2، ونفس الفكرة مع *= و/= و%=. كل العمليات الحسابية تصلح بهذه الطريقة.
الشريحة ٥
Simple Flow of Controlسير التحكم البسيط
Flow of control: The order in which statements are executed
Branch: Lets program choose between two alternatives
الملخص بالعربيةFlow of control هو ترتيب تنفيذ الجمل في البرنامج، والـ branch يتيح للبرنامج الاختيار بين بديلين.
الشريحة ٦
Branch Exampleمثال على الفرع
Suppose you want to design a program to compute a week’s salary for an hourly employee
To calculate hourly wages there are two choices:
Regular time (up to 40 hours): totalSalary = rate * hours;
If hours > 40 is true, then use totalSalary = rate * 1.5 * hours;
If it is not true: totalSalary = rate * hours;
الملخص بالعربيةالشرط hours > 40: لو صحيح يُحسب المرتب بأجر ونص rate * 1.5 * hours، ولو غير صحيح يُحسب بالأجر العادي rate * hours.
الشريحة ٨
Branch Example: the full programالبرنامج كاملًا مع التشغيل
#include<iostream>
using namespace std;
int main()
{
//compute a week's salary for an hourly employee
int hours;
double rate;
double totalSalary;
cout<<"enter total hours: \n";
cin>>hours;
cout<<"enter rate \n";
cin>>rate;
if(hours>40)
totalSalary=rate * 1.5 *hours;
else
totalSalary=hours*rate;
cout<<"total salary:"<<totalSalary;
}
Run 1: hours = 10, rate = 500 → total salary:5000
Run 2: hours = 50, rate = 100 → total salary:7500
الملخص بالعربيةبرنامج المرتب كاملًا كما في المحاضرة: يقرأ الساعات والسعر ويختار الحسبة المناسبة. عند إدخال ١٠ ساعات وسعر ٥٠٠ الناتج ٥٠٠٠، وعند ٥٠ ساعة وسعر ١٠٠ الناتج ٧٥٠٠ لأن الوقت الإضافي بأجر ونص. جرّبه بنفسك في تبويب «فيديو المحاضرة».
الشريحة ٩
if-else Flow Controlالتحكم في السير بـ if-else
if (boolean expression)
true statement
else
false statement
When the boolean expression is true
Only the true statement is executed
When the boolean expression is false
Only the false statement is executed
الملخص بالعربيةصيغة if-else: شرط منطقي بين قوسين، ثم جملة الـ true، ثم else وجملة الـ false. عند تحقق الشرط تُنفذ جملة الـ true فقط، وعند عدم تحققه تُنفذ جملة الـ false فقط.
الشريحة ١٠
Comparison operatorعوامل المقارنة
Operator
Example
True Condition
False Condition
==
A == B
A equals B
A is not equal to B
!=
A != B
A is not equal to B
A equals B
>
A > B
A is greater than B
A is not greater than B
<
A < B
A is less than B
A is not less than B
>=
A >= B
A is greater than or equal to B
A is less than B
<=
A <= B
A is less than or equal to B
A is greater than B
الملخص بالعربيةجدول عوامل المقارنة الستة كما في المحاضرة: == للتساوي، != لعدم التساوي، > أكبر من، < أصغر من، >= أكبر من أو يساوي، <= أصغر من أو يساوي. انتبه: المقارنة بعلامتي يساوي، وعلامة واحدة تعني التعيين.
الشريحة ١١
Find the output for the following programأوجد ناتج البرنامج
#include<iostream>
using namespace std;
int main()
{
//using comparison operator
int ahmedAge=10;
int omarAge=5;
//not that returns 1 (true) and return 0 means(false)
cout<<"is Ahmed older than Omar:"<<(ahmedAge>omarAge)<<endl;
cout<<"is Ahmed younger than Omar:"<<(ahmedAge<omarAge)<<endl;
cout<<"is Ahmed age equal to Omar age:"<<(ahmedAge==omarAge)<<endl;
cout<<"is Ahmed age not equal to Omar age:"<<(ahmedAge!=omarAge)<<endl;
cout<<"is Ahmed age older than or equal to Omar age:"<<(ahmedAge>=omarAge)<<endl;
cout<<"is Ahmed age younger than or equal to Omar age:"<<(ahmedAge<=omarAge)<<endl;
return 0;
}
الملخص بالعربيةتمرين المحاضرة: برنامج يقارن ahmedAge=10 بـ omarAge=5 بكل عوامل المقارنة. لاحظ أن النتيجة تُطبع ١ بمعنى true و٠ بمعنى false. توقّع الناتج قبل قلب الصفحة.
الشريحة ١٢
The solutionالحل
is Ahmed older than Omar:1
is Ahmed younger than Omar:0
is Ahmed age equal to Omar age:0
is Ahmed age not equal to Omar age:1
is Ahmed age older than or equal to Omar age:1
is Ahmed age younger than or equal to Omar age:0
الملخص بالعربيةالحل كما ظهر في تشغيل المحاضرة: أكبر: ١، أصغر: ٠، يساوي: ٠، لا يساوي: ١، أكبر أو يساوي: ١، أصغر أو يساوي: ٠. كل مقارنة صحيحة تطبع ١ وكل خاطئة تطبع ٠.
الشريحة ١٣
Logical operatorالعوامل المنطقية
Boolean expressions can be combined into more complex expressions with the following operator
Syntax: (Comparison_1) && (Comparison_2)
Example:if ( (2 < x) && (x < 7) )
Assume A=1, B=1, C=0, D=0
Operator
Behavior
Example
&&
Logical And: if both inputs are non-zero the outcome of the operation is true
A && B = 1 A && C = 0
||
Logical Or: if at least one input is non-zero, the outcome of the operation is true
A||B = 1 A||C = 1 C||D = 0
!
Logical Not: negates the logical condition
!(A&&B) = 0 !(C||D) = 1
الملخص بالعربيةالعوامل المنطقية تربط التعبيرات المنطقية في تعبيرات أعقد: && (And) تصح فقط لو المدخلان غير صفريين، و|| (Or) تصح لو مدخل واحد على الأقل غير صفري، و! (Not) تعكس الشرط. الصيغة: (Comparison_1) && (Comparison_2) مثل if ( (2 < x) && (x < 7) ).
الشريحة ١٤
Find the output for the following program:أوجد ناتج البرنامج
#include<iostream>
using namespace std;
int main()
{
//using comparison operator
int numb1=10;
int numb2=5;
//not that returns 1 (true) and return 0 means(false)
cout<<"Number1 <20 and Number2 >3 is "<<(numb1<20&&numb2>3)<<endl;
cout<<"Number1 >15 and Number2 >3 is"<<(numb1>15&&numb2>3)<<endl;
cout<<"Number1 <11 and Number2 <2 is"<<(numb1<11&&numb2<2)<<endl;
cout<<"Number1 <11 or Number2 <2 is "<<(numb1<11||numb2<2)<<endl;
cout<<"Number1 <11 not and Number2 <2 is "<<!(numb1<11&&numb2<2)<<endl;
return 0;
}
الملخص بالعربيةتمرين: برنامج يجمع المقارنات بالعوامل المنطقية على numb1=10 وnumb2=5 باستخدام && و|| و!. توقّع ناتج كل سطر قبل قلب الصفحة.
الشريحة ١٥
The solution:الحل
Number1 <20 and Number2 >3 is 1
Number1 >15 and Number2 >3 is0
Number1 <11 and Number2 <2 is0
Number1 <11 or Number2 <2 is 1
Number1 <11 not and Number2 <2 is 1
الملخص بالعربيةالحل: الأول ١ (الشرطان صحيحان)، الثاني ٠ (١٠ ليست أكبر من ١٥)، الثالث ٠ (٥ ليست أصغر من ٢)، الرابع ١ مع or لأن شرطًا واحدًا يكفي، والخامس ١ لأن ! تعكس الناتج.
الشريحة ١٦
Write an if-else statementاكتب جملة if-else
Write an if-else statement that: outputs the word High if the value of the variable score is greater than 100 and variable red cards are less than 3
Then output the word Low if the value of the score is at most 100 or red cards are more than 3. The variables are of type int.
الملخص بالعربيةاكتب جملة if-else تطبع High لو الـ score أكبر من ١٠٠ وعدد الـ red cards أقل من ٣، وتطبع Low لو الدرجة ١٠٠ على الأكثر أو الكروت أكثر من ٣. المتغيران من نوع int.
الشريحة ١٧
The solutionالحل
#include<iostream>
using namespace std;
int main()
{
int score;
int redCards;
cout<<"enter the score \n";
cin>>score;
cout<<"enter total number of red cards \n";
cin>>redCards;
if(score>100&&redCards<3)
cout<<"High";
else
cout<<"Low";
return 0;
}
Run: score = 120, red cards = 2 → High
الملخص بالعربيةالحل بربط الشرطين بـ &&: if(score>100&&redCards<3) تطبع High وإلا Low. عند إدخال ١٢٠ ثم ٢ يظهر High. جرّبه بنفسك في تبويب «فيديو المحاضرة».
الشريحة ١٨
Nested if-else Statementsجمل if-else المتداخلة
Branching mechanisms can be a subpart of another branching mechanism
An if-else-statement can include another if-else-statement as a subpart
Example:
if (count < 10)
if ( x < y)
cout << x << " is less than " << y;
else
cout << y << " is less than " << x;
الملخص بالعربيةآليات التفريع يمكن أن تكون جزءًا من تفريع آخر: جملة if-else قد تحتوي جملة if-else أخرى بداخلها. المثال: داخل if (count < 10) توجد جملة ثانية تقارن x بـ y وتطبع الأصغر.
الشريحة ١٩
Use nesting if-else-statements to solve the followingتمرين بالجمل المتداخلة
Design an if-else statement to warn a driver when fuel is low, but tell the driver to bypass pit stops if the fuel is close to full. Otherwise, there should be no output.
الملخص بالعربيةصمّم جملة متداخلة تحذّر السائق عند انخفاض الوقود: لو الوقود أقل من ٣/٤ انظر لو أقل من ١/٤ فاطبع تحذيرًا، وإلا فلا إخراج. ولو الوقود أكبر من ٣/٤ اطبع عبارة تقول لا تتوقف.
الشريحة ٢٠
The solutionالحل
double fuel;
cout<<"enter fuel value";
cin>>fuel;
if(fuel < 0.75)
if(fuel < 0.25)
cout << "Fuel very low.Caution!\n";
else
cout<<"Fuel less than 3/4. but its Ok you can go\n";
else
cout<<"Fuel over 3/4. Don't stop now!\n";
Run: fuel = 0.22 → Fuel very low.Caution!
الملخص بالعربيةالحل بالتداخل: الشرط الخارجي fuel < 0.75 وبداخله fuel < 0.25 للتحذير. عند إدخال ٠٫٢٢ ظهر Fuel very low.Caution!.
الشريحة ٢١
Nested if-elseإعادة كتابة المثال
The previous example can be written as
if(fuel > 0.75)
cout<<"Fuel over 3/4. Don't stop now!\n";
else if(fuel < 0.25)
cout << "Fuel very low.Caution!\n";
else
cout<<"Fuel less than 3/4. but its Ok you can go\n";
الملخص بالعربيةيمكن كتابة المثال السابق بصيغة مسطحة: if ثم else if ثم else؛ كل حالة على مستوى واحد بدل التداخل.
الشريحة ٢٢
Nested if-else Syntaxصيغة if-else متعددة الاتجاهات
A Multiway if-else statement is written as
if(Boolean_Expression_1)
Statement_1
else if ( Boolean_Expression_2)
Statement_2
...
else if (Boolean_Expression_n)
Statement _n
else
Statement_For_All_Other_Possibilities
الملخص بالعربيةالصيغة العامة للجملة متعددة الاتجاهات: if ثم أي عدد من else if بشروطها، وتنتهي بـ else تغطي كل الاحتمالات الأخرى.
الشريحة ٢٣
Write an if-else statement: the student gradeتمرين: تقدير الطالب
Write an if-else statement that: outputs the student’s grade if the value of the variable score is given by the user
#include <iostream>
using namespace std;
int main() {
int score;
char grade;
cout << "Enter the student's score: ";
cin >> score;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
cout << "The student's grade is: " << grade <<endl;
return 0;
}
Run: score = 77 → The student's grade is: C
الملخص بالعربيةحل تمرين التقديرات بسلم else if: من ٩٠ فأكثر A، من ٨٠ B، من ٧٠ C، من ٦٠ D، وإلا F. عند إدخال ٧٧ ظهر C. جرّبه بنفسك في تبويب «فيديو المحاضرة».
الشريحة ٢٤
The switch-statementجملة switch
The switch-statement is an alternative for constructing multi-way branches
switch (controlling expression)
{
case Constant_1:
statement_Sequence_1
break;
case Constant_2:
Statement_Sequence_2
break;
...
case Constant_n:
Statement_Sequence_n
break;
default:
Default_Statement_Sequence
}
الملخص بالعربيةجملة switch بديل لبناء الفروع متعددة الاتجاهات: تعبير تحكم، ولكل قيمة case بجملها وbreak، وdefault للحالات غير المطابقة.
الشريحة ٢٥
The switch-statement: notesملاحظات على switch
The break statement ends the switch-statement
Omitting a break statement allows the use of multiple case labels for a section of code
If no case label has a constant that matches the controlling expression, the statements following the default label are executed
It is a good idea to include a default section
Switch-statements can make some code more clear
A menu is a natural application for a switch-statement
الملخص بالعربيةملاحظات: break تنهي الـ switch، وحذفها يسمح باستخدام أكثر من case لنفس الجزء من الكود. عند عدم تطابق أي case تُنفذ جمل default، ومن الأفضل دائمًا تضمينها. القوائم تطبيق طبيعي للـ switch.
الشريحة ٢٦
Example: the day of the weekمثال: أيام الأسبوع
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "it is not a day";
}
الملخص بالعربيةمثال المحاضرة: day = 4 فتُنفذ case 4 ويطبع Thursday ثم break تُنهي الجملة. لو القيمة خارج ١-٧ تُنفذ default ويطبع it is not a day.
اختبر نفسك
With int day = 4; what does this switch print?
جرّب الكود
Run the example yourselfشغّل المثال بنفسك
Edit the code and run it here. It compiles and runs on Wandbox, a free online compiler service (GCC 13). Short programs only, and the code leaves this page to that service when you run it.
عدّل الكود وشغّله من هنا مباشرة عبر خدمة Wandbox المجانية (مترجم GCC 13). للبرامج القصيرة فقط، ويُرسل الكود إلى تلك الخدمة عند التشغيل.
The output appears here after you run the code.
Runs on the free Wandbox service (wandbox.org), GCC 13.2.0. No account needed.