You are on page 1of 17

1

Lecture 5,6

 Decision Making:
 if statement: An if statement consists of a condition followed by one or more
statements.
‫ ٔفٗ حانح عذو ذحققّ يش ْيرُفز حاجح‬if ‫ فٗ حانح ذحققّ ْيُفز جسى‬.‫ذحرٕٖ عهٗ ششط نرُفيز جًم يعيُح‬

 ‫شكهٓا‬:

if (condition)
{
// statement(s)
}
if ‫فٗ حانح ذحقق انششط يُفز جسى‬

Dr/ Abdelazeem
‫‪2‬‬

‫>‪#include <iostream‬‬
‫;‪using namespace std‬‬

‫)( ‪int main‬‬


‫{‬
‫;‪int a = 10‬‬
‫) ‪if( a < 20‬‬
‫{‬
‫;‪cout << "a is less than 20" << endl‬‬
‫}‬

‫;‪return 0‬‬
‫}‬
‫‪ o‬فى هذا البرنامج عرفنا متغير قيمة ب ‪ 10‬ثم سطر ‪ if‬والشرط هو هل قيمة ‪ a‬اصغر من ‪ 20‬فاذا تحقق‬
‫الشرط فسينفذ ماداخل ال ‪block‬‬
‫‪ o‬فتحقق الشرط اذا فطبع هذه الجملة كما هى ‪a is less than 20‬‬

‫‪ ‬برنامج المحاضرة‬

‫‪Dr/ Abdelazeem‬‬
3

 if … else statement: An if statement can be followed by an optional else statement,


which executes when the condition is false.
else ‫ ٔفٗ حانح عذو ذحققّ ْيُفز جسى‬if ‫فٗ حانح ذحقق انششط ْيُفز جسى‬

 ‫شكهٓا‬:

if(condition)
{
// statement(s) will execute if the condition is true
}
else
{
// statement(s) will execute if the condition is false
}
else ‫ ايا نٕ يرحقق انششط فيُفز جسى‬if ‫فٗ حانح ذحقق انششط يُفز جسى‬

Dr/ Abdelazeem
‫‪4‬‬

‫>‪#include <iostream‬‬
‫;‪using namespace std‬‬

‫)( ‪int main‬‬


‫{‬
‫;‪int a = 100‬‬
‫) ‪if( a < 20‬‬
‫{‬
‫;‪cout << "a is less than 20" << endl‬‬
‫}‬
‫‪else‬‬
‫{‬
‫;‪cout << "a is not less than 20" << endl‬‬
‫}‬

‫;‪return 0‬‬
‫}‬

‫‪ o‬فى هذا البرنامج وضعنا قيمة ‪ 100‬لمتغير ‪ a‬ثم شرط ‪ if‬فنالحظ عدم تحقق الشرط الن ‪ 100‬ليست اصغر من ‪20‬‬
‫‪ o‬ثم ‪ else‬فيتنفذ مابداخلها فى حالة عدم تحقق الشرط فنالحظ انه يطبع الجملة كما هى‬
‫‪a is not less than 20‬‬

‫‪ ‬برنامج المحاضرة‬

‫فى البرنامج دا نفذ جسم ‪ else‬النه لم يتحقق شرط جملة ‪if‬‬

‫‪Dr/ Abdelazeem‬‬
5

 if … else if … else statement: An if statement can be followed by an optional


else if...else statement, which is very usefull to test various conditions using single
if...else if statement.

‫ٔضع ششط اٌ ذحقق ْيُفز نٕ يرحققش ْيشٕف انششط انهٗ تعذِ ْٔكزا حرٗ يصم‬
... ‫ انهٗ انششط تراعٓا تيرحقق ْٔيُفز‬if ‫انٗ جًهح‬
else ‫ ْيُفز جسى‬if ‫اٌ نى ذحقق كم جًم‬

 ‫شكهٓا‬:

if(condition 1)
{
// statement(s) will execute if the condition1 is true
}
else if(condition 2)
{
// statement(s) will execute if the condition2 is true
}
else if(condition 3)
{
// statement(s) will execute if the condition3 is true
}
else
{
// executes when the none of the above condition is true.
}
‫ فارا ذحقق يُفز‬condition 2 ٍ‫ فٗ حانح عذو ذحققّ يرحقق ي‬if ‫ يُفز جسى‬condition 1 ‫فٗ حانح ذحقق‬
else ‫ ارا نى يرحقق كم انششٔط يُفز جسى‬.... ‫ انثاَيح ْٔكزا‬if ‫جسى‬

Dr/ Abdelazeem
‫‪6‬‬

‫>‪#include <iostream‬‬
‫;‪using namespace std‬‬

‫)( ‪int main‬‬


‫{‬
‫;‪int a = 30‬‬
‫) ‪if( a == 10‬‬
‫{‬
‫‪cout << "Value‬‬ ‫;‪of a is 10" << endl‬‬
‫}‬
‫) ‪else if( a == 20‬‬
‫{‬
‫‪cout << "Value‬‬ ‫;‪of a is 20" << endl‬‬
‫}‬
‫) ‪else if( a == 30‬‬
‫{‬
‫‪cout << "Value‬‬ ‫;‪of a is 30" << endl‬‬
‫}‬
‫‪else‬‬
‫{‬
‫‪cout << "Value‬‬ ‫;‪of a is not matching" << endl‬‬
‫}‬
‫;‪return 0‬‬
‫}‬

‫‪ o‬فى هذا البرنامج وضعنا قيمة ل ‪ a‬وهى ‪ 30‬ونالحظ عمل شروط فى حالة تحقق احد الشروط فينفذ الجسم‪.‬‬
‫نالحظ انه تحقق احد الشروط وهو فى السطر ‪ a==30‬وطبع فى النهاية السطر‪Value of a is 30‬‬

‫يهحٕظح ْايح جذا‪:‬‬


‫فٗ قٕاعذ ‪ if‬عثاسج عٍ ششٔط أ عالقاخ تًعُٗ اٌ يكٌٕ انششط عهٗ شكم أكثش يٍ أٔ اصغش يٍ ْٔكزا‪...‬‬

‫فً حانح نى اسدنا عمم ششطين سىيا والتذ من تحققهم فً جمهح‪ if‬نفصم تين انششوط تانشمض &&‬
‫فً حانح تحقق أحذ انششطين نفصم تينهم | |‬

‫‪Dr/ Abdelazeem‬‬
7

‫ ارا اسدَا ادخال احذ انذسجاخ ٔتُاءا عهيٓا يطثع انرقذيش‬: ‫يثال‬
#include <iostream>
using namespace std;

int main ()
{
int x ;
cout << "Enter The Degree: ";
cin >> x;
if( x <= 100 && x >= 85 )
{
cout << "Excellent" << endl;
}
else if ( x < 85 && x >= 75 )
{
cout << "Very Good" << endl;
}
else if ( x < 75 && x >= 65 )
{
cout << "Good" << endl;
}
else if ( x < 65 && x >= 60 )
{
cout << "Acceptable" << endl;
}
else
{
cout << "Failed" << endl;
}
return 0;

‫ ْٔكزا‬A+,A,B+ ‫يًكٍ اَد ذكرة انثشَايج دا تشكم آخش نهرقذيشاخ تشيٕص‬


 ُٗ‫تشآحرك يع‬

Dr/ Abdelazeem
8

 nested if statement: It is always legal to nest if-else statements, which means you
can use one if or else if statement inside another if or else if statement(s).
 ‫شكهٓا‬:

if( condition1)
{
// Executes when the condition1 is true
if(condition2)
{
// Executes when the condition2 is true
}
}

‫ فارا ذحقق ايضا‬condition2 ‫ اخش‬if ‫ ْٕٔ عثاسج عٍ ششط‬if ‫ يُفز جسى‬condition1 ‫فٗ حانح ذحقق انششط‬
‫يُفز انجسى انخاص تٓا‬

#include <iostream>
using namespace std;

int main ()
{
int a = 100;
int b = 200;

if( a == 100 )
{
if( b == 200 )
{
cout << "Value of a is 100 and b is 200" << endl;
}
}
return 0;
}

‫هنا هيطبع الجملة الن الشرط االول تحقق ثم دخل وتحقق ايضا من الشرط الثانى‬

Value of a is 100 and b is 200 ‫فيطبع على الشاشه‬

Dr/ Abdelazeem
9

‫برنامج مهم جدا‬


𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎 ‫ حل المعادلة‬.1
−𝑏± 𝑏 2 −4𝑎𝑐
𝑥= ‫وتحل بهذا القانون‬
2𝑎

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c;
float d, x1,x2,x;
cin >> a >> b >> c;
d=(b*b)- (4*a*c);
if (d > 0)
{
x1 = (-b + sqrt(d)) / (2*a);
x2 = (-b - sqrt(d)) / (2*a);
cout << "The first root of equation is : " << x1 << endl ;
cout << "The second root of equation is : " << x2 << endl ;
}
else if (d==0)
{
x = (-b) / (2*a);
cout << "The root of equation is : " << x << endl ;
}
else if(d < 0)
{

cout << "complex root " <<endl ;


}
return 0;
}

Dr/ Abdelazeem
10

 Loops:‫التكرار‬
Loops cause a section of your program to be repeated a certain number of times. The
repetition continues while a condition is true. When the condition becomes false, the
loop ends and control passes to the statements following the loop.
‫ التكرار يستمر عندا يكون الشرط محقق ويتوقف عندما يكون‬. ‫ تكرار جزء من البرنامج عدد من المرات‬o
.‫الشرط غير محقق‬

 while Loop: repeatedly executes a target statement as long as a a given


condition is true
 ‫شكهٓا‬:
while (condition)
{
statement(s);
}
. ‫ ويحدث تكرار‬block ‫ لو تحقق الشرط يتحقق مابداخل ال‬. ‫ هنا جملة واحدة او قالب من الجمل‬

Dr/ Abdelazeem
‫‪11‬‬

‫اذا اردنا طباعة ارقام من ‪ 1‬إلى ‪ 10‬كل رقم فى سطر على حدا‪.‬‬

‫>‪# include <iostream‬‬


‫; ‪using namespace std‬‬
‫)( ‪int main‬‬
‫{‬
‫;‪int i=1‬‬
‫)‪while (i<=10‬‬
‫{‬
‫;‪cout << "The value of i : " << i <<endl‬‬
‫;‪i++‬‬
‫}‬
‫;‪return 0‬‬
‫}‬

‫‪ o‬فى هذا البرنامج عرفنا متغير اسمه ‪ i‬وقيمته تبدأ ب ‪ 1‬ثم سطر ‪ while‬ومعناه اذا كانت قيمة ‪ i‬اصغر من ‪1‬‬
‫فيتحقق مابداخل ال ‪ block‬ونرى ان ‪ 1‬اقل من او يساوى ‪ 10‬فاذا تحقق يدخل ال‪ block‬ثم يطبع الرقم فى جملة‬
‫‪ cout‬ثم سطر ‪ i++‬وهذا معناه يزود ‪ 1‬فيتخزن فى ال ‪ i‬قيمة جديدة وهى ‪ 2‬ثم يذهب مرة الى جملة ‪while‬‬
‫ويتحقق هل ‪ 2‬اقل من او يساوى ‪ 10‬فاذا تحقق يكرر نفس الخطوات السابقة وهكذا‪.‬‬

‫‪Dr/ Abdelazeem‬‬
12

 for Loop: is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.
 ‫شكهٓا‬:

for ( initial value ; condition; increment or decrement )


{
statement(s);
}
.‫ هزه انخطح تنفز أوال ومشج واحذج‬:Initial value
.‫ لو كان صحيح فان جسم التكرار يتنفذ لو كان غير صحيح الينفذ‬: Condition
. increment or decrement ‫بعد تنفيذ جسم التكرار بيبدأ تنفيذ الزيادة او النقصان‬
. ‫ثم يتحقق من الشرط مرة أخرى وهكذا‬

.‫ كل رقم فى سطر على حدا‬10 ‫ إلى‬1 ‫اذا اردنا طباعة ارقام من‬

#include <iostream>
using namespace std;
int main ()
{
for(int i=1;i<=10;i++)
{
cout << "The value of i : " << i <<endl;
}
return 0;

Dr/ Abdelazeem
13

‫ نفس نتيجح انثشنامج انساتق‬o


‫ فتحقق انششط ثم دخم انً جسم‬10‫ ثم تحقق من انششط هم اصغش من او يساوي‬1 ‫ هنا تذأ انثشنامج انعذ من سقم‬o
2 ‫ ثم تحقق من انششط هم‬2 ‫ فأصثحت ب‬i ‫ عهً قيمح‬1 ‫ وصود‬increment ‫ ثم رهة انً ال‬1 ‫انتكشاس وطثع انقيمح‬
.‫ وهكزا‬2 ‫ ثم طثع ال‬10 ‫اصغش من او يساوي‬

 do….while Loop: Unlike for and while loops, which test the loop condition
at the top of the loop, the do...while loop checks its condition at the bottom of
the loop.
A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
.‫ أٔال ثى يرحقق يٍ انششط‬do ‫ ُْا يرى ذُفيز جسى‬

 ‫شكهٓا‬:

do
{
statement(s);
}
while( condition );

Dr/ Abdelazeem
‫‪14‬‬

‫اذا اردنا طباعة ارقام من ‪ 1‬إلى ‪ 10‬كل رقم فى سطر على حدا‪.‬‬

‫>‪#include <iostream‬‬
‫;‪using namespace std‬‬
‫)( ‪int main‬‬
‫{‬
‫;‪int i=1‬‬
‫‪do‬‬
‫{‬
‫;‪cout << "The value of i : " << i <<endl‬‬
‫;‪i++‬‬
‫}‬
‫;)‪while (i<=10‬‬

‫;‪return 0‬‬
‫}‬

‫‪ o‬نفس نتيجة البرامج السابقة‪.‬‬


‫‪ o‬فى هذا البرنامج وضعنا قيمة ابتدائية لل ‪ i‬وليكن ‪ 1‬ثم جملة ‪ do‬فيطبع أوال ‪ 1‬ثم يزود فتصبح ‪ 2‬ثم يذهب الى‬
‫سطر ‪ while‬ليتحقق ان ال ‪ 2‬اصغر من او يساوى ‪ 10‬فتحقق الشرط فيذهب ثانيا ل ‪ do‬ويطبع ال ‪ 2‬ثم يزود‬
‫وهكذا‪.‬‬
‫‪ o‬فى هذه الحالة يطبع أوال ألول مرة ويزود القيمة ثم يتحقق من الشرط بخالف طريقة‪ while‬التى بها التحقق من‬
‫الشرط أوال‪ .‬وهذا هو الفرق بينهم‪ ..‬‬

‫‪Dr/ Abdelazeem‬‬
15

‫تعض األيثهح انٓايح‬


100 ٗ‫ إن‬1 ٍ‫ اكرة تشَايج نحساب يجًٕع االسقاو ي‬.1

#include <iostream>
using namespace std;
int main ()
{
int i;
double s=0;
for ( i=1 ; i<=100 ; i++)
{
s=s + i ;
}
cout << "The Sum from 1 to 100 is " << s << endl;
return 0;
}

5050 :ْٗ ‫انُريجح‬

‫ يٍ االسقاو‬N ‫ اكرة تشَايج نحساب يجًٕع‬.2

#include <iostream>
using namespace std;
int main ()
{
int i,N;
double s=0;
cout << "Enter The first no: ";
cin >> i;
cout << "Enter The last no: ";
cin >> N;
for ( i ; i<=N ; i++)
{
s=s + i ;
}
cout << "The Sum is " << s << endl;
return 0;
}

Dr/ Abdelazeem
16

𝑵
𝒏𝟐 +𝟓
‫ اكرة تشَايج نحساب‬.3
𝟐
𝒏 +𝟕
𝒏=𝟏

#include <iostream>
using namespace std;
int main ()
{
int n,N;
double s=0;
cin >> N;
for ( n=1 ; n<=N ; n++)
{
s=s + (n*n+5.0)/(n*n+7) ;
}
cout << "s is: " << s << endl;
return 0;
}

𝑵
𝒏𝟐 +𝟓
‫ اكرة تشَايج نحساب‬.4
𝒏=𝟏 𝒏𝟐 +𝟒

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int n,N;
double s=0;
cin >> N;
for ( n=1 ; n<=N ; n++)
{
s=s + (n*n+5.0)/sqrt(n*n+4.0) ;
}
cout << "s is: " << s << endl;
return 0;
}

Dr/ Abdelazeem
‫‪17‬‬

‫‪ .5‬اكرة تشَايج نحساب يساحح ٔيحيظ دائشج تحيث اٌ َصف انقطش عذد صٔجٗ‪.‬‬

‫>‪#include <iostream‬‬
‫;‪using namespace std‬‬
‫)( ‪int main‬‬
‫{‬
‫;‪int r‬‬
‫;‪double a,c‬‬
‫;‪const float pi = 3.14‬‬
‫;‪cin >>r‬‬
‫)‪if (r%2 == 0‬‬
‫{‬
‫;‪a= pi * r * r‬‬
‫;‪c= 2 * pi * r‬‬
‫;‪cout << "The area of cicle is: " << a << endl‬‬
‫;‪cout << "The circum of cicle is: " << c << endl‬‬
‫}‬
‫;‪return 0‬‬
‫}‬

‫جرب انت تعمل لو كان نصف القطر فردى ‪‬‬

‫وجرب تكتب برنامج مساحة ومحيط مستطيل‬

‫برنامج مساحة ومحيط مربع‬

‫ممكن يجيلك فى االمتحان سؤال نظرى ‪ ..‬ويطلب منك ال ‪control statment‬‬

‫هكتبت كل جملة وتشرحها ببرنامج‬

‫‪Dr Abdelazeem Adel‬‬

‫‪01069691361‬‬

‫‪Dr/ Abdelazeem‬‬

You might also like