You are on page 1of 58

By: Mrs.

Afreen Mukhtar Solkar 1


• Introduction to Programming
‫مقدمة في البرمجة‬
Program: A program is a set of instructions that tell the computer to do various things.

.‫ البرنامج عبارة عن مجموعة من التعليمات التي تخبر الكمبيوتر أن تفعل أشياء مختلفة‬:‫التعريف‬

A set of rules and symbols used to construct a computer program.

.‫وهناك مجموعة من القواعد والرموز المستخدمة لبناء برنامج كمبيوتر‬

A language used to interact with the computer.

.‫واللغة المستخدمة على التفاعل مع الكمبيوتر‬

• Computer Languages ‫الكمبيوتر اللغات‬


1.Machine Language ‫آلة اللغة‬

• Uses binary code ‫يستخدم رمز ثنائي‬

• Machine-dependent ‫التي تعتمد على آلة‬

2.Assembly Language ‫لغة التجميع‬

• Uses mnemonics ‫يستخدم فن اإلستذكار‬


• Machine-dependent ‫التي تعتمد على آلة‬

3.High-Level Language (HLL) ‫عالية المستوى اللغة‬

• Uses English-like language ‫يستخدم اللغة اإلنجليزية شبيهة‬


• Machine independent ‫آلة مستقلة‬
• Examples: Pascal, C, C++, Java, Fortran, . . .

1. Machine Language ‫آلة اللغة‬

The representation of a computer program which is actually read and understood by the computer.

.‫تمثيل برنامج كمبيوتر وهو يقرأ الواقع ومفهومة من قبل الكمبيوتر‬


A program in machine code consists of a sequence of machine instructions.

.‫ويتألف البرنامج في رمز الجهاز من سلسلة من تعليمات الجهاز‬


Instructions::‫تعليمات‬
Machine instructions are in binary code.
.‫تعليمات الجهاز هي في الشفرة الثنائية‬
Instructions specify operations and memory cells involved in the operation.
.‫تعليمات تحدد العمليات وخاليا الذاكرة تشارك في العملية‬

By: Mrs. Afreen Mukhtar Solkar 2


Example: ‫مثال‬

Operation Address

0010 0000 0000 0100

0100 0000 0000 0101

0011 0000 0000 0110

2. Assembly Language ‫لغة التجميع‬

A symbolic representation of the machine language of a specific processor.

.‫التمثيل الرمزي للغة اآللة للمعالج معينة‬

Programming in assembly language is slow and error-prone but is more efficient in terms of hardware
performance.

‫البرمجة في لغة التجميع بطيئة وعرضة للخطأ ولكن هي أكثر كفاءة من حيث األداء األجهزة‬

3. High-level language ‫لغة رفيعة المستوى‬

A programming language which use statements consisting of English-like keywords such as "FOR", "PRINT"
or “IF“, ... etc.

‫ الخ‬... ،IF"" ‫" أو‬PRINT" ،FOR"" ‫لغة البرمجة التي تستخدم البيانات التي تتكون من كلمات اللغة اإلنجليزية مثل مثل‬

Much easier to program than in assembly language.

.‫أسهل بكثير للبرمجة مما كانت عليه في لغة التجميع‬

Operations can be described using familiar symbols.

.‫ويمكن وصف عمليات استخدام الرموز المألوفة‬


History ‫تاريخ‬

The first programming languages designed to communicate instructions to a computer were written in the
1950s.
.1950 ‫تم كتابة لغات البرمجة األولى تهدف إلى التواصل تعليمات إلى جهاز كمبيوتر في عام‬
C was developed between 1969 and 1973 as a system programming language.
‫ كلغة برمجة نظام‬1973 ‫ و‬1969 ‫بين عامي‬C ‫وقد وضعت‬
Prolog, designed in 1972, was the first logic programming language.

‫ كانت أول منطق لغة البرمجة‬،1972 ‫ صممت في عام‬،‫حاسوب‬

By: Mrs. Afreen Mukhtar Solkar 3


 Concepts of all Programming Languages
‫مفاهيم جميع لغات البرمجة‬
There are many programming languages available: Pascal, c, Java, Ada, Perl and Python.

: ‫هناك العديد من لغات البرمجة المتاحة‬

All of these languages share core concepts.

. ‫كل من هذه اللغات تبادل المفاهيم األساسية‬

By focusing on these concepts, you are better able to learn any programming language.

. ‫ وأنت أقدر على تعلم أي لغة برمجة‬، ‫من خالل التركيز على هذه المفاهيم‬

Hence, by learning Java, you are poised to learn other languages, such as C++ or Perl.

.‫أو بيرل‬C ++ ‫ تستعد لك ل تعلم لغات أخرى مثل‬،‫ من خالل تعلم جافا‬،‫وبالتالي‬

By learning the core concepts, you are also much more marketable as you are able to learn new technologies
quicker.

.‫ وأنت أيضا أكثر قابلية للتسويق قدر كنت قادرا على تعلم التكنولوجيات الجديدة أسرع‬، ‫من خالل تعلم المفاهيم األساسية‬

Note: Java is an object oriented programming language. However, we will not touch upon the concepts which
categorize it as one.

. ‫ فإننا لن أتطرق إلى المفاهيم التي تصنف على انها واحدة‬، ‫ ومع ذلك‬. ‫ جاوة هو كائن المنحى لغة البرمجة‬:‫مالحظة‬

 Introduction of C Language
‫ اللغة‬C ‫مقدمة من‬
C is a general purpose computer programming language, it Supports Structure programming.

.‫ فإنه يدعم البرمجة الهيكل‬،‫هو لألغراض العامة لغة برمجة الكمبيوتر‬C

C was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs.

.‫مختبرات بيل‬AT & T ‫ في‬1973 ‫ و‬1969 ‫في األصل من قبل دينيس ريتشي بين عامي‬C ‫وقد وضعت‬

By: Mrs. Afreen Mukhtar Solkar 4


 C++ programming language
C ++ ‫لغة برمجة‬
C++ is a general-purpose programming language.
C ++.‫هي لغة برمجة لألغراض العامة‬
It is object-oriented and generic programming features.

.‫ومن وجوه المنحى وميزات البرمجة العامة‬

C++ was developed by Bjarne Stroustrup at Bell Labs, starting in 1979.

.1979 ‫ بدءا من عام‬،‫التي كتبها بيارن ستروستروب في مختبرات بيل‬C ++ ‫وقد وضعت‬

C++ is standardized by the International Organization for Standardization (ISO), with the latest (and current)
standard version.

‫ مع أحدث (والحالي) اإلصدار القياسي‬،‫من قبل المنظمة الدولية للتوحيد القياسي‬C ++ ‫وموحدة‬

C++ is one of the most popular programming language for graphical application and it based on C language.

C ‫هي واحدة من لغة البرمجة األكثر شعبية بالنسبة للتطبيق رسومية وذلك على أساس اللغة‬C ++

Algorithms and Flowchart • ‫الخوارزميات وانسيابي‬


Algorithm ‫الخوارزمية‬

Definition:- An Algorithm is a process or set of rules to be followed in calculations or other problem- •


solving operations, especially by a computer.

‫ خاصة‬،‫ خوارزمية هي عملية أو مجموعة من القواعد التي يجب اتباعها في الحسابات أو غيرها من عمليات حل المشكلة‬- :‫تعريف‬ •
.‫من قبل جهاز كمبيوتر‬

The Word “Algorithm” comes from the Arabic Writer Muhammed bin Musa Khwarizmi. •

.‫كلمة "خوارزمية" يأتي من الكاتب العربي محمد بن موسى الخوارزمي‬

Currently the word “Algorithm” is strongly associated with Computer Science. •

‫حالياكلمة "خوارزمية" يرتبطارتباطاقوياعلومالحاسوب‬

An Algorithm specifies a series of steps that perform a particular task. •

‫خوارزمية يحدد سلسلة من الخطوات التي تؤدي مهمة معينة‬

By: Mrs. Afreen Mukhtar Solkar 5



Algorithm is the procedure that takes some values or set of values, as input and produces some values or •
set of values, as output.

.‫ كإخراج‬،‫ كمدخل وتنتج بعض القيم أو مجموعة من القيم‬،‫الخوارزمية هو اإلجراء الذي يأخذ بعض القيم أو مجموعة من القيم‬

An Algorithm is a Sequence of steps that converts input into output. •

.‫خوارزمية هو سلسلة من الخطوات التي تحول المدخالت إلى مخرجات‬

Input: - An Algorithm has zero or more inputs, taken from a specified set of Object. Example •
{a1,a2,…..,an}
‫ مأخوذة من مجموعة محددة من الكائنات‬،‫ خوارزمية لديه صفر أو أكثر من المدخالت‬- :‫المدخالت‬

Output: - An Algorithm has zero or more outputs, taken from a specified set of object. •

Example: - a1<=a2<=……<=an

‫ مأخوذة من مجموعة محددة من الجسم‬،‫ خوارزمية لديه صفر أو أكثر النواتج‬- :‫اإلخراج‬

Q. Write an algorithm to add two numbers entered by user.

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

Step 5: Display sum

Step 6: Stop

By: Mrs. Afreen Mukhtar Solkar 6


• Flowchart ‫مخطط‬
• Flowcharts allow you to draw a picture of the way a process actually works so that you can understand
the existing process and develop ideas about how to improve it.

