0% found this document useful (0 votes)
13 views26 pages

Programming Concepts With C++1

ملخص

Uploaded by

ameshtawy46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views26 pages

Programming Concepts With C++1

ملخص

Uploaded by

ameshtawy46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

‫مبدئيا البرمجة سهلة جدا وممتعه جدا‪،،‬‬

‫البرمجه مابيشتغلش فيها ال العباقرة؟ خطأ‬


‫البرمجة مابشتغلش فيها ال خريجين حاسبات؟ خطأ‬
‫في ناس زمايلنا واصدقاءنا أفضل مننا خريجين كليات أدبيه وبيشتغلو معانا وماشاء ا اصبحو تيم ليدرز‬
‫وبروجيكت مانيجرز ووووو وتقدمو تقدم رهيب عن المتخصصين‪ ..‬ليه؟ لن هم أحبو الموضوع وفهمو‬
‫الموضوع‪..‬‬
‫البرمجة لما نيجي نتعامل معاها مش عايزين نتعامل معاها على انها شيء هلمي‪،،‬‬
‫فكرة البرمجه كلها هي إن البرمجة طريقة حل مشكلة معينه‪.‬‬
‫انت عندك مشكله وعشان تحلها بتفكر ايه هي خطوات حل المشكلة دي‪ ،،‬مال‪.4،3،2،1‬‬
‫خطوات حل المشكله دي لزم نفهمها للكومبيوتر‪ ،‬وزي ماقلنا الكومبيوتر مش بيفهم لغتنا فعشان نفهمه‬
‫بنستخدم لغة برمجه‪..‬‬
‫كأنك بالظبط بهندس عربي وعندك عمال هنود مال‪ ..‬انت كمهندس بتضع الحل ومحتاج العمال ينفذو‬
‫الحل فمحتاج انهم يفهمو ومحتاج مترجم وسيط بينكم‪.‬‬
‫طيب بالنسبة للمنهج هو عباره عن اساسيات البرمجه‪ ،‬وهي واحده لجميع اللغات‪.‬‬
‫يبقى المطلوب منك انك تاخد الموضوع بمتهى البساطه جدا وتمشي معايا واحده واحده‪ ،‬وهتلقي نفسك‬
‫وصلت لدرجة انت ماكنتش متخيلها ان شاء ا‪ .‬دي مقدمه بسيطه عشان نبدأ مع بعض واحنا مرتاحين‬
What Is Programming and Program Development Life Cycle ?

• Programming is a process of problem solving

• Step 1: Analyze the problem

– Outline the problem and its requirements

– Design steps (algorithm) to solve the problem

• Step 2: Implement the algorithm

– Implement the algorithm in code

– Verify that the algorithm works

• Step 3: Maintenance

– Use and modify the program if the problem


domain changes
Algorithm:

– Step-by-step problem-solving process

2
The Problem Analysis–Coding–Execution Cycle
• Understand the Overall problem
• Understand problem requirements
– Does program require user interaction?
– Does program manipulate data?
– What is the output? ‫افهم المشكله بشكل عام‬
• If the problem is complex, divide it into subproblems ‫واعرف هل مطلوب تفاعل مع اليوزر‬
‫ما هي القوانين اللي هستخدمها‬
– Analyze each subproblem as above ‫ماهي المدخلت والمخرجات؟‬

‫ الفضل اقسم‬،‫ إذا كانت المشكله معقده او كبيرة‬:‫نصيحة مهمه‬


• Run code through compiler ‫ ليسهل حل هذه المشاكل؟‬..‫المشكله الكبيرة الى مشاكل صغيره‬
• If compilergenerates errors
– Look at code and remove errors
– Run code again through compiler
• If there are no syntax errors
– Compiler generates equivalent machine code
Linker links machine code with system resources

• Once compiled and linked, loader can place program into main memory for execution
• The final step is to execute the program
• Compiler guarantees that the program follows the rules of the language
– Does not guarantee that the program will run correctly
The Language of a Computer

