c++ course - lesson 1

15
THE NAME OF ALLAH C++ Course First Lesson First we will talk about some concepts of programming...... First we must think before beginning in our coding and we should build our idea in our mind and write it on paper as ships to express our idea to make it easy for anyone to work in our project and this method called "Flow Chart". and we will talk about it again because we will use it. Second we should know that the programmer not making a program from NOTHING but it use some steps and rules to make it's idea or to make a program to do anything.....so I decide to make our course as stairs for any code.....we will talk about every step in the general case of any code and explain everything about it. : Libraries According to last words that every code consist of some steps and we know that the computer is a stupid machine so it must have everything to work to avoiding anything need to thinks so the libraries likes boxes contains this steps of the programming .and every library contain some steps and we write this line to use any library: #include <LibraryName.h> For Example: the most important library #include <iostream.h > And we will begin our program now with this step.

Upload: mohamed-ahmed

Post on 22-May-2015

1.525 views

Category:

Documents


3 download

DESCRIPTION

Follow this link : http://bit.ly/9jJ914

TRANSCRIPT

Page 1: C++ Course - Lesson 1

THE NAME OF ALLAH

C++ Course

First Lesson

First we will talk about some concepts of programming......

First we must think before beginning in our coding and we should build our idea

in our mind and write it on paper as ships to express our idea to make it easy for

anyone to work in our project and this method called "Flow Chart". and we will

talk about it again because we will use it.

Second we should know that the programmer not making a program from

NOTHING but it use some steps and rules to make it's idea or to make a

program to do anything.....so I decide to make our course as stairs for any

code.....we will talk about every step in the general case of any code and explain

everything about it.

: Libraries

According to last words that every code consist of some steps and we

know that the computer is a stupid machine so it must have everything to

work to avoiding anything need to thinks so the libraries likes boxes

contains this steps of the programming .and every library contain some

steps and we write this line to use any library:

#include <LibraryName.h>

For Example:

the most important library

#include <iostream.h >

And we will begin our program now with this step.

Page 2: C++ Course - Lesson 1

Variables:

we use variables look like boxes to save any number to use it now or after

some time in our operations that the program like the human mind it need

to save any number or anything to do everything on it later so in

programming we use variables and memory boxes and it is different in its

size and in its uses and we will talk about it after 5 min :D :D bust you

must know that every variable has a name and we can't declare 2

variable with the same name....it will make an ERORR in

Compiler........what is Compiler?? I will explain it later.

Data Types :

As we were talking about variables we will talk about its types

Examples Range Description س Name

1 , 2 , 3 It contains just integers int

1 , 2 It take small range Short

1 , 2 ,3 It take a great range for numbers long

1.20 , 2.453 It take float points float

1.203 OR 1 It could take float or int double

'a' , '1' , '&' It take anything as character char

"Khaled" It used for text string

It take: True OR False bool

And in first w make declaration for any variable by its data type as:

Int x ;

Char y ;

And w make an "initialization" it means to give the variable an initial value

so it will be:

Int x = 0 ;……..or any value as we need and look ' ; ' ..this character is

very important to know the compiler that this step is finished.

Page 3: C++ Course - Lesson 1

Inputs and Outputs:

Any program need to show a lot of things to user and may be take inputs

from user or not. so we will find methods to show anything.

To show anything we will write it:

Cout<<"The Name Of Allah" ;

And we could use it as:

Cout<<"The" << "Name" << "Of"<< "Allah" ;

And this method could show the result of an operation that

Int x = 5 ;

int y = 6 ;

cout << x + y ; it will show 11.

But if we need to take the value of x and y from user so we will need to

use input method . it will be:

Int x = 0 ;

Int y = 0 ;

Cin >> x ;

Cin >> y ;

Cout << x + y ;

And we could use it to notify the user what will user input to the program

As before every cin we will use cout to give an inform to user as :

Cout << " Enter the First Number : " ;

Cin >> x ;

Now we will try First Code……But Now we will take this form as const

and I will explain it later:

Page 4: C++ Course - Lesson 1

#include <iostream>

using namespace std ;

int main ()

{

//Write Your Code Here

system("pause");

return 0 ;

}

Download this program Dev c++

http://bloodshed-dev-c.en.softonic.com/

and set up it and choose FileNewSource File

and write your Code and Press F9

this called Compiler it look like translator but it translate the C++ Code

to EXE file you could use it later.

Page 5: C++ Course - Lesson 1
Page 6: C++ Course - Lesson 1

As you see it is very nice to try but you need to think how to build a good

idea and good interface.

Operators:

There are operators to make calculations as we know + , - , * , / , %.

There are anther operator make update to variable as plus 1 to the

++ To plus one.

--To minus one.

As X++; OR X-- ;

There is different from X++ and ++X as in first we will add one to X before

do any operation and in second it will add one to X after make the

operation.

And there are anther operations as:

+= , -= , /= / %= ,…….

X+= 5 ; as the same X = X+5 ; and it will be the same in all.

And there are logical operators to make checks such as if (5 == 3)

== it used to test it 2 variables equal or Not

<= To check if the first number less than or Equal the Second.

>= To check if the first number greater than or Equal the Second.

And to use this operators we should know

if

else

that: I said that if this condition it true do something if not do anther

