Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

1

Lecture 2

 Decision Making: Decision making structures require that the programmer


specify one or more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to be false.

 if statement: An if statement consists of a condition followed by one or more


statements.

 Syntax:

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

Dr/ A.A.A
‫‪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/ A.A.A‬‬
3

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


which executes when the condition is false.

 Syntax:

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

Dr/ A.A.A
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;
}

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

:‫ المخطط التدفقى‬

 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.

Dr/ A.A.A
5

 Syntax:

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 ‫جسى‬
#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;
}

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

Dr/ A.A.A
‫‪6‬‬

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

‫يهحىظح هايح جذا‪:‬‬


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

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

‫يثال ‪ :‬ارا اسدَا ادخال احذ انذسجاخ وتُاءا عهيها يطثع انرقذيش‬

‫>‪#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/ A.A.A‬‬
7

 switch statement: A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is checked for each case.

‫هُا تيعًم إخرثاس عهى قيى فارا ذطاتقد يع احذ انحاالخ يطثع ياتذاخهها‬
 Syntax:
switch(expression)

{
case constant-expression :
statement(s);
break; \
case constant-expression :
statement(s);
break;

// you can have any number of case statements.


default :
statement(s);
}
default ‫ فاَه يُفز جًم ال‬case ‫ اخرياسيح ودوسها فى حانح عذو ذطاتق انقيى انخاصح تال‬default ‫هُا‬

Dr/ A.A.A
8

#include <iostream>
using namespace std;

int main ()
{
int x = 3 ;
switch (x)
{
case 2 :
cout << "The Value of x is 2" << endl;
break;
case 3 :
cout << "The Value of x is 3" << endl;
break;
case 4 :
cout << "The Value of x is 4" << endl;
break;
default:
cout << "not matching" << endl;
}
return 0;
}
‫ والتى نضعها بجانب‬x ‫ على تطابق قيم ال‬switch ‫ ثم يعمل مقارنات داخل ال‬3 ‫ وهى‬x ‫ هنا وضعنا قيمة لل‬o
‫ هى ايقاف (كسر) عمل‬break ‫ االساسية فينفذ الجملة وفائدة‬x ‫ فاذا تطابقت احد القيم مع قيمة ال‬case
.‫المقارنات حتى ال ينفذ باقى الحاالت‬
default ‫ فسينفذ ال‬case ‫ اذا لم تطابق قيم ال‬o
The Value of x is 3 ‫ فيطبع الجملة هذه‬3 ‫ هنا نالحظ تطابق القيمة‬o

Dr/ A.A.A
9

‫برنامج التقديرات‬

#include <iostream>
using namespace std;

int main ()
{
char x ;
cin >> x;
switch (x)
{
case 'A' :
cout << "Excellent" << endl;
break;
case 'B' :
cout << "Very Good" << endl;
break;
case 'C' :
cout << "Good" << endl;
break;
case 'D' :
cout << "Acceptable" << endl;
break;
default:
cout << "Failed" << endl;
}
return 0;
}

case ‫اذا دخل اليوزر أحد الرموز (الحرف) فيطبع الجملة الخاصة بتطابق الحرف مع حروف ال‬

Dr/ A.A.A
10

:‫ المخطط التدفقى‬

 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).
 Syntax:

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

‫ فارا ذحقق ايضا‬condition2 ‫ اخش‬if ‫ وهى عثاسج عٍ ششط‬if ‫ يُفز جسى‬condition1 ‫فى حانح ذحقق انششط‬
‫يُفز انجسى انخاص تها‬

Dr/ A.A.A
11

#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/ A.A.A
12

 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
.‫غير محقق‬

Loop Type Description


Repeats a statement or group of statements while a given condition is
while loop
true. It tests the condition before executing the loop body.
Execute a sequence of statements multiple times and abbreviates the
for loop
code that manages the loop variable.
Like a while statement, except that it tests the condition at the end of
do...while loop
the loop body
You can use one or more loop inside any another while, for or
nested loops
do..while loop.

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


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

Dr/ A.A.A
13

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

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

Dr/ A.A.A
‫‪14‬‬

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

‫‪ ‬المخطط التدفقى ‪:‬‬

‫‪Dr/ A.A.A‬‬
15

 for Loop: is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.

 Syntax:

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/ A.A.A
16

‫ وفس وتيجح انثشوامج انساتق‬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 ‫ ٌىا يتم تىفيز جسم‬

Dr/ A.A.A
17

 Syntax:

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

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

#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/ A.A.A
‫‪18‬‬

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


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

‫‪ ‬المخطط التدفقى‪:‬‬

‫‪ Nested loops: A loop can be nested inside of another loop. C++ allows at least 256‬‬
‫‪levels of nesting.‬‬