.‫الرسوم البيانية تسمح لك لرسم صورة للطريقة عملية في الواقع يعمل بحيث تتمكن من فهم العملية الحالية وتطوير األفكار حول كيفية تحسينه‬

• These flowcharts make it easy to identify rework loops and complexity in a process.

.‫هذه المخططات االنسيابية تجعل من السهل تحديد الحلقات إعادة صياغة والتعقيد في العملية‬

• Using a flowchart has a variety of benefits: ƒ

:‫باستخدام مخطط لها مجموعة متنوعة من الفوائد‬

• It helps to clarify complex processes. ƒ

.‫فهو يساعد على توضيح العمليات المعقدة‬

• It Identifies steps that do not add value to the internal or external customer, including: delays; needless
storage and transportation; unnecessary work, duplication, and added expense; breakdowns in
communication.

،‫ واالزدواجية‬،‫ تخزين غني والنقل؛ العمل ال لزوم لها‬.‫ التأخير‬:‫ بما في ذلك‬،‫ويحدد الخطوات التي ال تضيف قيمة للعميل داخلي أو خارجي‬
.‫وحساب المضافة؛ أعطال في االتصاالت‬

• It helps team members gain a shared understanding of the process and use this knowledge to collect
data, identify problems, focus discussions, and identify resources. ƒ

‫ والتركيز‬،‫ وتحديد المشاكل‬،‫كما أنه يساعد أعضاء الفريق الحصول على فهم مشترك لهذه العملية واستخدام هذه المعرفة لجمع البيانات‬
.‫ وتحديد الموارد‬،‫المناقشات‬

• It serves as a basis for designing new processes.

By: Mrs. Afreen Mukhtar Solkar 7


By: Mrs. Afreen Mukhtar Solkar 8
 Local Environment Setup
‫إعداد البيئة المحلية‬
If you are still willing to set up your environment for C++, you need to have the following two softwares on
your computer.

.‫ تحتاج إلى أن يكون لبرامج التاليين على جهاز الكمبيوتر الخاص بك‬،C ++‫إذا كنت ال تزال على استعداد القامة بيئتك ل‬
1. Text Editor: ‫محرر النص‬:
This will be used to type your program.

.‫وسوف تستخدم هذه لكتابة البرنامج‬

2. Turbo c++:
++‫توربو ج‬

The files you create with your editor are called source files and for C++ they typically are named with the
extension .cpp.

.‫ التمديد‬.CPP ‫ أنها عادة ما تتم تسمية مع‬C ++ ‫ويطلق على الملفات التي تقوم بإنشائها مع محرر الملفات المصدر ول‬

When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking
each other's methods. Let us now briefly look into what a class, object, methods, and instant variables mean.

‫ دعونا اآلن‬.‫ فإنه يمكن تعريفها بأنها مجموعة من الكائنات التي تتصل عن طريق استدعاء أساليب بعضهم البعض‬، C ++‫عندما ننظر برنامج‬
.‫ والمتغيرات الفورية‬،‫ يعني الطرق‬،‫ وجوه‬،‫ننظر لفترة وجيزة في ما فئة‬

 Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as
behaviors - wagging, barking, and eating. An object is an instance of a class.

‫ كائن مثيل من‬.‫ وتناول الطعام‬،‫ نباح‬،‫ يهز‬- ‫ اللون واالسم وتولد وكذلك السلوكيات‬- ‫ كلب الدول‬:‫ مثال‬.‫ كائنات لها الدول والسلوكيات‬- ‫•وجوه‬
.‫فئة‬

 Class - A class can be defined as a template/blueprint that describes the behaviors/states that object of its
type support.

.‫ تنص على أن الهدف من دعم نوعه‬/ ‫ مخططا أن يصف السلوكيات‬/ ‫ فئة يمكن تعريفها بأنها قالب‬- ‫•الفئة‬

 Methods - A method is basically a behavior. A class can contain many methods. It is in methods where
the logics are written, data is manipulated and all the actions are executed.

‫ يتم التالعب بها البيانات‬،‫ هو في طرق حيث تتم كتابة المنطق‬.‫ يمكن أن تحتوي فئة العديد من األساليب‬.‫ طريقة هو في األساس السلوك‬- ‫•طرق‬
.‫ويتم تنفيذ جميع اإلجراءات‬

 Instant Variables - Each object has its unique set of instant variables. An object's state is created by the
values assigned to these instant variables.
‫ يتم إنشاء دولة كائن من حيث القيم المعينة لهذه‬.‫ كل كائن له مجموعة فريدة من نوعها من المتغيرات حظة‬- ‫•المتغيرات فوري‬
.‫المتغيرات الفورية‬

By: Mrs. Afreen Mukhtar Solkar 9


 Basic Structure of a C++ Program
‫برنامج‬C ++ ‫ الهيكل األساسي لل‬
The best way to start learning a programming language is by writing a program.

.‫أفضل طريقة للبدء في تعلم لغة البرمجة من خالل كتابة البرنامج‬


Therefore here is our first program.

.‫لذلك هنا هو البرنامج األول‬

//* my first program in C++*//

#include<iostream.h>

int main()

cout<<“Hello World”;

return 0;

//* my first program in C++*//

Comment line: This is called the comment line.

‫ هذا ما يسمى خط تعليق‬:‫تعليق السطر‬.

All lines begin with two slash signs(//) are comments and they do not have any effects on the program.

.‫) هي تعليقات وليس لديهم أي تأثير على البرنامج‬//( ‫جميع خطوط تبدأ مع اثنين من عالمات مائل‬

#include<iostream.h>

Pre-processor directive: Lines begin with hash sign (#) are called preprocessor directive.

.‫ التوجيه قبل المعالج‬:‫) توجيه المعالج‬#( ‫وتسمى خطوط تبدأ مع عالمة التجزئة‬

A Program always starts with # Symbol.

iostream.h is a header file, having extension (.h)

int main ( )  main function (‫)الوظيفةالرئيسية‬

{ open braces (‫)األقواسالمفتوحة‬

By: Mrs. Afreen Mukhtar Solkar 10


cout<<” hello world “ ;

}  Close braces (‫)األقواسقريبة‬

int main()

This line shows the beginning of the main function.

.‫يظهر هذا الخط بداية المهمة الرئيسية‬

This is the point where all C++ programs start their execution.

.‫تنفيذها‬C ++ ‫هذه هي النقطة التي تبدأ جميع برامج‬