something and may be not to do anything. and it will be:

Page 7: C++ Course - Lesson 1

if (Check condition)

{

//What it will do if true

}

else

{

//what it will do if false

}

NOTE the prickets {} very important as if you forget it

By default the program will take the first step after it and will never see

what after it.

Example:

if(x == y)

{

cout << "X is Equal Y";

}

else

{

cout<< "X Not Equal Y";

}

But we can use it for more comparisons as:

If(x == y)

{

cout<<"X Equal Y ";

}

else if (x > y)

{

Page 8: C++ Course - Lesson 1

cout << "X is Greater Than Y";

}

else

{cout << "X is Less Than Y"; }

Note that you can use it cout << endl; to make a new line in the screen to

know the meaning of it try this code:

#include <iostream>

using namespace std ;

int main ()

{

cout << "The Name OF Allah" ;

cout << "This Lesson ONE";

system("PAUSE");

return 0 ;

}

And try this:

#include <iostream>

using namespace std ;

int main ()

{

cout << "The Name OF Allah" << endl ;

cout << "This Lesson ONE" << endl;

system("PAUSE");

return 0 ;

}

Anther way to make new line is : cout <<"\n";

You can use \n "fe west el kalam ":D :D.

Page 9: C++ Course - Lesson 1

Switch:

It look like if with more one comparison. You can use it when you have

more choice as if the user chosen it the prog should make something and

if user chosen anther so the prog will make anther something

And the general case will be:

swithch (what will the prog depend on in choices )

{

case choice :

//what will do

break ;

case anther choice :

//what will do

break ;

default:

//what will do

break ;

}

And for example if you need to write code take a number from user and

check if the number

r show "Number 1 Mean RED "

g show "Number 1 Mean GREEN "

b show "Number 1 Mean BLUE "

if not 'r' nor 'g' nor 'b'

show "Wrog"

Page 10: C++ Course - Lesson 1

now we will think to take an input from user as character

so you should look to this code:

#include <iostream>

using namespace std ;

int main ()

{char ch ;

cout << "Enter The Char : " << endl ;

cin >> ch ;

switch(ch)

{

case 'r':

{

cout << "Number 1 Mean RED \n" ;

break ;

}

case 'g':

{

cout << "Number 1 Mean GREEN \n";

break ;

}

case 'b' :

{

cout << "Number 1 Mean BLUE \n";

break ;

}

default:

cout <<"Wrong";

break ;

}

system("PAUSE");

Page 11: C++ Course - Lesson 1

return 0 ; }

Looping:

If you want the prog to do anything more than one….what will you do??

Will you re-write the code more than one??

We will use the looping to do it.

Looping has 3 ways for loop or while loop or do while loop.

For loop:

I think that I can anyone from the 3 to do the same loop but let us see

how it work:

The general case:

for (initial ; check ; update )

{

//write you code here

}

For loop need know when it will stop or it will infinite loop as in this case

for (int I = 0 ; I <= 5 ; )

{

cout <<"For LOOP\n";

}

Look I decelerate variable I in the for. It will let the for to control it self and

you can use it in the scope of for "y3ny ben el prickets bta3t el for bs"

I wrote it as the for will find the i= 0 and look to check case that if I less

than or equal 5 so it will return true so it will compile what will be written

between the prickets but look I will equal to 0 for ever so we need to

increment I to equal 5 to stop the for loop so it will be:

Page 12: C++ Course - Lesson 1

for (int I = 0 ; I <= 5 ; I++)

{

cout <<"For LOOP\n";

}

If you write it:

for( ; ; )

{

cout <<"For LOOP\n";

}

*you can use two for loop as

for(int I = 0 ; I < 5 ; I++)

{

For(int j = 1 ; j <5 ; j++)

{

cout<<"Two Loop";

}

}

It will never stop.

While Loop:

It is different in its shape from for as if check and if true it will do if now the

prog will skip what among its prickets.

The General case of it:

while(check case)

{

//write your code

}

Example:

Page 13: C++ Course - Lesson 1

#include <iostream>

using namespace std ;

int main ()

{

int x ;

while(x <= 5)

{

cout << "While loop. \n";

}

x++;

system("PAUSE");

return 0 ;

}

Do While Loop:

It different from while loop as it will do it code first without any check

And will check if true so it will do it again till return false to stop looping

and the update statement will be in the code within prickets

Example for it:

#include <iostream>

using namespace std ;

int main ()

{

int x ;

do

{

cout << "While loop. \n";

x++;

}

Page 14: C++ Course - Lesson 1

while(x <= 5);

system("PAUSE");

return 0 ;

}

THE END OF LESSON ONE

AASIGMENT ONE

Write code to make a calculator which take 2 numbers from user and the

operation that will make it of the 2 numbers if '+' make plus operation on

the 2 and show on the screen "The Result ="and show your result . do

it for all operation + , - , *, /

And after showing the result show it in the screen

Please choose:

1:New Operation

2:Exit

And take the user choice and do it.

Hint

You will use

if else

switch

may be any kind of looping

send the cpp file to this mail

[email protected]

Attention:

Page 15: C++ Course - Lesson 1

If you not write the codes you will not be with us in the

next lesson .

GOOD LUCK

ENG Khaled