• Machine language: language of a computer .‫اللغة اللي بيفهمها الكومبيوتر هي الصفار والوحايد فقط‬
• Binary digit (bit): The digit 0 or 1 ‫لما يكون عندي سيكونس من الصفار والوحايد يسمى الباينري كود‬
• Binary code: A sequence of 0s and 1s .‫وهذه هي اللغه الوحيده التي يفهمها الكومبيوتر‬
• Byte: A sequence of eight bits

The Evolution of Programming Languages

• Early computers were programmed in machine language


• To calculate wages = rates * hoursin machine language:

100100 010001 //Load ‫كان الول لزم تكتب الكواد بالشكل دا كدا‬
!!‫ ودا صعب جدا‬،‫اصفار ووحايد‬
100110 010010 //Multiply ..‫فعملو مايسمى بلغة السيمبلي وفيها يتم استخدام رموز‬

100010 010011 //Store

• Assemblylanguage instructions are mnemonic


• Assembler: translates aprogram written in assemblylanguage into machine language
‫يعني كلمات مقابلها أكواد‪ ،،‬بمعنى لو عايز اكتب المر لوود مال‬
‫هيكون مقابل له بلغو الله ‪ 100100‬وهكذا ‪ ،‬بدل ما نكتب‬
‫الصفار والوحايد دي انا هكتب بس الوامر اللي هيا ‪Load ,‬‬
‫‪stor, add, sub‬وهكذا وهيتم ترجمتها للصفار والوحايد عشان‬
‫الكومبيوتر يفهمها‪.‬‬
‫يبقى انا كدا محتاج مترجم‪ ،‬في لغة السيمبلي المترجم يسمى‬
‫‪Assembler‬‬

‫‪Using assemblylanguage instructions, wages = rates • hourscan‬‬


‫‪be written as:‬‬

‫‪LOAD‬‬ ‫‪rate‬‬
‫‪MULT‬‬ ‫‪hour‬‬
‫‪STOR‬‬ ‫‪wage‬‬

‫•‬ ‫‪High-level languages include Basic, FORTRAN, COBOL, Pascal,C, C++, C#, and Java‬‬
‫•‬ ‫‪Compiler: translates a program written in a high-level language machine language‬‬
‫•‬ ‫;‪The equation wages = rate • hours can be written in C++ as: wages = rate * hours‬‬

‫بعد السمبلي‪ ،‬الموضوع اتطور وظهر ما يسمى ‪ High-level languages‬وهي بيتم فيها كتابة مفهومه باللغه النجليزية‪ ،‬لكن بردو‬
‫الكومبيوتر مش هيفهمها‪ ،‬ومحتاج مترجم يترجمها الى الصفار والوايد‪ ،‬والمترجم دا يسمى ‪Compiler:‬‬
‫‪The Problem Analysis–Coding–Execution Cycle‬‬

‫‪Problem Analysis‬‬
‫هنا ملخص كل اللي فات‪:‬‬
‫أول حاجه بحلل المشكله وادرسها‬
‫واعرف ال ‪input‬وال ‪output‬‬
‫‪Using Any‬‬ ‫‪Algorithm Design‬‬ ‫والقوانين اللي هستخدمها‪.‬‬
‫‪Programming‬‬ ‫ام هضع الحل في صورة‬
‫‪Language‬‬
‫خطوات (‪.)Algorithm‬‬
‫ام تأتي خطوة الكود ودا باستخدام‬
‫اي لغة برمجه‪.‬‬
‫‪Coding‬‬ ‫محتاجين ‪compiler‬يحول لغة‬
‫البرمجه من الـ ‪high level‬‬
‫‪language‬الى الـ ‪low level‬‬
‫‪language‬‬
‫‪compiler‬‬ ‫وايضا من وظائف الـ‬
‫‪errors‬‬ ‫‪ Compiler‬انه يكتشف الخطاء‬
‫‪Translate code from‬‬
‫‪high-level language‬‬
‫في الكود واانت تصححها ‪.‬‬
‫‪to low-level language‬‬ ‫بعدها يتم مرحلة التنفيذ ‪.‬‬
‫ام النتيجة‪.‬‬
‫‪Execution‬‬
‫‪errors‬‬
‫وممكن ااناء التنفيذ يحصل‬
‫ايرورفبنرجع تاني نعيد تحليل‬
‫المشكله من البداية‬
‫‪Get results‬‬