‫‪ Syntax:‬‬
‫‪ nested for loop:‬‬

‫) ‪for ( initial value; condition; increment‬‬


‫{‬
‫) ‪for (initial value; condition; increment‬‬
‫{‬
‫;)‪statement(s‬‬
‫}‬
‫‪statement(s); // you can put more statements.‬‬
‫}‬

‫‪Dr/ A.A.A‬‬
19

 nested while loop:

while(condition)
{
while(condition)
{
statement(s);
}
statement(s); // you can put more statements.
}

 nested do …. while loop:

do
{
statement(s); // you can put more statements.
do
{
statement(s);
}
while( condition );

}
while( condition );

Dr/ A.A.A
20

 Loop Control Statements: Loop control statements change execution


from its normal sequence. When execution leaves a scope, all automatic objects
that were created in that scope are destroyed.

Jump statements ‫وتسمى ايضا بال‬

 break statement:
 When the break statement is encountered inside a loop, the loop is
immediately terminated and program control resumes at the next statement
following the loop.
 It can be used to terminate a case in the switch statement

#include <iostream>
using namespace std;
int main ()
{
for(int i=1;i<=10;i++)
{
if (i == 5)
break;
cout << "The value of i : " << i <<endl;
}
return 0;
}
‫ يكسر‬5 ‫ ب‬i ‫ ولكن استخدمنا شرط عندما تكون قيمة‬10 ‫ الى‬1 ‫ فى هذا البرنامج من المفترض انه يطبع من‬o
‫ الى مابعد ذلك‬5 ‫التكرار وال يطبع القيمة من‬

Dr/ A.A.A
21

:‫ المخطط التدفقى‬

 continue statement:
 The continue statement works somewhat like the break statement. Instead of
forcing termination, however, continue forces the next iteration of the loop to take
place, skipping any code in between.
 For the for loop, continue causes the conditional test and increment portions of the
loop to execute. For the while and do...while loops, program control passes to the
conditional tests.

Dr/ A.A.A
‫‪22‬‬

‫>‪#include <iostream‬‬
‫;‪using namespace std‬‬
‫)( ‪int main‬‬
‫{‬
‫)‪for(int i=1;i<=10;i++‬‬
‫{‬
‫)‪if (i == 5‬‬
‫;‪continue‬‬
‫;‪cout << "The value of i : " << i <<endl‬‬
‫}‬
‫;‪return 0‬‬
‫}‬
‫‪ o‬فى هذا البرنامج من المفترض انه يطبع من ‪ 1‬الى ‪ 10‬ولكن استخدمنا شرط عندما تكون قيمة ‪ i‬ب ‪ 5‬يكسر‬
‫التكرار عند هذه القيمة وال يطبع القيمة ‪ 5‬ولكن يطبع مابعد ال ‪5‬‬

‫‪ ‬المخطط التدفقى‪:‬‬

‫‪Dr/ A.A.A‬‬
23

 goto statement:
 A goto statement provides an unconditional jump from the goto to a labeled
statement in the same function.
 NOTE: Use of goto statement is highly discouraged because it makes difficult to
trace the control flow of a program, making the program hard to understand and
hard to modify. Any program that uses a goto can be rewritten so that it doesn't
need the goto.

#include <iostream>
using namespace std;
int main ()
{
cout << "line 1" << endl;
cout << "line 2" << endl;
goto line;
cout << "line 3" << endl;
cout << "line 4" << endl;
line:
cout << "line 5" << endl;
return 0;
}

.‫ فيىفز مايهّ ٌزا انعىُان‬line ً‫ٌىا يزٌة انّ انسطش انزِ عىُاو‬

Dr/ A.A.A
‫‪24‬‬

‫‪ ‬المخطط التدفقى‪:‬‬

‫‪Dr/ A.A.A‬‬
25

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

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

#include <iostream>
using namespace std;
int main ()
{

double s=0;
for (int 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 N;
double s=0;
cout << "Enter The first no: ";
cin >> i;
cout << "Enter The last no: ";
cin >> N;
for (int i ; i<=N ; i++)
{
s=s + i ;
}
cout << "The Sum is " << s << endl;
return 0;
}

Dr/ A.A.A
26

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

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

Dr/ A.A.A
27

) factorial ( ... 𝒏! ‫ اكرة تشَايج نحساب‬.5

#include <iostream>
using namespace std;
int main ()
{
int f=1,n;
cin >> n;
for (int i=1 ; i<=n ; i++)
{
f=f*i ;
}
cout << "fact is: " << f << endl;
return 0;
}

.‫ اكرة تشَايج نحساب يساحح ويحيظ دائشج تحيث اٌ َصف انقطش عذد صوجى‬.6

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

Dr Abdelazeem Adel

01069691361
Dr/ A.A.A

You might also like