Lec 02 Hello
Lec 02 Hello
PROGRAMMING
Dr. Abdelhady Mostafa
Lecture 2
Course contents
■ Lecture 1: Introduction
■ Lecture 2: C++ Hello world!
■ Lecture 3: Flow of control (Loops)
■ Lecture 4: Flow of control (Decisions)
■ Lecture 5: Structures
■ Lecture 6: Functions (Exam 1)
■ Lecture 7: Functions (cont.)
■ Lecture 8: Objects and classes
■ Lecture 9: Objects and classes (cont.)
■ Lecture 10: Arrays and strings
■ Lecture 11: Arrays and strings (cont.) (Exam 2)
2
Today’s menu
■ Basic program construction
■ Input/output statements
■ Variable types
■ Arithmetic operations
3
C++ First program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
4
Functions
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
5
Functions
6
Functions
#include <iostream>
int main()
Always start with main() {
return 0;
7
Program Statements
int main()
8
Program Statements
int main()
9
Directives
int main()
return 0;
10
Directives
Preprocessor directive
#include <iostream>
- written before main()
using namespace std;
- start with #
int main()
- doesn’t end with a semicolon
{
- tells the compiler to do something
cout << “Hello World\
- #include = insert another file into my
n”;
code
return 0;
- iostream is header file concerned with
}
basic input/output operations
11
Directives
The using Directive
#include <iostream>
- A namespace is a part of the program in
using namespace std;
which certain names are recognized
int main()
{
- cout is declared in std
cout << “Hello World\
n”;
-otherwise, each time we use cout we
return 0;
write:
}
std::cout << “Hello World”;
12
Whitespace
Compiler completely ignores whitespace #include <iostream>
such as: spaces, carriage returns, tab using namespace std;
int main()
Exceptions:
{
- #include MUST be written on one line
cout << "Hello World\
- string constants cannot be broken n";
return 0;
Use this property in the smart way!
}
13
Example
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ cout << {
return 0;
16
String Constants
return 0;
17
Comments
Comment Syntax:
// for one line comment
/* for multiple lines comment */
18
Example
#include <iostream> //preprocessor
directive
using namespace std; //“using” directive
int main() //function name
“main”
{ //start function body
/* I can also
Use comments here */
cout << “Hello World\n”; //statement
return 0; //statement 19
Variable Declaration
#include <iostream>
Basic variable type: bool, char,
using namespace std;
short, int, long, float, int main()
{
double int var1;
float var2;
char var3;
You must declare a variable before using it ……………
……………
return 0;
It is better to declare variables at the }
beginning of the program
20
Variable Names
#include <iostream>
You can use upper- and lowercase letters,
using namespace std;
digits from 1 to 9 and underscore (_)
int main()
{
first character must be a letter or
int var1;
underscore float var2;
char var3;
Case sensitive: ……………
var1 is not the same as VAR1 ……………
return 0;
}
Use meaningful names!
21
Type Conversion
#include <iostream>
using namespace std;
int main()
{
int count = 7;
float avgWeight = 155.5;
double totalWeight = count * avgWeight;
cout << “totalWeight=” << totalWeight <<
endl;
return 0;
} 22
Automatic Conversion
23
Question
int integerOne = 1;
float doubleOne = 2.5;
24
Input with cin
// fahren.cpp
// demonstrates cin, newline
#include <iostream>
using namespace std;
int main()
{
int ftemp; //for temperature in fahrenheit
cout << “Enter temperature in fahrenheit: ”;
cin >> ftemp;
int ctemp = (ftemp-32) * 5 / 9;
cout << “Equivalent in Celsius is: ” << ctemp << ‘\n’;
return 0;
}
25
The setw Manipulator
26
The setw Manipulator
// width2.cpp
// demonstrates setw manipulator
#include <iostream>
#include <iomanip> // for setw
using namespace std;
What happens if setw() less
int main()
than number of characters?
{
long pop1=2425785, pop2=47, pop3=9761;
cout << setw(8) << “LOCATION” << setw(12) << “POPULATION” <<
endl
<< setw(8) << “Portcity” << setw(12) << pop1 << endl
<< setw(8) << “Hightown” << setw(12) << pop2 << endl
<< setw(8) << “Lowville” << setw(12) << pop3 << endl;
return 0;
} 27
Arithmetic Assignment Operators
#include <iostream>
using namespace std;
int main()
{
int ans = 27;
ans += 10; //same as: ans = ans + 10;
cout << ans << “, ”;
ans /= 3; //same as: ans = ans / 3;
cout << ans << “, ”;
ans %= 3; //same as: ans = ans % 3; (remainder
operation)
cout << ans << endl;
return 0;
}
28
Increment Operators
#include <iostream>
using namespace std;
int main()
{
int count = 10;
cout << “count=” << count << endl; //displays 10
cout << “count=” << ++count << endl; //displays 11
(prefix)
cout << “count=” << count << endl; //displays 11
cout << “count=” << count++ << endl; //displays 11 (postfix)
cout << “count=” << count << endl; //displays 12
return 0;
}
29
Postfix vs prefix
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = ++a;
cout << “b = ” << b << “ a = ” << a << endl;
int c = a++;
cout << “c = ” << c << “ a = ” << a << endl;
return 0;
}
b = 6, a = 6
c = 6, a = 7
30
Types of program errors
Program errors are also referred to as program bugs.
o Logic errors
31
Syntax errors
33
Runtime errors
35
Logic errors
cout statements
Use debugger
37
Wrap up
■ Basic program construction
■ Input/output statements
■ Variable types
■ Arithmetic operations
38
Next Lecture …
Flow of control
(Loops)
39
Questions?
40