‫‪10‬‬
‫يبقى ملخص كل اللي فات‪:‬‬
‫اني افكر في المشكله ام أكتب الكود‪.‬‬
‫الن هنتعلم ازاي هنكتب الكود !!‬
‫وهنتعرف الول على بعض المفاهيم‪..‬‬

‫‪Then‬‬
‫‪Think‬‬ ‫‪Write Code‬‬

‫‪11‬‬
‫‪Basic Definitions‬‬
‫•‬ ‫‪Programming language: a set of rules, symbols, and special words used to write computer programs.‬‬
‫•‬ ‫‪Computer program Sequence of statements whose objective is to accomplish a task.‬‬
‫•‬ ‫‪Syntax: rules that specify which statements (instructions) are legal‬‬

‫يبقى ملخص كل اللي فات‪:‬‬


‫اني افكر في المشكله ام أكتب الكود‪.‬‬
‫الن هنتعلم ازاي هنكتب الكود !!‬
‫وهنتعرف الول على بعض المفاهيم‪..‬‬
‫‪ ‬لغة البرمجة ه مجموعه من القواعد والرموز والكلمات الخاصة تستخدم لكتابة البرنامج‪.‬‬
‫‪ ‬برنامج الكومبيوتر هو مجموعه متتالية من الجمل وظيفتها انجاز مهمة معينه‪.‬‬
‫‪ ‬ال ‪syntax‬وهي كلمة هنستخدمها كتير جدا‪ ،‬وهي عبارة عن قواعد بنطبقها على الجمل اللي كتبناها عشان نشوف هل الجمل دي سليمة من‬
‫وجهة نظر لغة البرمجه اللي بكتب بيها ول ل‪ syntaxerror .‬يعني خطأ لغوي‪ ،‬حرف ناقص من الكلمه او حرف زائد وهكذا‪...‬‬

‫‪12‬‬
‫‪Computer System‬‬

‫‪Oper‬‬
‫‪2‬‬

‫‪Oper‬‬

‫‪Input‬‬ ‫‪2‬‬
‫‪Operation‬‬
‫‪Output‬‬
‫‪1‬‬

‫‪Processing‬‬

‫لو فهمت الرسمة دي‪ ،‬اي كود هيكون سهل ان شاء ا‬


‫أي نظام كومبيوتر عبارة عن مدخلت‪ ،‬ام معالجه أو عملياتام مخرجات‪.‬‬
‫المستخدم ممكن هو بيدخل ال‪ input‬وبيعرف الناتج اللي هو ‪ ، output‬لكن اليوزر مش عارف اللي بيحصل جوا البروسسيسسنج‪.‬‬

‫‪14‬‬
 Example 2

• Write a program to find the Area of a rectangle


The area of the Rectangle are given by the following formula: Area = Rect Length * Rect Width.
Input : Rectangle Length , Rectangle Width.
Processing : Area = Rect Length * Rect Width. :‫نجرب نطبق‬
Output : Print Out The area. .‫مطلوب برنامج يحسب مساحة المستطيل‬
‫احدد المدخلت وهي الطول والعرض‬
 Example 2 ‫ هي عملية الضربز‬: ‫البروسسيسينج اللي هيتم‬
.‫المخرجت هي مساحة المستطيل‬
• Write a program to find the perimeter and area of a square
The perimeter and area of the square are given by the followingformulas:
perimeter = Side Length * 4
area = Side Length * Side Length
Input : Square Side Length.
Processing :perimeter = Side Length * 4.
area = Side Length * Side Length
Output :Print Out The Perimeter and Area

