Professional Documents
Culture Documents
Lec 5,6
Lec 5,6
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
;return 0
}
oفى هذا البرنامج عرفنا متغير قيمة ب 10ثم سطر ifوالشرط هو هل قيمة aاصغر من 20فاذا تحقق
الشرط فسينفذ ماداخل ال block
oفتحقق الشرط اذا فطبع هذه الجملة كما هى a is less than 20
برنامج المحاضرة
Dr/ Abdelazeem
3
شكهٓا:
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
;return 0
}
oفى هذا البرنامج وضعنا قيمة 100لمتغير aثم شرط ifفنالحظ عدم تحقق الشرط الن 100ليست اصغر من 20
oثم elseفيتنفذ مابداخلها فى حالة عدم تحقق الشرط فنالحظ انه يطبع الجملة كما هى
a is not less than 20
برنامج المحاضرة
Dr/ Abdelazeem
5
ٔضع ششط اٌ ذحقق ْيُفز نٕ يرحققش ْيشٕف انششط انهٗ تعذِ ْٔكزا حرٗ يصم
... انهٗ انششط تراعٓا تيرحقق ْٔيُفز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
oفى هذا البرنامج وضعنا قيمة ل aوهى 30ونالحظ عمل شروط فى حالة تحقق احد الشروط فينفذ الجسم.
نالحظ انه تحقق احد الشروط وهو فى السطر a==30وطبع فى النهاية السطرValue of a is 30
فً حانح نى اسدنا عمم ششطين سىيا والتذ من تحققهم فً جمهح 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;
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;
}
هنا هيطبع الجملة الن الشرط االول تحقق ثم دخل وتحقق ايضا من الشرط الثانى
Dr/ Abdelazeem
9
#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)
{
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
.الشرط غير محقق
Dr/ Abdelazeem
11
اذا اردنا طباعة ارقام من 1إلى 10كل رقم فى سطر على حدا.
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.
شكهٓا:
. كل رقم فى سطر على حدا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
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
}
Dr/ Abdelazeem
15
#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;
}
#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
}
01069691361
Dr/ Abdelazeem