0% found this document useful (0 votes)
12 views40 pages

Lec 02 Hello

Uploaded by

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

Lec 02 Hello

Uploaded by

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

COMPUTER AND

PROGRAMMING
Dr. Abdelhady Mostafa

Assiut University – Dept. of Mechatronics Eng.


MET 111 - First semester 2024/2025

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

■ Types of program errors

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

 Functions are fundamental block of C++ #include <iostream>


 main is the function name using namespace std;
 Parentheses () are distinguishing feature of
int main()
a function {
 Parentheses () not always empty
cout << “Hello World\
 Parentheses () hold function arguments n”;
 int means that main return integer value return 0;
 Braces { } define begin and end of function }

6
Functions

#include <iostream>

using namespace std;

int main()
Always start with main() {

cout << “Hello World\


n”;

return 0;

7
Program Statements

 Statement is fundamental unit of C++ #include <iostream>

programming using namespace std;

int main()

 Statements tell the computer to do {

something cout << “Hello World\


n”;

 A semicolon signals the end of a statement return 0;

8
Program Statements

 cout << “Hello World\n”; #include <iostream>

Display Hello World on the screen using namespace std;

int main()

 return 0; cout << “Hello World\

tells main() to return the value 0 to n”;

whoever called it return 0;

9
Directives

 #include <iostream> #include <iostream>

preprocessor directive using namespace std;

int main()

using namespace std; {

using directive cout << “Hello World\


n”;

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 << {

“Hello World\n” cout << “Hello World\


n”;
;return
return 0;
0;}
}

Both are the same for compiler!! 14


Is there something wrong here?
#include <iostream> #include
using namespace <iostream>
std; using namespace std;
int main() int main()
{ {
cout << cout << “Hello
“Hello World\n”; World\n”;
return 0; return 0;
} }
15
Output Using cout
 Cout pronounced “C out” #include <iostream>

using namespace std;


 << called insertion operator
int main()

cout << “Hello World\


n”;

return 0;

16
String Constants

 “Hello World\n” is a string constant #include <iostream>

using namespace std;

 Same sentence will be displayed each time int main()

 \n is an escape sequence, means new line cout << “Hello World\


n”;

return 0;

17
Comments

 Comments are very useful for programmer and user


 Compiler ignores comments
 Most programmers don’t use enough of them 

 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;

int sumOne = integerOne + doubleOne; //A


double sumTwo = integerOne + doubleOne; //B

Which statement gives correct answer, A or B ?

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.

 C++ program may have one or more of four types of errors:


o Syntax errors
o Linker errors Usually, the errors become
more difficult to find and fix as
o Runtime errors you move down this list

o Logic errors

31
Syntax errors

 Occurs when the rules of C++ language are not followed.

 Common examples are:


‒ Misspelled variable and function names
‒ Missing semicolons
‒ Unmatched parentheses, square brackets, and curly braces
‒ Using a variable that has not been declared

 The compiler can detect such errors.


32
Linker errors

 Linker errors are generated when the linker encounters what


looks like a function call; but it cannot find a function with that
name.

 Common examples are:


‒ misspelling C++ standard function (like main)
‒ not including the header file for a function

33
Runtime errors

 Occurs during the execution of a program when the computer


executes an illegal operation.

 Common examples are:


‒ trying to divide by a variable that contains a value of zero
‒ trying to open a file that does not exist
‒ referencing an out-of-range array element

 There is no way for the compiler to detect these errors when


the program is compiled. 34
Logic errors

 Occurs when a programmer implements the algorithm for


solving a problem incorrectly.

 Common examples are:


‒ multiplying when you should be dividing
‒ adding when you should be subtracting
‒ opening and using data from the wrong file
‒ displaying the wrong message

35
Logic errors

 Logic errors are the hardest to find and fix because:


‒ The compiler does not detect these errors
‒ There is no indication of error when the program is
executed
‒ The program may produce correct results for some input
data and wrong results for other input data

 Logic errors can only be detected by examining the program


thoroughly. This is usually done by using a debugger.
36
Error Detection Techniques

 cout statements

 Use debugger

37
Wrap up
■ Basic program construction

■ Input/output statements

■ Variable types

■ Arithmetic operations

■ Types of program errors

38
Next Lecture …

Flow of control
(Loops)

39
Questions?

40

You might also like