The word main is followed by a pair of parenthesis ().
.)( ‫ويتبع كلمة الرئيسي بواسطة زوج من األقواس‬
Right after the parenthesis we can find the body of the main function in braces {}.
.}{ ‫مباشرة بعد قوسين يمكننا العثور على جثة وتتمثل المهمة الرئيسية في األقواس‬
cout<<“Hello World”;

This line is a C++ statement. It is the statement that produce some effect in the program.
.‫ هو بيان أن تنتج بعض التأثير في البرنامج‬.‫البيان‬C ++ ‫وهذا الخط هو‬
The statement always ends with a semi colon (;).
.)‫بيان ينتهي دائما مع فاصلة منقوطة (؛‬
This character is used to mark the end of the statement.
.‫ويستخدم هذا الحرف بمناسبة نهاية البيان‬
return 0;
The return statement causes the main function to finish.
.‫يتسبب في بيان عودة وتتمثل المهمة الرئيسية إلنهاء‬
cout<<“Hello World”;

This line is a C++ statement. It is the statement that produce some effect in the program.

.‫ هو بيان أن تنتج بعض التأثير في البرنامج‬.‫البيان‬C ++ ‫وهذا الخط هو‬

The statement always ends with a semi colon (;).


.)‫بيان ينتهي دائما مع فاصلة منقوطة (؛‬
This character is used to mark the end of the statement.
.‫ويستخدم هذا الحرف بمناسبة نهاية البيان‬
return 0;
The return statement causes the main function to finish.
.‫يتسبب في بيان عودة وتتمثل المهمة الرئيسية إلنهاء‬
cout :- this statement is used to print the string enclosed in quotation.
.‫ يستخدم هذا البيان لطباعة سلسلة المغلقة في االقتباس‬- :‫االحصاء‬
It is also used to print the value of the variable.

.‫كما انها تستخدم لطباعة قيمة المتغير‬

By: Mrs. Afreen Mukhtar Solkar 11


Example:

cout<<” welcome to computer center “;

cout<<”variable names”;

The(<<)operator called insertion operator.

.‫دعا المشغل (<<) مشغل اإلدراج‬

cin: -cin is used for inputting the value of variable…

- ‫ يستخدم‬:‫سين‬cin ‫إلدخال قيمة المتغير‬

Example :-cin>> a; , cin>> b;

cin>> a >>b ;

The operator >> is known as extraction operator, it extract the value.

.‫ واستخراج القيمة‬،‫المشغل >> كما هو معروف المشغل استخراج‬

Compile & Execute C++ Program:


C ++:‫•تجميع وتنفيذ برنامج‬
Let's look at how to save the file, compile and run the program. Please follow the steps given below:
:‫ يرجى اتباع الخطوات الواردة أدناه‬.‫ ترجمة وتشغيل البرنامج‬،‫دعونا ننظر في كيفية حفظ الملف‬

1. Open turbo C++ and write the code as above.

2. Save the file as: hello.cpp

3. Open a command prompt and go to the directory where you saved the file.

4. You will be able to see ' Hello World ' printed on the window.

.‫ وكتابة الكود كما أعاله‬C ++ ‫افتح توربو‬1.


hello.cpp‫حفظ الملف على النحو التالي‬2.
.‫افتح موجه األوامر ثم انتقل إلى الدليل الذي قمت بحفظ الملف‬3.
.‫وسوف تكون قادرا على رؤية "مرحبا أيها العالم" مطبوعة على النافذة‬4.

By: Mrs. Afreen Mukhtar Solkar 12


 Semicolons & Blocks in C++

‫منقوطة وكتل في‬C ++


In C++, the semicolon is a statement terminator.
.‫ الفاصلة المنقوطة هو فاصل بيان‬،C ++ ‫؟ في‬C ++ ‫منقوطة وكتل في‬
That is, each individual statement must be ended with a semicolon.
.‫ يجب أن تنتهي كل فرد البيان؟ بفاصلة منقوطة‬،‫وهذا هو‬

For example, following are three different statements:


:‫ فيما يلي ثالثة بيانات مختلفة‬،‫؟ على سبيل المثال‬

x = y;
y = y+1;
add(x, y);

A block is a set of logically connected statements that are surrounded by opening and closing braces.

.‫كتلة عبارة عن مجموعة من البيانات منطقيا المتصلة التي تحيط بها؟ االفتتاح والختام األقواس‬

For example:
{
cout << "Hello World"; // prints Hello World
return 0;
}

C++ does not recognize the end of the line as a terminator. For this reason, it does not matter where you put a
statement in a line.

.‫ أليس كذلك؟ ال يهم أين كنت وضعت بيانا في خط‬،‫ لهذا السبب‬.‫نهاية السطر كما فاصل‬C ++ ‫تعترف‬

For example:
x = y;
y = y+1;
add(x, y);

is the same as

x = y; y = y+1; add(x, y);

By: Mrs. Afreen Mukhtar Solkar 13


• Whitespace in C++
C ++ ‫• بيضاء في‬
• A line containing only whitespace, with a comment, is known as a blank
line, and C++ compiler totally ignores it.

‫؟‬.‫مترجم يتجاهل تماما‬C ++ ‫ و‬،‫ كما هو معروف فارغ؟ الخط‬،‫ مع تعليق‬،‫؟ والسطر الذي يحتوي فقط على مساحات‬

• Whitespace is the term used in C++ to describe blanks, tabs, newline characters
and comments.

.‫ أحرف السطر؟ والتعليقات‬،‫ عالمات‬،‫لوصف الفراغات‬C ++ ‫بيضاء هو مصطلح يستخدم في‬

• Whitespace separates one part of a statement from another and enables the compiler to identify where
one element in a statement, such as int, ends and the next element begins.

‫؟ ينتهي ويبدأ العنصر‬،‫ مثل كثافة العمليات‬،‫بيضاء يفصل جزء واحد من بيان من آخر وتمكن المترجم لتحديد أين عنصر واحد في بيان لها‬
.‫التالي‬

Statement 1:
int age;

In the above statement there must be at least one whitespace character (usually a space) between int and age for
the compiler to be able to distinguish them.

‫في البيان المذكور أعاله أن يكون هناك واحد على األقل حرف مسافة بيضاء (عادة مسافة) بين الباحث والعمر للمترجم أن يكون قادرا على‬
.‫التمييز بينها‬

Statement 2:
fruit = apples + oranges; // Get the total fruit

In the above statement 2, no whitespace characters are necessary between fruit and =, or between = and apples,
although you are free to include some if you wish for readability purpose.

‫ على الرغم من أنت حر لتشمل بعض إذا كنت‬،‫ أو بين = والتفاح‬،=‫ لم أحرف المسافات البيضاء الضرورية بين الفواكه و‬،‫ أعاله‬2 ‫في البيان‬
.‫ترغب لغرض القراءة‬

By: Mrs. Afreen Mukhtar Solkar 14


• Comments in C++
C ++ ‫• تعليقات في‬
Program comments are explanatory statements that you can include in the C++
code.
.‫؟ التعليمات البرمجية‬C ++ ‫تعليقات البرنامج هي البيانات اإليضاحية التي يمكن أن تدرج في‬
These comments help anyone reading the source code.
.‫هذه التعليقات تساعد أي شخص يقرأ شفرة المصدر‬

All programming languages allow for some form of comments.


.‫جميع لغات البرمجة تسمح لبعض شكل تعليقات‬
C++ supports single-line and multi-line comments.
.‫ يدعم خط واحد وتعليق متعدد الخطوط‬C ++ ‫؟‬
All characters available inside any comment are ignored by C++ compiler.
C ++. ‫يتم تجاهل كافة األحرف المتاحة داخل أي تعليق من قبل مترجم‬
C++ comments start with/ /* and end with *//. For example:
//* This is a comment *//
//* C++ comments can also*//
//* span multiple lines*//

 C++ Data Types

C ++ ‫أنواع البيانات‬
• While doing programming in any programming language, you need to use various variables to store
various information.

.‫ تحتاج إلى استخدام متغيرات مختلفة لتخزين المعلومات المختلفة‬،‫في حين تقوم البرمجة في أي لغة برمجة‬

• Variables are nothing but reserved memory locations to store values.

.‫المتغيرات ليست سوى مواقع الذاكرة المحجوزة لتخزين القيم‬

• This means that when you create a variable you reserve some space in memory.

.‫وهذا يعني أنه عند إنشاء المتغير الذي حجز بعض المساحة في الذاكرة‬

• You may like to store information of various data types like character, wide character, integer, floating
point, double floating point, boolean etc.

‫ منطقية الخ‬،‫ نقطة عائمة مزدوجة‬،‫ النقطة العائمة‬،‫ صحيح‬،‫ حرف واسع‬،‫قد ترغب في تخزين المعلومات من أنواع البيانات المختلفة مثل حرف‬

• Based on the data type of a variable, the operating system allocates memory and decides what can be
stored in the reserved memory.

.‫ نظام التشغيل يخصص الذاكرة ويقرر ما يمكن تخزينها في ذاكرة محفوظة‬،‫استنادا إلى نوع البيانات متغير‬

By: Mrs. Afreen Mukhtar Solkar 15


 Primitive Built-in Types: Data Types
‫ أنواع البيانات‬:‫ البدائية المدمج في أنواع‬
shortint : This data type is used to represent short integer.

.‫ يستخدم هذا النوع من البيانات لتمثيل صحيح قصير‬:‫الباحث القصير‬

int: This data type is used to represent integer.

.‫ يستخدم هذا النوع من البيانات لتمثيل عدد صحيح‬:‫الباحث‬

Long int:-This data type is used to represent long integer.

‫ طويلة كثافة العمليات‬:‫ هذا نوع البيانات لتمثيل صحيح طويل‬،‫يستخدم‬

Float: This data type is used to represent floating point number.

.‫ يستخدم هذا النوع من البيانات لتمثيل العائمة عدد النقطة‬:‫تطفو‬

Double: This data type is used to represent double precision floating point number.

.‫ يستخدم هذا النوع من البيانات لتمثيل الدقة المزدوجة العائمة عدد النقطة‬:‫نقرا‬

Long double: This data type is used to represent double precision floating poin

‫ طويل مزدوج‬:‫يستخدم هذا النوع من البيانات لتمثيل الدقة المزدوجة النقطة العائمة‬t

char: This data type is used to represent a single character.

.‫ يستخدم هذا النوع من البيانات لتمثيل حرف واحد‬:‫شار‬

bool: This data type is used to represent Boolean value. Using variable names and data

‫ باستخدام أسماء وبيانات متغيرة‬.‫ يستخدم هذا النوع من البيانات لتمثيل قيمة منطقية‬:‫منطقي‬

By: Mrs. Afreen Mukhtar Solkar 16


C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table lists
down seven basic C++ data types:

‫ يسرد الجدول التالي أسفل سبعة أنواع‬.‫تقديم مبرمج تشكيلة غنية من أنواع البيانات المعرفة من قبل المستخدم المدمج في وكذلك‬C ++
C ++: ‫البيانات األساسية‬

Type Keyword

Boolean bool

Character char

Integer int

Floating point float

Double floating point double

Valueless void

Wide character wchar_t

The following table shows the variable type, how much memory it takes to store the value in memory, and what
is maximum and minimum value which can be stored in such type of variables.

‫ وما هي قيمة الحد األقصى والحد األدنى الذي يمكن تخزينها‬،‫ مقدار الذاكرة ما يلزم لتخزين القيمة في الذاكرة‬،‫ويبين الجدول التالي نوع متغير‬
.‫في مثل هذا النوع من المتغيرات‬

Type Typical Bit Width Typical Range

char 1byte -127 to 127 or 0 to 255

unsigned char 1byte 0 to 255

signed char 1byte -127 to 127

int 4bytes -2147483648 to 2147483647

By: Mrs. Afreen Mukhtar Solkar 17


unsigned int 4bytes 0 to 4294967295

signed int 4bytes -2147483648 to 2147483647


short int 2bytes -32768 to 32767

unsigned short int Range 0 to 65,535

signed short int Range -32768 to 32767

long int 4bytes -2,147,483,648 to 2,147,483,647

signed long int 4bytes same as long int

unsigned long int 4bytes 0 to 4,294,967,295

float 4bytes +/- 3.4e +/- 38 (~7 digits)

double 8bytes +/- 1.7e +/- 308 (~15 digits)

long double 8bytes +/- 1.7e +/- 308 (~15 digits)

wchar_t 2 or 4 bytes 1 wide character

The sizes of variables might be different from those shown in the table, depending on the compiler and the
computer you are using.

.‫ وهذا يتوقف على مترجم وجهاز الكمبيوتر الذي تستخدمه‬،‫أحجام متغيرات قد تكون مختلفة عن تلك التي تظهر في الجدول‬

By: Mrs. Afreen Mukhtar Solkar 18


Following is the example, which will produce correct size of various data types on your computer.

.‫ والتي سوف تنتج حجم الصحيح من أنواع البيانات المختلفة على جهاز الكمبيوتر الخاص بك‬،‫فيما يلي سبيل المثال‬

#include <iostream.h>

int main()

cout << "Size of char : " << sizeof(char) << endl;

cout << "Size of int : " << sizeof(int) << endl;

cout << "Size of short int : " << sizeof(short int) << endl;

cout << "Size of long int : " << sizeof(long int) << endl;

cout << "Size of float : " << sizeof(float) << endl;

cout << "Size of double : " << sizeof(double) << endl;

cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;

return 0;

This example uses endl, which inserts a new-line character after every line and << operator is being used to pass
multiple values out to the screen. We are also using sizeof() operator to get size of various data types.

‫ ونحن أيضا‬.‫ التي إدراج حرف سطر جديد بعد كل سطر و<< يستخدم المشغل لتمرير قيم متعددة إلى الشاشة‬،ENDL ‫يستخدم هذا المثال‬
.‫المشغل () للحصول على حجم أنواع البيانات المختلفة‬sizeof ‫استخدام‬

By: Mrs. Afreen Mukhtar Solkar 19


 C++ Variable and its Types
C ++ ‫متغير وأنواعه‬

A variable provides us with named storage that our programs can use.

.‫يوفر متغير لنا تخزين المسماة التي يمكن استخدام برامجنا‬

Each variable in C++ has a specific type, which determines the size and layout of the variable's memory.

‫كل متغير في‬C ++ .‫ والذي يحدد حجم وشكل من الذاكرة للمتغير‬،‫لديه نوع معين‬

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with
either a letter or an underscore.

.‫ يجب أن يبدأ مع أي بريد إلكتروني أو تسطير‬.‫ والسفلية‬،‫يمكن أن تتكون اسم متغير من الحروف واألرقام‬

There are following basic types of variable in C++ as explained in last chapter:

Type Description

bool Stores either value true or false.

char Typically a single octet(one byte). This is an integer type.

int The most natural size of integer for the machine.

float A single-precision floating point value.

double A double-precision floating point value.

void Represents the absence of type.

wchar_t A wide character type.

By: Mrs. Afreen Mukhtar Solkar 20


 Variable Definition in C++:

C ++: ‫تعريف المتغير في‬

A variable definition means to tell the compiler where and how much to create the storage for the variable.

.‫تعريف متغير يعني أن نقول للمترجم حيث وكم إلنشاء لتخزين المتغير‬

A variable definition specifies a data type, and contains a list of one or more variables of that type as follows:

‫ ويحتوي على قائمة واحدة أو أكثر من‬،‫ويحدد تعريف متغير نوع البيانات‬

type variable_list;

Here, type must be a valid C++ data type including char, w_char, int, float, double, bool or any user-defined
object, etc., and variable_list may consist of one or more identifier names separated by commas.

‫ منطقي أو أي كائن المعرفة من قبل‬،‫ مزدوجة‬،‫ تطفو‬،‫ كثافة‬،w_char ،‫نوع البيانات بما في ذلك شار‬C ++ ‫ يجب أن يكون نوع صالحة‬،‫هنا‬
‫من واحد أو أكثر من أسماء معرف مفصولة بفواصل‬variable_list ‫ ويمكن أن تتكون‬،‫ الخ‬،‫المستخدم‬

Some valid declarations are shown here:

int i, j, k; char c, ch; float f, salary; double d;

 Variable Scope in C++

C ++ ‫نطاق المتغير في‬


A scope is a region of the program and broadly speaking there are three places, where variables can be declared:

:‫ حيث يمكن أن يتم تعريف المتغيرات‬،‫ونطاق غير منطقة من البرنامج وبشكل عام هناك ثالثة أماكن‬

1. Inside a function or a block which is called local variables.

.‫ داخل وظيفة أو كتلة وهو ما يسمى المتغيرات المحلية‬.1

2. Outside of all functions which is called global variables.

.2.‫خارج من جميع الوظائف التي تسمى المتغيرات العالمية‬

By: Mrs. Afreen Mukhtar Solkar 21


 Local Variables

:‫المتغيرات المحلية‬

Variables that are declared inside a function or block are local variables.

.‫المتغيرات التي تم تعريفها داخل دالة أو كتلة هي المتغيرات المحلية‬

They can be used only by statements that are inside that function or block of code.

.‫ويمكن استخدامه فقط من قبل البيانات الموجودة داخل تلك الوظيفة أو كتلة من التعليمات البرمجية‬

Local variables are not known to functions outside their own.

.‫ليست معروفة المتغيرات المحلية إلى وظائف خارج خاصة بهم‬

Following is the example using local variables:

‫وفيما يلي مثال على ذلك هو استخدام المتغيرات المحلية‬

#include <iostream.h>

int main ()

int a, b;

int c;

a = 10;

b = 20;

c = a + b;

cout << c;

return 0;

Output:

30

By: Mrs. Afreen Mukhtar Solkar 22


 Global Variables:

:‫المتغيرات العالمية‬
Global variables are defined outside of all the functions, usually on top of the program.

.‫ وعادة على الجزء العلوي من البرنامج‬،‫يتم تعريف المتغيرات العالمية خارج عن وظائف‬

The global variables will hold their value throughout the life-time of your program.

.‫سوف المتغيرات العالمية تعقد قيمتها في جميع أنحاء الحياة مرة من البرنامج‬

A global variable can be accessed by any function.

.‫متغير عالمي يمكن الوصول إليها من قبل أي وظيفة‬

That is, a global variable is available for use throughout your entire program after its declaration.

.‫ متغير عمومي متوفرة لالستخدام في جميع أنحاء البرنامج بأكمله بعد إعالنها‬،‫وهذا يعني‬

Following is the example using global and local variables:

#include <iostream.h>

int g;

int main ()

int a, b;

a = 10;

b = 20;

g = a + b;

cout << g;

return 0;

Output:

30

By: Mrs. Afreen Mukhtar Solkar 23


 Operators:
:‫العاملين‬
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++
is rich in built-in operators and provide the following types of operators:

‫غنية المدمج في المشغلين وتوفير األنواع التالية من‬. C ++ ‫المشغل هو الرمز الذي يقول المترجم لتأدية معالجات رياضية أو منطقية محددة‬
:‫مشغلي‬
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators

‫•العوامل الحسابية‬
‫•مشغلي العالئقية‬
‫•العوامل المنطقية‬
‫•مشغلي المختصة بالبت‬
‫•عوامل التعيين‬
‫•مشغلي متفرقات‬
This chapter will examine the arithmetic, relational, logical, bitwise, assignment and other operators one by one.
.‫ وتعيين والمشغلين اآلخرين واحدا تلو اآلخر‬،‫ أحادي المعامل‬،‫ والعالئقية ومنطقية‬،‫وهذا الفصل دراسة الحساب‬

 Arithmetic Operators:
‫•العوامل الحسابية‬

It provides operators for five basic arithmetical calculations: addition, subtraction, multiplication, division, and
taking the modulus.
.‫ وأخذ معامل‬،‫ الجمع والطرح والضرب والقسمة‬:‫ويوفر المشغلين لمدة خمس الحسابات الحسابية األساسية‬
Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after B % A will give 0
an integer division
++ Increment operator, increases integer value A++ will give 11
by one
-- Decrement operator, decreases integer A-- will give 9
value by one

By: Mrs. Afreen Mukhtar Solkar 24


Example:

#include <iostream.h>
main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0;

Output:

When the above code is compiled and executed, it produces the following result:
Line 1 - Value of c is :31
Line 2 - Value of c is :11
Line 3 - Value of c is :210
Line 4 - Value of c is :2
Line 5 - Value of c is :1
Line 6 - Value of c is :21
Line 7 - Value of c is :22

By: Mrs. Afreen Mukhtar Solkar 25


 Relational Operators
‫مشغلي العالئقية‬

In computer science, a relational operator is a programming language construct oroperator that tests or defines
some kind of relation between two entities.

.‫ مشغل العالئقية هو بناء لغة البرمجة أو المشغل الذي يختبر أو تحدد نوعا من العالقة بين كيانين‬،‫في علوم الكمبيوتر‬

Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


== Checks if the values of two (A == B) is not true.
operands are equal or not, if yes
then condition becomes true.
!= Checks if the values of two (A != B) is true.
operands are equal or not, if
values are not equal then
condition becomes true.
> Checks if the value of left operand (A > B) is not true.
is greater than the value of right
operand, if yes then condition
becomes true.
< Checks if the value of left operand (A < B) is true.
is less than the value of right
operand, if yes then condition
becomes true.
>= Checks if the value of left operand (A >= B) is not true.
is greater than or equal to the
value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand (A <= B) is true.
is less than or equal to the value of
right operand, if yes then
condition becomes true.

By: Mrs. Afreen Mukhtar Solkar 26


 Logical Operators

‫العوامل المنطقية‬

Assume variable A holds 1 and variable B holds 0, then:

Operator Description Example


&& Called Logical AND operator. If (A && B) is false.
both the operands are non-zero,
then condition becomes true.
|| Called Logical OR Operator. If (A || B) is true.
any of the two operands is non-
zero, then condition becomes true.
! Called Logical NOT Operator. Use !(A && B) is true.
to reverses the logical state of its
operand. If a condition is true, then
Logical NOT operator will make
false.

 Bitwise Operators
‫أحادي المعامل مشغلي‬

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:

:‫ و^ هي كما يلي‬،| ،‫ جداول الحقيقة لو‬.‫مشغل أحادي المعامل يعمل على اجزاء وإجراء العملية بعض الشيء من قبل بت‬

p q p&q p|q p^q


0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011

By: Mrs. Afreen Mukhtar Solkar 27


The Bitwise operators supported by C++ language are listed in the following table. Assume variable A holds 60
and variable B holds 13, then:

Operator Description Example


& Binary AND Operator copies a bit (A & B) will give 12 which is
to the result if it exists in both 0000 1100
operands.
| Binary OR Operator copies a bit if (A | B) will give 61 which is 0011
it exists in either operand. 1101
^ Binary XOR Operator copies the (A ^ B) will give 49 which is 0011
bit if it is set in one operand but 0001
not both.
~ Binary Ones Complement (~A ) will give -61 which is 1100
Operator is unary and has the 0011 in 2's complement form due
effect of 'flipping' bits. to a signed binary number.
<< Binary Left Shift Operator. The A << 2 will give 240 which is
left operands value is moved left 1111 0000
by the number of bits specified by
the right operand.
>> Binary Right Shift Operator. The A >> 2 will give 15 which is 0000
left operands value is moved right 1111
by the number of bits specified by
the right operand.

 Assignment Operators:
:‫مشغلي المهمة‬

There are following assignment operators supported by C++ language:

Operator Description Example


= Simple assignment operator, C = A + B will assign value of A
Assigns values from right side + B into C
operands to left side operand.
+= Add AND assignment operator, It C += A is equivalent to C = C + A
adds right operand to the left
operand and assign the result to
left operand.
-= Subtract AND assignment C -= A is equivalent to C = C - A
operator, It subtracts right operand
from the left operand and assign
the result to left operand.
*= Multiply AND assignment C *= A is equivalent to C = C * A
operator, It multiplies right
operand with the left operand and
assign the result to left operand.

By: Mrs. Afreen Mukhtar Solkar 28


/= Divide AND assignment operator, C /= A is equivalent to C = C / A
It divides left operand with the
right operand and assign the result
to left operand.
%= Modulus AND assignment C %= A is equivalent to C = C %
operator, It takes modulus using A
two operands and assign the result
to left operand.
<<= Left shift AND assignment C <<= 2 is same as C = C << 2
operator.
>>= Right shift AND assignment C >>= 2 is same as C = C >> 2
operator.
&= Bitwise AND assignment C &= 2 is same as C = C & 2
operator.
^= Bitwise exclusive OR and C ^= 2 is same as C = C ^ 2
assignment operator.
|= Bitwise inclusive OR and C |= 2 is same as C = C | 2
assignment operator.

 Misc Operators:
‫مشغلي متفرقات‬:
The following table lists some other operators that C++ supports.

Operator Description
sizeof sizeof operator returns the size of a variable. For
example, sizeof(a), where ‘a’ is integer, and will
return 4.
Condition ? X : Y Conditional operator (?). If Condition is true then it
returns value of X otherwise returns value of Y.
, Comma operator causes a sequence of operations to
be performed. The value of the entire comma
expression is the value of the last expression of the
comma-separated list.
. (dot) and -> (arrow) Member operators are used to reference individual
members of classes, structures, and unions.
Cast Casting operators convert one data type to another.
For example, int(2.2000) would return 2.
& Pointer operator ‘&’ returns the address of a
variable. For example &a; will give actual address of
the variable.
* Pointer operator * is pointer to a variable. For
example *var; will pointer to a variable var.

By: Mrs. Afreen Mukhtar Solkar 29


 Input/output in C++
‫ اإلخراج في‬/ ‫اإلدخال‬C ++
C++ input/output streams are primarily defined by iostream, a header file that is part of the C++ standard
library.

C ++.‫ ملف الرأس الذي هو جزء من المكتبة القياسية في‬، iostream‫ اإلخراج في المقام األول من قبل‬C ++ / ‫يتم تعريف إدخال تيارات‬

These are combined as a library of functions.

.‫يتم الجمع بين هذه كمكتبة من الوظائف‬

Like the cstdio header inherited from C's stdio.h, iostream provides basic input and output services for C++
programs.

C ++.‫توفر المدخالت والمخرجات الخدمات األساسية للبرامج‬iostream ‫ و‬، stdio.h C‫ الموروثة من‬cstdio ‫مثل رأس‬

iostream uses the objects cin, cout, cerr, and clog for sending data to and from the standard streams input,
output, error (unbuffered), and log (buffered) respectively.

‫ وتسد إلرسال البيانات من وإلى تيارات القياسية المدخالت والمخرجات والخطأ (غير‬،cerr ،cout ، CIN‫ الكائنات‬iostream ‫يستخدم‬
.‫ وسجل (مخزنة) على التوالي‬،)‫مصقول‬

As part of the C++ standard library, these objects are a part of the std namespace.

.‫ هذه الكائنات هي جزء من مساحة األمراض المنقولة جنسيا‬، C ++‫كجزء من المكتبة القياسية في‬

 Header files

‫رأس الملفات‬
The classes of the input/output library reside in several headers.

<ios> contains the definitions of ios_base and basic_ios classes, that manage formatting information and the
associated stream buffer.

.‫ التي تدير معلومات التنسيق والمخزن المؤقت تيار المرتبطة بها‬،basic_ios‫ و‬ios_base ‫يحتوي على تعريفات الطبقات‬

<istream> contains the definition of basic_istream class template, which implements formatted input.

.‫ الذي ينفذ المدخالت منسق‬، basic_istream‫يحتوي على تعريف قالب فئة‬

<ostream> contains the definition of basic_ostream class template, which implements formatted output.

.‫ الذي ينفذ الناتج منسق‬، basic_ostream‫يحتوي على تعريف قالب فئة‬

<iostream> contains the definition of basic_iostream class template, which implements formatted input and
output.

By: Mrs. Afreen Mukhtar Solkar 30


.‫ الذي ينفذ المدخالت والمخرجات مهيأ‬، basic_iostream‫يحتوي على تعريف قالب فئة‬

<fstream> contains the definitions of basic_ifstream, basic_ofstream and basic_fstream class templates which
implement formatted input, output and input/output on file streams.

/ ‫ التي تنفذ تنسيق إدخال واإلخراج واإلدخال‬basic_fstream ‫والطبقة‬basic_ofstream ، basic_ifstream‫يحتوي على تعريفات قوالب‬
.‫اإلخراج على تدفقات الملف‬

<sstream> contains the definitions of basic_istringstream, basic_ostringstream and basic_stringstream class


templates which implement formatted input, output and input/output on string-based streams.

‫ التي تنفذ تنسيق إدخال‬basic_stringstream ‫والطبقة‬basic_ostringstream ، basic_istringstream‫يحتوي على تعريفات قوالب‬


.‫ اإلخراج على تيارات القائم على السلسلة‬/ ‫واإلخراج واإلدخال‬

<iomanip> contains formatting manipulators.

.‫يحتوي على المتالعبين تنسيق‬

<iosfwd> contains forward declarations of all classes in the input/output library.

.‫ اإلخراج‬/ ‫يحتوي اإلعالنات إلى األمام من جميع الفئات في مكتبة اإلدخال‬

 Looping/ Control Statements


‫ التحكم‬/ ‫ البيانات حلقات‬

Loop types ‫أنواع حلقة‬


There may be a situation, when you need to execute a block of code several number of times. In general,
statements are executed sequentially:
:‫ يتم تنفيذها البيانات بالتتابع‬،‫ بشكل عام‬.‫ عندما تحتاج إلى تنفيذ كتلة من التعليمات البرمجية عدة عدة مرات‬،‫قد يكون هناك الوضع‬

The first statement in a function is executed first, followed by the second, and so on.
.‫ وهلم جرا‬،‫ تليها في المرتبة الثانية‬،‫يتم تنفيذ العبارة األولى في وظيفة أوال‬

Programming languages provide various control structures that allow for more complicated execution paths.
.‫توفر لغات البرمجة هياكل المراقبة المختلفة التي تسمح لمسارات تنفيذ أكثر تعقيدا‬

A loop statement allows us to execute a statement or group of statements multiple times and following is the
general from of a loop statement in most of the programming languages:

:‫وجاء في بيان حلقة يسمح لنا لتنفيذ بيان أو مجموعة من البيانات عدة مرات وفيما يلي العامة من بيان حلقة في معظم لغات البرمجة‬

By: Mrs. Afreen Mukhtar Solkar 31


Flowchart:

C++ programming language provides the following type of loops to handle looping requirements.

.‫ لغة برمجة النوع التالي من الحلقات على التعامل مع متطلبات حلقات‬++ C ‫يوفر‬

Loop Type Description

while loop Repeats a statement or group of statements while a


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

By: Mrs. Afreen Mukhtar Solkar 32


While Loop ‫حائط اللوب‬
A while loop statement repeatedly executes a target statement as long as a given condition is true.
.‫وجاء في بيان حلقة في حين ينفذ مرارا بيان الهدف ما دام شرط معين هو الصحيح‬

Syntax

The syntax of a while loop in C++ is:

while(condition)
{
statement(s);
}

Here, statement(s) may be a single statement or a block of statements.


.‫ قد يكون بيان (ق) بيان واحد أو كتلة من البيانات‬،‫هنا‬

The condition may be any expression, and true is any non-zero value.
‫ والصحيح هو أي قيمة غير الصفر‬،‫قد تكون حالة أي تعبير‬

The loop iterates while the condition is true.


.‫وتتكرر حلقة في حين أن الشرط صحيحا‬

When the condition becomes false, program control passes to the line immediately following the loop.
.‫ يمر برنامج مراقبة خط فورا بعد الحلقة‬،‫عندما تصبح حالة كاذبة‬

Flowchart

By: Mrs. Afreen Mukhtar Solkar 33


Example :

#include <iostream.h>
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

for Loop ‫لحلقة‬


A 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

The syntax of a for loop in C++ is:

for ( init; condition; increment )


{
statement(s);
}

By: Mrs. Afreen Mukhtar Solkar 34


Flowchart:

Example:

#include <iostream.h>
int main ()
{
// for loop execution
for( int a = 10; a < 20; a = a + 1 )

{
cout << "value of a: " << a << endl;
}
return 0;
}

By: Mrs. Afreen Mukhtar Solkar 35


Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

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.
.‫ في حين حلقة لتنفيذ مرة واحدة على األقل‬... ‫ إال أن مضمونة القيام به‬،‫ في حين حلقة مشابهة لحلقة في حين‬... ‫وبه‬
Syntax
The syntax of a do...while loop in C++ is:

do
{
statement(s);
}

while( condition );

Flowchart:

By: Mrs. Afreen Mukhtar Solkar 36


Example:

#include <iostream.h>
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}

Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