14
Your First C++ Program
#include <iostream>
usingnamespace std; C++ ‫هنكتب أول برنامج لينا بلغة‬
int main() DevC++ ‫ اسمه‬platform ‫احنا هنستخدم‬
{ File=>New=>New ‫هفتحه وافتح‬
cout << "My first C++ program." << endl; project=>Console Application
return0;
} ‫ هو المسؤول عن تحويل الكود اللي‬compiler‫الـ‬
Sample Run: ‫هكتبه من لغة الهاي ليفل الى لغة الصفار والوحايد‬
‫ وايضا هو اللي هيعرفني الخطاء اللي ممكن‬،
My first C++ program.
.‫اعملها في الكود‬
Processing a C++ Program

• To execute a C++ program: ‫ الجزء البيض اللي‬،‫البرنامج عباره عن جزئين‬


‫ والجزء الااني‬text editor ‫بكتب فيه الكود اسمه‬
– Use an editor to create a source program in C++
‫ والشاشه السوداء اللي بيظهر‬compiler‫وهو ال‬
– Preprocessor directives begin with # and are processedby a the preprocessor console ‫عليها ناتج الكود اسمها شاشة ال‬
– Use the compiler to:
• Check that the program obeys the rules
• Translate into machine language (object program)
– Linker:
• Combines object program with other programs provided by the SDK to create executable code
– Loader:
• Loads executable program into main memory
– The last step is to execute the program

