Professional Documents
Culture Documents
Lec 2 New
Lec 2 New
Lecture 2
Syntax:
if(condition)
{
// statement(s) will execute if the condition is true
}
if فى حانح ذحقق انششط يُفز جسى
Dr/ A.A.A
2
>#include <iostream
;using namespace std
;return 0
}
oفى هذا البرنامج عرفنا متغير قيمة ب 10ثم سطر ifوالشرط هو هل قيمة aاصغر من 20فاذا تحقق
الشرط فسينفذ ماداخل ال block
oفتحقق الشرط اذا فطبع هذه الجملة كما هى a is less than 20
المخطط التدفقى:
Dr/ A.A.A
3
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
: المخطط التدفقى
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وفصم تيه انششَط تانشمض &&
فّ حانح تحقق أحذ انششطيه وفصم تيىٍم | |
يثال :ارا اسدَا ادخال احذ انذسجاخ وتُاءا عهيها يطثع انرقذيش
>#include <iostream
;using namespace std
}
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;
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;
}
هنا هيطبع الجملة الن الشرط االول تحقق ثم دخل وتحقق ايضا من الشرط الثانى
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
.غير محقق
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:
. كل رقم فى سطر على حدا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
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
المخطط التدفقى:
Nested loops: A loop can be nested inside of another loop. C++ allows at least 256
levels of nesting.
Syntax:
nested for loop:
Dr/ A.A.A
19
while(condition)
{
while(condition)
{
statement(s);
}
statement(s); // you can put more statements.
}
do
{
statement(s); // you can put more statements.
do
{
statement(s);
}
while( condition );
}
while( condition );
Dr/ A.A.A
20
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
#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;
}
#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
#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