nested Loops: ‫حلقات متداخلة‬:


A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting.
.‫ مستويات التعشيش‬256 ‫يسمح على األقل‬. C ++ ‫يمكن أن تتداخل حلقة داخل حلقة أخرى‬
Syntax
The syntax for a nested for loop statement in C++ is as follows:

for ( init; condition; increment )


{
for ( init; condition; increment )
{
statement(s);
}
statement(s); // you can put more statements.
}

By: Mrs. Afreen Mukhtar Solkar 37


Example:

The following program uses a nested for loop to find the prime numbers from 2 to 10:

#include <iostream.h>
int main ()
{
int i, j;
for(i=2; i<10; i++)
{
for(j=2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) cout << i << " is prime\n";
}
return 0;

Output:

2 is prime
3 is prime
5 is prime
7 is prime

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.
.‫ ويتم تدمير كل الكائنات اآللية التي تم إنشاؤها في هذا النطاق‬،‫ عندما يترك تنفيذ نطاق‬.‫البيانات السيطرة حلقة تتغير التنفيذ من التسلسل العادي‬

C++ supports the following control statements.

.‫ يدعم البيانات التحكم التالية‬++ C

Control Statement Description


break statement Terminates the loop or switch statement and transfers
execution to the statement immediately following the
loop or switch.
continue statement Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
goto statement Transfers control to the labeled statement. Though it
is not advised to use goto statement in your program.

By: Mrs. Afreen Mukhtar Solkar 38


Break Statement ‫بيان استراحة‬
The break statement has the following two usages in C++:

C ++:‫البيان استراحة لديه األعراف اثنين التالية في‬

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

‫ يتم إنهاء فورا حلقة وبرنامج مراقبة يستأنف في البيان التالي لحلقة‬،‫ داخل حلقة‬break ‫عند مواجهة اإلفادة‬ 

 It can be used to terminate a case in the switch statement (covered in the next chapter).
(‫ويمكن استخدامه إلنهاء حالة في بيان التبديل (المشمولة في الفصل التالي‬ 

If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of
the innermost loop and start executing the next line of code after the block.
.
‫ وبيان كسر سيوقف تنفيذ حلقة أعمق وبدء تنفيذ السطر التالي من‬،)‫إذا كنت تستخدم الحلقات المتداخلة (أي حلقة واحدة داخل حلقة أخرى‬
.‫التعليمات البرمجية بعد كتلة‬
Syntax
The syntax of a break statement in C++ is:

break;

Flowchart:

By: Mrs. Afreen Mukhtar Solkar 39


Example :

#include <iostream.h>
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15)
{
// terminate the loop
break;
}
}while( a < 20 );
return 0;

Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

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.

‫ برنامج‬،‫ في حين تمرير التحكم الحلقات‬... ‫ وتفعل‬،‫ لبعض الوقت‬.‫ ومواصلة أسباب االختبار وزيادة أجزاء المشروط من الحلقة لتنفيذ‬،‫لللحلقة‬
.‫لالختبارات المشروطة‬

Syntax
The syntax of a continue statement in C++ is:
continue;

By: Mrs. Afreen Mukhtar Solkar 40


Flow Diagram

Example

#include <iostream.h>
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
do
{
if( a == 15)
{
// skip the iteration.
a = a + 1;
continue;
}
cout << "value of a: " << a << endl;
a = a + 1;
while( a < 20 );

return 0;
}

By: Mrs. Afreen Mukhtar Solkar 41


Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

goto Statement:
:‫غوتو بيان‬

A goto statement provides an unconditional jump from the goto to a labeled statement in the same function.

.‫وينص بيان غوتو قفزة غير المشروط من غوتو بيان وصفت في نفس الوظيفة‬

Syntax
The syntax of a goto statement in C++ is:

goto label;
..
.
label: statement;
Where label is an identifier that identifies a labeled statement. A labeled statement is any statement that is
preceded by an identifier followed by a colon (:).

Flowchart:

By: Mrs. Afreen Mukhtar Solkar 42


Example:

#include <iostream.h>
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
LOOP:do
{
if( a == 15)
{
// skip the iteration.
a = a + 1;
goto LOOP;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;

Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

 Decision-making statements:
:‫البيانات صنع القرار‬
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.

‫ جنبا إلى جنب مع بيان أو‬،‫تتطلب هياكل صنع القرار أن مبرمج تحديد واحد أو أكثر من الشروط التي سيتم تقييمها أو اختبارها من قبل البرنامج‬
‫ تصريحات أخرى ليتم تنفيذها إذا كان الشرط عازمة على أن تكون‬،‫ واختياريا‬،‫البيانات ليتم تنفيذها إذا تم تحديد شرط أن يكون ذلك صحيحا‬
.‫كاذبة‬

By: Mrs. Afreen Mukhtar Solkar 43


Following is the general from of a typical decision making structure found in most of the programming
languages:

:‫تبعه عام من هيكل صنع القرار نموذجية وجدت في معظم لغات البرمجة‬

C++ programming language provides following types of decision making statements.

.‫ لغة برمجة األنواع التالية من البيانات صنع القرار‬C ++ ‫يوفر‬

Statement Description
if statement An ‘if’ statement consists of a boolean expression
followed by one or more statements.
if...else statement An ‘if’ statement can be followed by an optional
‘else’ statement, which executes when the boolean
expression is false.
switch statement A ‘switch’ statement allows a variable to be tested
for equality against a list of values.
nested if statements You can use one ‘if’ or ‘else if’ statement inside
another ‘if’ or ‘else if’ statement(s).
nested switch statements You can use one ‘switch’ statement inside another
‘switch’ statement(s).

By: Mrs. Afreen Mukhtar Solkar 44


If Statement:
‫إذا اإلعالن‬
An if statement consists of a boolean expression followed by one or more statements.
.‫يتكون بيان إن تعبير منطقي تليها بيانات واحدة أو أكثر‬

Syntax

The syntax of an if statement in C++ is:

if(boolean_expression)
{
// statement(s) will execute if the boolean expression is true
}

If the boolean expression evaluates to true, then the block of code inside the if statement will be executed.

.‫ ثم كتلة من التعليمات البرمجية داخل إذا البيان انه سيتم تنفيذها‬، true‫إذا تقييم التعبير المنطقي إلى‬

If boolean expression evaluates to false, then the first set of code after the end of the if statement (after the
closing curly brace) will be executed.

.‫ ثم أول مجموعة من التعليمات البرمجية بعد نهاية إذا بيان (بعد متعرج إغالق) سيتم تنفيذها‬،‫إذا تقييم التعبير المنطقي لكاذبة‬

Flowchart:

By: Mrs. Afreen Mukhtar Solkar 45


Example:

#include <iostream.h>
int main ()
{
// local variable declaration:
int a = 10;
// check the boolean condition
if( a < 20 )
{
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}

Output:

a is less than 20;


value of a is : 10

if…else Statement:
:‫ بيان آخر‬... ‫إذا‬
An if statement can be followed by an optional else statement, which executes when the boolean expression is
false.
.‫ والتي يتم تنفيذها عند التعبير منطقية غير صحيح‬،‫يمكن اتباعها لبيان إذا كان عن طريق بيان آخر اختياري‬

Syntax
The syntax of an if...else statement in C++ is:

if(boolean_expression)
{
// statement(s) will execute if the boolean expression is true
}
else
{
// statement(s) will execute if the boolean expression is false
}

If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of
code will be executed.
.‫ وسيتم تنفيذ كتلة خالف آخر من التعليمات البرمجية‬،‫ ثم إذا كان سيتم تنفيذها كتلة من التعليمات البرمجية‬، true‫إذا تقييم التعبير المنطقي إلى‬

By: Mrs. Afreen Mukhtar Solkar 46


Flowchart:

Example:

#include <iostream.h>
int main ()
{
// local variable declaration:
int a = 100;
// check the boolean condition
if( a < 20 )
{
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
else
{
// if condition is false then print the following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}

Output:
a is not less than 20;
value of a is : 100

By: Mrs. Afreen Mukhtar Solkar 47


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
The syntax for a switch statement in C++ is as follows:

switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
// you can have any number of case statements.
default : //Optional
statement(s);
}

Flowchart:

By: Mrs. Afreen Mukhtar Solkar 48


Example:
#include <iostream.h>
int main ()
{
// local variable declaration:
char grade = 'D';
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
Output:
You passed
Your grade is D

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
The syntax for a nested if statement is as follows:
if( boolean_expression 1)
{
// Executes when the boolean expression 1 is true
if(boolean_expression 2)
{
// Executes when the boolean expression 2 is true
}
}

By: Mrs. Afreen Mukhtar Solkar 49


Example:
#include <iostream.h>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
// check the boolean condition
if( a == 100 )
{
// if condition is true then check the following
if( b == 200 )
{
// if condition is true then print the following
cout << "Value of a is 100 and b is 200" << endl;
}

}
cout << "Exact value of a is : " << a << endl;
cout << "Exact value of b is : " << b << endl;
return 0;
}

Output:

Value of a is 100 and b is 200


Exact value of a is : 100
Exact value of b is : 200

 Arrays
‫ صالحة‬
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same
type.

.‫ والذي يقوم بتخزين ذات حجم ثابت جمع متتابعة من العناصر من نفس النوع‬،‫ ومجموعة‬،‫ بنية بيانات‬C ++ ‫يوفر‬

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of
variables of the same type.

‫ ولكن غالبا ما يكون أكثر فائدة للتفكير في مجموعة كمجموعة من المتغيرات من نفس‬،‫يتم استخدام مجموعة لتخزين مجموعة من البيانات‬
.‫النوع‬

By: Mrs. Afreen Mukhtar Solkar 50


Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array
variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual
variables.

‫ تقوم بتعريف متغير مجموعة واحدة مثل أرقام وأرقام‬،number99‫ و‬،... ‫ و‬،number1 ، number0‫ مثل‬،‫بدال من تصريح المتغيرات الفردية‬
.‫] لتمثيل المتغيرات الفردية‬99[ ‫ وأرقام‬،... ‫ و‬،]1[ ‫ أرقام‬،]0[ ‫استخدام‬

A specific element in an array is accessed by an index.

.‫يتم الوصول لعنصر معين في مجموعة من فهرس‬

 Declaring Arrays:
:‫ إعالن صالحة‬
To declare an array in C++, the programmer specifies the type of the elements and the number of elements
required by an array as follows:
:‫ يحدد مبرمج نوع من العناصر وعدد من العناصر المطلوبة من قبل مجموعة على النحو التالي‬، C ++‫لإلعالن صفيف في‬

type arrayName [ arraySize ];

This is called a single-dimension array.


‫وهذا ما يسمى مجموعة البعد واحد‬

The array Size must be an integer constant greater than zero and type can be any valid C++ data type.
.‫ نوع بيانات صالحة‬C ++ ‫يجب أن يكون مجموعة الحجم عدد صحيح ثابت أكبر من الصفر ونوع يمكن أن يكون أي‬

For example, to declare a 10-element array called balance of type double, use this statement:
:‫ استخدم هذا البيان‬،‫ عنصرا يسمى توازن من نوع مزدوج‬10 ‫ إلعالن مجموعة‬،‫على سبيل المثال‬

double balance[10];

 Initializing Arrays:
:‫ تهيئة صالحة‬
You can initialize C++ array elements either one by one or using a single statement as follows:
:‫ إما واحدا تلو اآلخر أو باستخدام عبارة واحدة على النحو التالي‬C ++ ‫يمكنك تهيئة عناصر مجموعة‬

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } cannot be larger than the number of elements that we declare for the
array between square brackets [ ].
[].‫عدد القيم بين األقواس {} ال يمكن أن يكون أكبر من عدد من العناصر التي نعلن للمجموعة بين األقواس المربعة‬

Following is an example to assign a single element of the array:


:‫التالي مثال لتعيين عنصر واحد من مجموعة‬

By: Mrs. Afreen Mukhtar Solkar 51


If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you
write:
:‫ إذا كنت أكتب‬،‫ لذا‬.‫ يتم إنشاء مجموعة فقط كبيرة بما يكفي لعقد التهيئة‬،‫إذا قمت بحذف حجم المصفوفة‬
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example.
.‫سوف تقوم بإنشاء بالضبط نفس مجموعة كما فعلت في المثال السابق‬

balance[4] = 50.0;

 Arrays in C++:
C ++:‫ المصفوفات في‬

Arrays are important to C++ and should need lots of more detail. There are following few important concepts,
which should be clear to a C++ programmer:

‫ هناك التالية بعض المفاهيم المهمة التي يجب أن تكون واضحة‬.‫ وينبغي بحاجة الى الكثير من المزيد من التفاصيل‬،C ++‫صفائف مهمة ل‬
C ++:‫لمبرمج‬

Concept Description

Multi-dimensional arrays C++ supports multidimensional arrays. The simplest


form of the multidimensional array is the two-
dimensional array.
Pointer to an array You can generate a pointer to the first element of an
array by simply specifying the array name, without
any index.
Passing arrays to functions You can pass to the function a pointer to an array by
specifying the array's name without an index.
Return array from functions C++ allows a function to return an array.

Multi-dimensional Arrays:
:‫المصفوفات متعددة األبعاد‬
C++ allows multidimensional arrays. Here is the general form of a multidimensional array declaration:
:‫ هنا هو الشكل العام إلعالن مجموعة متعددة األبعاد‬.‫يسمح المصفوفات متعددة األبعاد‬C ++

type name[size1][size2]...[sizeN];

For example, the following declaration creates a three dimensional 5 . 10 . 4 integer array:

:‫ صحيح مجموعة‬4 .10 .‫ الثالث‬5 ‫ التصريح التالي الذي يخلق األبعاد‬،‫على سبيل المثال‬

int threedim[5][10][4];

By: Mrs. Afreen Mukhtar Solkar 52


Two-Dimensional Arrays
‫ثنائي األبعاد صفائف‬

The simplest form of the multidimensional array is the two-dimensional array.


.‫أبسط شكل مجموعة متعددة األبعاد هو صفيف ثنائي األبعاد‬

A two-dimensional array is, in essence, a list of one-dimensional arrays.


.‫ وقائمة من صفائف أحادية البعد‬،‫ في جوهرها‬،‫وهناك مجموعة ثنائية األبعاد هي‬

To declare a two-dimensional integer array of size x,y, you would write something as follows:
:‫ وكنت أكتب شيئا على النحو التالي‬،‫ ص‬،‫إلعالن مجموعة عددية ثنائية األبعاد من حجم س‬
type arrayName [ x ][ y ];

Where type can be any valid C++ data type and arrayName will be a valid C++ identifier.
.‫ معرف‬C ++ ‫ نوع البيانات ومجموعة اسم سيكون ساري المفعول‬C ++ ‫حيث نوع يمكن أن يكون أي صحيح‬

A two-dimensional array can be think as a table, which will have x number of rows and y number of columns.

.‫ والتي سيكون لها × عدد الصفوف وعدد ذ األعمدة‬،‫وهناك مجموعة ثنائية األبعاد يمكن أن يفكر كجدول‬

A 2-dimensional array a, which contains three rows and four columns can be shown as below:

:‫ والتي تحتوي على ثالثة صفوف وأربعة أعمدة على النحو التالي‬،‫األبعاد لذلك‬-2 ‫يمكن أن تظهر هناك مجموعة‬

Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the
array, and i and j are the subscripts that uniquely identify each element in a.

‫ هي السفلية التي تنفرد‬j ‫و‬i ‫ و‬،‫ حيث هو اسم مجموعة‬،]‫ يتم تحديد كل عنصر في مجموعة ومن اسم عنصر النموذج على [أنا] [ي‬،‫وبالتالي‬
.‫بتحديد كل عنصر في‬

By: Mrs. Afreen Mukhtar Solkar 53


 Functions
‫ المهام‬
A function is a group of statements that together perform a task.
.‫وظيفة هي مجموعة من البيانات التي تؤدي معا مهمة‬

Every C++ program has at least one function, which is main(), and all the most trivial programs can define
additional functions.
‫ ولكن منطقيا تقسيم عادة هو من هذا‬،‫ كيفية تقسيم الشفرة بين وظائف مختلفة متروك لكم‬.‫يمكنك تقسيم التعليمات البرمجية إلى وظائف منفصلة‬
‫القبيل أن كل وظيفة بأداء مهمة معينة‬

You can divide up your code into separate functions. How you divide up your code among different functions is
up to you, but logically the division usually is such that each function performs a specific task.

.‫ ويمكن لجميع برامج أتفه تحديد الوظائف اإلضافية‬،)( ‫ وهو الرئيسية‬،‫ لديه وظيفة واحدة على األقل‬C ++ ‫كل برنامج‬

 Defining a Function:
:‫ تحديد وظيفة‬

The general form of a C++ function definition is as follows:


:‫ هي كما يلي‬C ‫ تعريف الدالة‬++‫الشكل العام ل‬

return_type function_name( parameter list )


{
body of the function
}

A C++ function definition consists of a function header and a function body. Here are all the parts of a function:

:‫ هنا جميع أجزاء من وظيفة‬.‫ من ضربة رأس وظيفة وظيفة الجسم‬C ‫ تعريف الدالة‬++ ‫يتكون‬

 Return Type: A function may return a value. The return_type is the data type of the value the function
returns. Some functions perform the desired operations without returning a value. In this case, the
return_type is the keyword void.

‫ بعض أداء مهام العمليات المطلوبة دون‬.‫هو نوع البيانات من قيمة ترجع الدالة‬return_type ‫ و‬.‫ وظيفة قد ترجع قيمة‬:‫عودة النوع‬ 
.‫هو الفراغ الكلمة‬return_type ،‫ في هذه الحالة‬.‫إرجاع قيمة‬

 Function Name: This is the actual name of the function. The function name and the parameter list
together constitute the function signature.

.‫ اسم الدالة وقائمة المعلمة تشكل معا التوقيع وظيفة‬.‫ هذا هو االسم الفعلي الدالة‬:‫وظيفة اسم‬ 

By: Mrs. Afreen Mukhtar Solkar 54


 Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The parameter list refers to the
type, order, and number of the parameters of a function. Parameters are optional; that is, a function may
contain no parameters.

‫ ويشار إلى هذه القيمة إلى المعلمة كما الفعلية‬.‫ يمكنك تمرير قيمة إلى المعلمة‬،‫ عندما يتم استدعاء وظيفة‬.‫ معلمة يشبه نائبا‬:‫المعلمات‬ 
‫ قد تحتوي على وظيفة‬،‫ وهذا هو‬.‫ معلمات اختيارية‬.‫ وعدد من المعلمات وظيفة‬،‫ والنظام‬،‫ تشير قائمة المعلمة إلى نوع‬.‫أو وسيطة‬
.‫بدون معلمات‬

 Function Body: The function body contains a collection of statements that define what the function
does.
.‫ وظيفة الجسم يحتوي على مجموعة من البيانات التي تحدد ما تفعله وظيفة‬:‫ وظيفة الجسم‬

• File Processing:
:‫• معالجة الملف‬
Basic input/output

‫ اإلخراج األساسي‬/ ‫إدخال‬

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent
chapters.
.‫ اإلخراج التي سنرى في الفصول الالحقة‬/ ‫ مجموعة واسعة من القدرات اإلدخال‬C ++ ‫توفر المكتبات القياسية‬

This chapter will discuss very basic and most common I/O operations required for C++ programming.

C ++.‫ الالزمة لبرمجة‬I / ‫وهذا الفصل يناقش يا العمليات األساسية جدا واألكثر شيوعا‬

C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device likes a keyboard, a
disk drive, or a network connection etc. to main memory, this is called input operation and if bytes flow from
main memory to a device likes a display screen, a printer, a disk drive, or a network connection, etc., this is
called output operation.

‫ أو اتصال الشبكة‬،‫ محرك األقراص‬،‫ إذا تدفق بايت من جهاز يحب لوحة المفاتيح‬.‫ التي هي تسلسل بايت‬،‫يحدث في مجاري المياه‬C ++ I / O
‫ محرك أقراص‬،‫ طابعة‬،‫ وهذا ما يسمى عملية اإلدخال وإذا تدفق بايت من الذاكرة الرئيسية للجهاز يحب شاشة العرض‬،‫وما إلى الذاكرة الرئيسية‬
.‫ وهذا ما يسمى عملية االنتاج‬،‫ وما إلى ذلك‬،‫أو اتصال الشبكة‬

By: Mrs. Afreen Mukhtar Solkar 55


• I/O Library Header Files:
:‫مكتبة رأس الملفات‬I / O •

There are following header files important to C++ programs:

C ++:‫هناك التالية رأس الملفات الهامة لبرامج‬

Header File Function and Description


<iostream> This file defines the cin, cout, cerr and clog objects,
which correspond to the standard input stream, the
standard output stream, the un-buffered standard error
stream and the buffered standard error stream,
respectively.
<iomanip> This file declares services useful for performing
formatted I/O with so-called parameterized stream
manipulators, such as setw and setprecision.
<fstream> This file declares services for user-controlled file
processing. We will discuss about it in detail in File
and Stream related chapter.

• The Standard Output Stream(cout)


(cout)‫• دفق إخراج قياسي‬

The predefined object cout is an instance of ostream class.

ostream.‫كائن محدد مسبقا هو مثيل لفئة‬cout ‫و‬

The cout object is said to be "connected to" the standard output device, which usually is the display screen.

.‫ والتي عادة هو شاشة العرض‬،‫ إلى أن "متصلة" جهاز اإلخراج القياسية‬cout ‫ويقال الكائن‬

The cout is used in conjunction with the stream insertion operator, which is written as << which are two less
than signs as shown in the following example.

.‫ التي كما هو مكتوب << وهما أقل من عالمات كما هو موضح في المثال التالي‬،‫ بالتعاون مع المشغل تيار اإلدراج‬cout ‫يتم استخدام‬

By: Mrs. Afreen Mukhtar Solkar 56


• The Standard Input Stream (cin)
(cin)‫• تيار اإلدخال القياسي‬

The predefined object cin is an instance of istream class.


istream.‫كائن محدد مسبقا هو مثيل لفئة‬CIN ‫و‬

The cin object is said to be attached to the standard input device, which usually is the keyboard.
.‫ والتي عادة ما يكون لوحة المفاتيح‬،‫ لضمها الى جهاز اإلدخال القياسية‬CIN ‫ويقال الكائن‬

The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater
than signs as shown in the following example.

.‫ والتي كما هو مكتوب >> وهما أكبر من عالمات كما هو موضح في المثال التالي‬،‫ بالتعاون مع المشغل استخراج تيار‬CIN ‫يتم استخدام‬

#include <iostream.h>
int main( )
{
char name[50];
cout << "Please enter your name: ";
cin >> name;
cout << "Your name is: " << name << endl;
}

Output:
Please enter your name: afreen
Your name is: afreen

• Structures and Classes


‫• الهياكل وفئات‬
Structures ‫الهياكل‬
• A structure is a set of elements of any type (except for the void type). Thus, the structure combines
logically related data of different types.

‫ فإن هيكل يجمع البيانات ذات الصلة منطقيا من‬،‫ وهكذا‬.)‫هيكل عبارة عن مجموعة من العناصر من أي نوع (باستثناء نوع الفراغ‬ •
.‫أنواع مختلفة‬

By: Mrs. Afreen Mukhtar Solkar 57


Structure Declaration ‫إعالن الهيكل‬
• The structure data type is determined by the following description:

‫يتم تحديد نوع البيانات بنية من الوصف التالي‬ •

• struct structure_name
{
elements_description
};

• The structure name can't be used as an identifier (name of a variable or function).

(‫ال يمكن استخدام اسم هيكل كمعرف (اسم متغير أو وظيفة‬ •

• It should be noted that in MQL4 structure elements follow one another directly, without alignment.

.‫ دون المحاذاة‬،‫ عناصر هيكل الواحدة تلو األخرى مباشرة‬MQL4 ‫وتجدر اإلشارة إلى أنه في‬ •

• In C++ such an order is made to the compiler using the following instruction:

‫ يتم مثل هذا األمر إلى مترجم باستخدام التعليمات التالية‬C ++ ‫في‬ •

• If you want to do another alignment in the structure, use auxiliary members, "fillers" to the right size.

‫ "الحشو" إلى الحجم الصحيح‬،‫ واستخدام أعضاء المساعدة‬،‫إذا كنت تريد أن تفعل محاذاة أخرى في الهيكل‬ •

Classes ‫فصول‬
• Classes differ from structures in the following:

:‫الطبقات تختلف من الهياكل في ما يلي‬ •

• The keyword class is used in declaration;

‫يتم استخدام الفئة الكلمات الرئيسية في اإلعالن؛‬

• By default, all class members have access specified private, unless otherwise indicated.

.‫ ما لم يذكر خالف ذلك‬،‫ جميع أعضاء الفئة من الوصول المحدد الخاص‬،‫افتراضيا‬ •

• Data-members of the structure have the default type of access as public, unless otherwise indicated;

.‫ ما لم يذكر خالف ذلك‬،‫بيانات أعضاء بنية يكون النوع االفتراضي من وصول وعلنية‬ •

• Class OBJECTS always have a table of virtual functions, even if there are no virtual functions declared
in the class. Structures can't have virtual functions;

‫ هياكل ال يمكن أن يكون لها وظائف‬.‫ حتى إذا لم تكن هناك وظائف افتراضية أعلن في الصف‬،‫كائنات فئة دائما جدول وظائف افتراضية‬ •
‫افتراضية‬

By: Mrs. Afreen Mukhtar Solkar 58

You might also like