2. Ahmed nobi
- Android developer with one year experience
- Android Developer at Cleodev software House.
- programming instructor with three year experience in Luxor and Aswan.
- working on online course (programming school)
- former chair IEEE Aswan SB
- Ex Vice Student representative IEEE Egypt Section
contact me:
LinkedIn: www.linkedin.com/in/ahmed-nobi
Facebook: www.facebook.com/Ahmed.Nobi.Eltwins
mail: ahmed.nobi95@gmail.com
Brief about me
3. Courseâs outline
⢠Variables
⢠Data type
⢠Operators
⢠Selection
⢠Loop
⢠Arrays
⢠Functions
Sessionâs outline today
⢠Variables
⢠Data type
⢠Operators
⢠Selection
4. Structure of a C++ Program
Hello World++
// Hello World program
#include <iostream>
Using namespace std;
int main() {
cout << "Hello Worldn";
return 0;
}
comment
Allows access to an I/O
library
output (print) a
string
Program returns a status
code (0 means OK)
Starts definition of special function
main()
Some common includes
ďľ Basic I/O: iostream.h
ďľ I/O manipulation: iomanip.h
ďľ Standard Library: stdlib.h
ďľ Time and Date support: time.h
5. Variables/ constants
Variables are just names for locations in memory.
⢠Variables are just names for locations in memory.
⢠In C++ all variables must have a type (not all languages require this).
⢠In C++ all variables must be declared before they can be used.
Data type var_name;
⢠Sometimes, it is just convenient to give a name to a constant value
Const Data type var_name= value;
6. Variable Names
How to name you variables ?
C++ variable naming Rules:
ďľ made up of letters, digits and underscore.
ďľ Must start with a non-digit.
ďľ Case sensitive
a is not the same name as A
ďľ Can be any length
ďľ Good variable names tell the reader what the
variable is used for!
8. Data type (cont.)
Common data type which will we use
⢠int for integer numbers ex 1 ,2 ,4, 77
⢠double for real numbers ex 1.77 , 55.8845
⢠char for characters ex A ,b , e
⢠bool for Boolean values ex true , false
10. Mathematical Operators
+ - * / %
ďľ Operators have rules of precedence and associativity that control how
expressions are evaluated.
ďľ What is the value of this C++ expression ?:
2 / 3 / 4 + 5
ďľ Answer: You can't tell unless you know the rules.
11. C++ Math Operator Rules
Operator Associativity Precedence
() left to right high
* / % left to right middle
+ - left to right low
ďľ Now - what is the value of this?:
2 / 3 / 4 + 5
ďľ How about this: (7*3/4-2)*5
12. Relational and Equality Operators
Relational and Equality operators are used to
compare values:
ďľ Relational Operators:
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
ďľ Equality Operators:
== Equal to
!= Not Equal to
14. I/O ⢠Cin >> to read data
⢠Cout << to write data or display on
secreen
⢠Cerr << for error msg
15. examples ⢠Write a program to sum two numbers
⢠Write a program to calculate the area of circle
16. Output format
That helps you to format your output.
Escape Sequence Description
⢠n Newline. Position the screen cursor to the beginning of the next line.
⢠t Horizontal tab. Move the screen cursor to the next tab stop
⢠a Alert. Sound the system bell
⢠Backslash.Used to print a backs lash character
⢠â Double quote. Used to print a double quote character
17. Blocks and its
importance
⢠We can make a block in code by { } to put limits for
executed lines and when we need it to execute
⢠For example : the main block of main function
⢠Block of output response for if statement
18. Selection
⢠The condition expression of the if statement must be within parentheses. If it is true, the statement
immediately following the if statement is executed
ex: if (cond.) what you need to do
⢠If multiple statements must be executed, they must be enclosed in curly braces following the if
statement (this is called a statement block):
ex: if (cond.)
{ // begins statement block
what you need to do
} // ends statement block
19. ⢠The if statement also supports an else clause.An else clause represents one or a block of
statements to be executed if the tested condition is false. For example,
Ex : if (cond.)
{
// user guessed correctly
}
else
{
// user guessed incorrectly
}
20. ⢠A second use of the else clause is to string together two or more if statements. For example, if the user guesses
incorrectly, we want our response to differ based on the number of guesses. We could write the three tests as
independent if statements:
If (num_tries == 1) cout << "Oops! Nice guess but not quite it.n";
if (num_tries == 2) cout << "Hmm. Sorry. Wrong a second time.n";
if (num_tries == 3) cout << "Ah, this is harder than it looks, isn't
it?n";
⢠However, only one of the three conditions can be true at any one time. If one of the if statements is true, the others
must be false. We can reflect the relationship among the if statements by stringing them together with a series of
else-if clauses:
if (num_tries == 1)
cout << "Oops! Nice guess but not quite it.n";
else if (num_tries == 2)
cout << "Hmm. Sorry. Wrong again.n";
else if (num_tries == 3)
cout << "Ah, this is harder than it looks, isn't it?n";
else
cout << "It must be getting pretty frustrating by now!n";
21. ⢠One confusing aspect of nested if-else clauses is the difficulty of organizing their logic correctly. For example,
EX if (usr_guess == next_elem)
{
// user guessed correctly
}
else
{
if (num_tries == 1)
// ... output response
else if (num_tries == 2)
// ... output response
else if (num_tries == 3)
// ... output response
else
// ... output response
}
Briefly:
If (conditional statement )
{
If (conditional statement )
{
output response
}
}
22. Now let us try to use { }
Modify the following code to produce the output shown. You must not
make any changes other than inserting braces. The compiler ignores
indentation in a C++ program. We eliminated the indentation from the
following code to make the problem more challenging.
a) Assuming x = 5 and y = 8, the following output is produced. @@@@@ $$$$$
&&&&&
b) Assuming x = 5 and y = 8, the following output is produced. @@@@@
c) Assuming x = 5 and y = 8, the following output is produced. @@@@@
&&&&&
d) Assuming x = 5 and y = 7, the following output is produced. [Note: The last
three output statements after the else are all part of a block.] #####
$$$$$ &&&&&
if ( y == 8 )
if ( x == 5 )
cout << "@@@@@" << endl;
else
cout << "#####" << endl;
cout << "$$$$$" << endl;
cout << "&&&&&" << endl;
23. a) Assuming x = 5 and y = 8, the following output is produced. @@@@@ $$$$$ &&&&&
b) Assuming x = 5 and y = 8, the following output is produced. @@@@@
c) Assuming x = 5 and y = 8, the following output is produced. @@@@@ &&&&&
d) Assuming x = 5 and y = 7, the following output is produced. [Note: The last three output
statements after the else are all part of a block.] ##### $$$$$ &&&&&
if ( y == 8 )
if ( x == 5 )
cout << "@@@@@" << endl;
else
cout << "#####" << endl;
cout << "$$$$$" << endl;
cout << "&&&&&" << endl;
24. Summary
⢠Simple if
If ( cond. ) executable statement
⢠If with block
If ( cond. ) { executable statements }
⢠If else with block
If ( cond. ) { executable statements }
Else
{
executable statements }
⢠If with else if
If (cond) {executable statements }
Else ifv
{executable statements }
Else {executable statements }