15
Processing a C++ Program (cont'd.)

16
Interacting With User: Displaying Messages on Screen
• In C++ , we use Cout << “ Text To be Displayed on The screen “ ;
• To use Cout << , we must use ‫أول حاجه هنتعلمها جملة الطباعه على الشاشه جملة‬
cout ‫الـ‬
– #include <iostream> ; ‫لزم ييجي بعدها علمتين اقل من <<واسمهم‬
– using namespace std; ; ‫ ولزم انهي الجملة بـ‬insertion operator ‫الـ‬
• #include <iostream> notifies the preprocessor to include the contents of the input/output stream header file
<iostream> in the program
‫ لزم استدعي المكتبة‬cout‫وعشان استخدم امر ال‬
• We can use the Escape Sequence to format the Displayed Text.
. ‫التي تحتوي على تعريف المر دا‬
‫اول سطرين دول هما اللي بتستدعي فيهم المكتبة‬
cout ‫الخاصة بأمر الـ‬

‫ امر أنزل بيه‬: ‫دي أوامر مهمه بحسن بيها الكود‬


،،‫ أمر اسيب بيه مسافه بين كلمتين او جملتين‬،‫سطر‬
‫وهكذا‬
‫عايزاك تجرب الوامر دي كلها في الكود بتاعك‬
‫وتشوف كل أمر بيعمل ايه‬

comment
• We can put commenton our programs using
• // to comment a single line
// this progrm is written by Fatima Alabbasy
• /*
Mulitple Lines ‫ كومنت يعني كلم‬،‫عشان أعمل كومنت في الكود‬
• */ to commentmultiple lines ‫اكتبه لنفسي او لحد هياخد مني الكود يكمل عليه‬
/* This program is written by Fatima Alabbasy .‫والكلم دا الكومبايلر مش هيشوفه ول هيقرأه‬
‫ قبل السطر‬// :‫في طريقتين‬
On Monday 11/11/2024
‫أو لو الكومنت طويل شوية أكتر من سطر في‬
*/ /* ‫* وفي نهايته اكتب‬/ ‫بدايت الكومنت اكتبـ‬

Example 2

• Write a program to find the perimeter and area of a square


The perimeter and area of the square are given by the followingformulas: perimeter = Side Length * 4
area = Side Length * Side Length
Input: Square Side Length
Processing: perimeter = Side Length * 4
area = Side Length * Side Length
Output: Print Out The Perimeter and Area
:‫ماال لكتابة برنامج يحسب محيط ومساحة المربع‬
.‫زي مااتفقنا اول خطوة أحدد المدخلت والبروسسيسينج والمخرجات‬
‫طول الضلع فقط‬:(‫المدخلت )المعطيات‬
4*‫ المحيط=طول الضلع‬:(‫البروسيسينج)القاوانين()العمليات‬
‫المساحة= طول الضلع * طول الضلع‬
‫ يطبع لي المحيط والمساحة‬:(‫المخرجات)الناتج‬
Interacting With User: Accept Input From User

• In C++ , we use Cin >> Variable; To accept an input from the user.
cin ‫المر الااني للتفاعل مع اليوزر هو امر‬
• To use Cin >>, we must use #include <iostream> ;
• #include<iostream> notifies the preprocessor to include in the program the contents of the input/output stream header
file <iostream>.
• A variable is a location in the computer’s memory where a value can be stored for
use by a program.
• All variables must be declared with a name and a data type before they can be used in a program.
• Declaration
DataType Identifier;
Int width ;

:‫حاجه مهمه بقى وهي المتغيرات وتعريف المتغيرات‬


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

24
C++ Data Types ‫أنواع المتغيرات‬
Character or small signed: -128 to 127
char 1byte
integer. unsigned: 0 to 255
signed: -2147483648 to 2147483647
int Integer. 4bytes
unsigned: 0 to 4294967295
signed: -32768 to 32767
short int (short) Short Integer. 2bytes
unsigned: 0 to 65535
signed: -2147483648 to 2147483647
long int (long) Long integer. 4bytes
unsigned: 0 to 4294967295
Boolean value. It can
bool take one of two values: true or 1byte true or false
false.
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
Double precision floating
double 8bytes +/- 1.7e +/- 308 (~15 digits)
point number.
Long double precision
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
floating point number.
25
Working With Variable

int length ;
Length 5
int width;
int area; Width 10

cin>>Length; Cin>>width;
Area = Length * width ;
Area 50

:‫ملحظة مهمه‬
lenght ‫ او مال‬x ‫ واسمه مال‬int ‫ لما اجي اعرف متغير نوعه رقم صحيح‬.‫ وماينفعش ول حرف كابتل‬small ‫اوامر الكود كلها لزم تكون حروف‬
.‫ الكومبايلر بيعطي أمر للميموري تحجز مكان للمتغير باسمه‬:‫ايه اللي بيحصل‬

Declaring & Initializing Variables


‫عرفنا ازاي نعرف المتغير‬
.‫هنا هنتعلم ازاي نعطيه قيمة مبدئية‬
• Initialization means to give a variable an initial value. .‫ممكن اعطي المتغير قيمه في نفس السطر اللي بعرفه فيه‬
.‫وممكن سطر تاني‬
• Variables can be initialized when declared: int
.‫وممكن استقبل القيمة دي من المستخدم‬
first=13, second=10; char ch=' ';
double x=12.6;
• All variables must be initialized before they are used in an arithmetic operatation
– But not necessarily during declaration

26
Rules on Variable Names

• DO NOT use reservedwords as variable names (e.g. if, else, int, float, case, for, …).
• The first character has to be a letter or underscore. It can not be a numeric digit.
• The secondand the other characters of the name canbe any letter, any number, or an underscore “_”.
Examples
Some valid names:
my_name, m113_1, salary, bluemoon , _at
Some invalid names:
my name, my-name , 1stmonth , salary! , guns&roses ,
:‫قواعد تسمية المتغيرات‬
.‫مال‬cout ‫ بمعنى ماينفعش اسميه باسم‬:‫ممنوع استخدم الكلمات المحجوزة في اللغه وانا بسمي المتغير بتاعي‬
_ ‫ باقي الحروف عادي ممكن يجي فيها ارقام عادي او‬.‫اول حرف في اسم المتغير لزم يكون حرف وليس رقم‬
‫ وايضا ممنوع في اسم المتغير يكون في‬.‫ كل العلمات دي ممنوعه‬$ ,‫ = أو @ أ‬,‫ أ‬% ‫باقي العلمات ممنوع استخامها في تسمية المتغير مال * او‬
‫ موجود أمالة على السماء الصحيحه واسماء المتغيرات الخاطئة‬.‫مسافات‬
‫ وهكذا‬length ‫ لو هيحسي طول سميه‬area ‫يعني لو المتغير هيكون فيه المساحه سميه‬..‫نصيحة وانت بتسمي المتغير سمية اسم معبر‬

Frequently used data types .‫دي ملحظات مهمة لزم نعرفها عن انواع الداتا‬
‫والفروق بينهم‬
The data type unsigned is used to represent positive integers.
float and double data types for storing realnumbers.
The float data type has a precisionof seven digits-This means after the decimal point you can have seven
digits
-Example: 3.14159 534.322344 0.1234567
-The double data type has a precision of fifteen digits
Example : -3738.7878787878 .141592653589790

30
Frequently used data types
• We can use Scientific Notation to represent real numbersthat are very large or very small in value.
• The letters e or E is used to represent times 10 to the power.
Example:
• 1.23 x 10 5 is represented as 1.23e5 or 1.23e+5 or 1.23E5
• 1 x 10 -9 is represented as 1e-9

.‫البروجرامر الشاطر بيعرف يستخدم النوع الصحيح لستغلل المساحة في الميموري افضل استخدام‬

1.23E5 ‫ أو‬1.23e+5 ‫ أو‬1.23e5 ‫ هي‬c++ ‫ بلغة ال‬1.23 x 10 5 ‫طريقة كتابة الرقم دا‬

 Arithmetic Operations
Parentheses are used in C++ expressions in the same manner as in algebraic expressions.
 For example, to multiply a times the quantityb+c we write: a * ( b + c ).
 There is no arithmetic operator for exponentiation in C++, So x2 is
represented as x * x.

C++ ‫العمليات الحسابية في لغة الـ‬


‫ هي العمليا ت العادية الجمع والطرح والضرب‬
% ‫والقسمة وباقي القسمة اللي هو‬
‫ حاجة اسمها تربيع‬C++ ‫ مفيش في الـ‬

31
Precedence of arithmetic operations

Example :

For example, 2 + 3 * 5 and (2 + 3) * 5 both have different meaning


32
• ? = 1 + 2 * (3 + 4)

– Evaluated as 1 +(2 * (3+4))and the result is 15


• ?=5*2+ 9% 4

– Evaluated as (5*2) + (9 % 4) and the result is 11

• ? = 5 * 2 % ( 7 – 4)

– Evaluated as (5 * 2)%(7 – 4)and the result is 1

 Data Type of an Arithmetic Expression


Data type of an expression depends on the type of its operands , Data type conversion is done by the compiler
• If operators are *, /, +, or – , then the type of the result will be:
– integer, if all operands are integer. operands ‫نفسه بيعتمد على نوع الـ‬expression ‫نوع ال‬
» Int A , B; .‫اللي في التعبير الحسابي نفسه‬
» A+ B Integer. ‫ هي‬operator ‫ والـ‬B‫و‬A ‫ هنا هم الـ‬operands ‫ الـ‬A+ B
+ ‫الـ‬
– float, If at least one operand is float and there is no double ‫ يبقى‬Int ‫ نوعهم‬operands ‫يبقو لو كان عندي كل الـ‬
‫ وهكذا‬Int ‫الكسبريشن هيبقى نوعه‬
» Int A ; Float B;
» A+B Float.
– double, if at least one operand is double
• Int A ; Float B; Double C;
» A+B+C double.
Example Express‫ مختلفة في الـ‬operands ‫لو عندي انواع الـ‬
‫يبقى النوع الكبر هو اللي يغلب‬ion

int * int; result int

int + float; result float

Int + double / float; result double

int – double; result double

• The data type of the target variable is also important


• If the result is a real number and the target variable is declared as integer, only the integer part of the
result will be kept, and decimal part will be lost.

Example

‫ بمعنى‬،‫الجزء دا مهم جدا عشان بيكون سبب ان نتيجة العمليات الحسابية يكون خطأ‬
:‫مال‬
16.666 ‫ المفروض الجابة‬6 ‫ لما اقسمها على‬100‫الـ‬
‫ الول‬، ‫لكن انا هنا في الكود عرفت ثلث متغيرات‬
‫ تمام؟‬cnt‫ و‬:sum ‫ والااني والاالث ارقام غير صحيحة‬،‫نوعه رقك صحيح‬int avg
‫ لكن انا معرفه‬float ، ‫ المفروض الجابة تطلع‬float ‫ على‬float ‫طيب لو قسمت‬
!! 16.6666 ‫ فقط وليس‬16 ‫ على انه رقم صحيح يبقى الجابه هتطلع‬avg ‫ال‬
38
Data Type of an Arithmetic Expression

The result of the division will be 16


float avg; avg will be 16.0
int sum=100, cnt = 6;
avg = sum / cnt;

• Only the integer part of the result will be considered if two operands are integer
Even when the target variable is float

float‫ على انه رقم كسر‬avg ‫طيب هنا انا عرفت الـ‬
‫ ايه اللي هيحصل؟‬int ‫والمتغيرات التانيه اعرفها على انها ارقام صحيحة‬
16.666 ‫ المفروض الجابة‬6 ‫ لما اقسمها على‬100‫المفروض الـ‬
int ‫ يبقى الجابه هتطلع لي‬int ‫ على‬int ‫لكن انا هنا انا قسمت رقم‬
!! 16.6666 ‫ فقط وليس‬16 ‫هتطلع‬
.float ‫في المعادله نوعه‬operends ‫عشان احل المشكله هنا اخلى احد ال‬

39
‫‪Type Casting‬‬

‫معناه اني احول من داتا تايب الى داتا تايب آخر‪.‬‬


‫)(‪int main‬‬ ‫في الاال‪ 3/5 :‬المفروض يطلع رقم كسر لكن اللي هيتطبع فعلي ‪ 1‬فقط لن كل‬
‫حدود العملية الحسابية‪ int‬فالناتج هيطلع‪int‬‬
‫{‬
‫عشان احل المشكلة والناتج يطلع صح ‪ 1.66‬المفروض اخلى أحد حدود العملية‬
‫;‪int i=5, j=3‬‬ ‫الحسابية ‪Float‬‬
‫;‪float div‬‬ ‫ودا بيتم عن طريق كتابة كلمة )‪ (float‬بين قوسين جمب المتغير اللي اريد‬
‫;‪div= i/j‬‬
‫;‪cout<< div‬‬
‫تغير نوعه كما هو موضع في الماال‪:‬‬
‫}‬
‫‪Type cast: tels the compiler to treat i as float.‬‬
‫)(‪int main‬‬

‫{‬
‫;‪int i=5, j=3‬‬
‫;‪float div‬‬
‫;‪div=(float) i/j‬‬
‫;‪cout<< div‬‬
‫}‬
‫‪After type casting , The div will be 1.66667‬‬

‫‪40‬‬
Increment and Decrement Operators

• Increment operator: increment variable by 1


– Pre-increment: ++variable

– Post-increment: variable++
• Decrement operator: decrement variable by 1 :‫الجزء دا مهم جدا جدا‬
.‫ تستخدم لزيادة قيمة المتغير بواحد‬++ ‫الـ‬
. ‫ تستخدم لنقصان قيمة المتغير بواحد‬-- ‫الـ‬
– Pre-decrement: --variable ‫ بواحد فهتكون قيمته‬I ‫ هتزود الـ‬I++ ‫ اذن‬I=5 ‫مال لو‬
‫ وهكذا‬6 :‫الجديده‬
– Post-decrement: variable -- ‫ لها شكلين‬Increment and Decrement ‫عمليات الـ‬
:‫اما تكون قبل اسم المتغير او بعده كما هو موضح‬
• Examples :

++K , K++ k= K+1

--K , K-- K= K-1

41
Increment and Decrement Operators

• If the value produced by ++ or – – is not used in an expression, it does not matter whether it is a
pre or a post increment (or decrement).
• W hen ++ (or – –) is used before the variable name, the computer first increments (or decrements)
the value of the variable and then uses its new value to evaluate the expression.
• When ++ (or – –) is used after the variable name, the computer uses the current value of the variable
to evaluate the expression, and then it increments (or decrements) the value of the variable.
• There is a difference between the following

x = 5;
Cout << ++x;

You might also like