L02 C++
L02 C++
FC12323
Lecture 02
2
f
Symbols of low chart
Start or stop
Input or output
Process
Decision or condition
Flow
3
f
Problem solving
• In a programming environment the problem solving process requires the
following steps.
1. Analyse the problem, outline the problem and its solution
requirement and design algorithm to solve the problem.
2. Implement the algorithm in a programming language such as C++
and verify that the algorithm works.
3. Maintain the program by using and modifying it if the problem
domain changes.
4
Exercise
• Find the sum of two numbers by using a low chart and pseudo code.
5
f
Exercise Start
Pseudo code
Read 2 num x, y
Input x, y
Find sum S as x+y
Print S
S = x+ y
Print S
End
6
Exercise
• Find the average of three numbers by using a low chart and pseudo
code.
7
f
Exercise Start
Pseudo code
Input x, y, z
Read 3 num x, y, z
Find Average as (x+y+z)/3
Print Average
Average = (x+ y + z)/3
Print
Average
End
8
Exercise
• Find the given number is either odd or even by using a low chart and
pseudo code
• Hint : The modulo or mod is the remainder after dividing one number by
another.
Ex: 11 mod 4 = 3
2 mod 2 = 0
f
Exercise Start
Pseudo code
Read num x
Find Result as x mod 2 Input x
If result = 0
Print Even
No
Else Result = x mod 2 Result = 0 Print Odd
Print Odd
Yes
End
Print Even
10
Exercise
• Find the given character is either vowel or not by using a low chart and
pseudo code
• Hint : OR represent as || in low charts
11
f
f
Exercise Start
Pseudo code
Read character b
Input b
If b = “A” || “E” || “I” || “O” || “U” || “a” || “e” ||
“I” || “o” || “u”
Print b is a Vowel
Else b = “A” || “E” || “I” No
Print b is
Print b is not a vowel || “O” || “U” || “a” || “e” || “I” ||
not a vowel
“o” || “u”
Yes
Print b is a
End
vowel
12
Exercise
1. Develop a program to input three marks of a student and calculate the
average mark of the student.
2. Those who have achieved the average mark greater than 60 are selected
to special degree program.Modify the earlier problem to check whether
the student is selected to Special degree program.
13
Exercise
14
Exercise
15
Exercise
16
First C++ Program
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. cout << "Welcome to C++!\n";
7. return 0;
8.
9. }
17
#include preprocessing directive
#include <iostream>
• Line 1, #include <iostream>
• Lines that begin with # are processed by the preprocessor before the
program is compiled.
18
#include preprocessing directive
#include <iostream>
• Line 1, #include <iostream>
• This line noti ies the preprocessor to include in the program the contents
of the input/output stream header <iostream>
19
f
f
using namespace std
using namespace std;
• Line 2, using namespace std;
• Means that we can use names for objects and variables from the standard
library.
20
Blank Lines and White Space
• Line 3 : Simply a blank line.
• Use blank lines, space characters and tab characters (i.e., “tabs”) to make
programs easier to read.
21
The main function
int main()
• Line 4 : int main() is a part of every C++ program.
• The parentheses after main indicate that main is a program building block
called a function.
22
int main()
The main function {
• The keyword int to the left of main indicates that main “returns” an integer
(whole number) value.
• The left brace, {, (line 5) must begin the body of every function.
23
The output statement cout<< “Welcome to C++!\n“ ;
; : statement terminator
24
Escape sequence cout<< “Welcome to C++! “\n ;
• The escape sequence \n means newline. It causes the cursor (i.e., the
current screen-position indicator) to move to the beginning of the next line
on the screen.
25
The return statement {
return 0;
• Line 7 : return 0 }
• When the return statement is used at the end of main, as shown here, the
value 0 indicates that the program has terminated successfully.
26
Omitting Namespace
• The using namespace std line can be omitted and replaced with
the std keyword, followed by the :: operator for some objects
#include <iostream>
using namespace std; #include <iostream>
int main() {
int main() {
cout << “HelloWorld!”; std::cout << “HelloWorld!”;
return 0;
return 0;
}
}
27
Common errors
Omitting the semicolon at the end of a C++ statement is a syntax error. The syntax of a
programming language speci ies the rules for creating proper programs in that language.
A syntax error occurs when the compiler encounters code that violates C++’s language
rules (i.e., its syntax). The compiler normally issues an error message to help you locate
and ix the incorrect code. Syntax errors are also called compiler errors, compile-time
errors or compilation errors, because the compiler detects them during the compilation
phase. You cannot execute your program until you correct all the syntax errors in it.
28
f
f
C++ Case Sensitivity
Example:
Variable declaration - age vs AGE
29
ff
Errors in C++
• Types of Errors
1. Compilation errors
2. Link time errors
3. Runtime errors
4. Logical errors
30
Errors in C++
1. Compilation errors
31
Errors in C++
2. Link time errors
These error occurs when after compilation we link the di erent object iles
with main’s object using RUN command. These are errors generated when
the executable of the program cannot be generated.
incorrect header iles
Writing Main() instead of main()
32
f
ff
f
Errors in C++
3. Run time errors
Errors which occur during program execution(run-time) after successful
compilation are called run-time errors. These types of error are hard to
ind as the compiler doesn’t point to the line at which the error occurs.
division by zero also known as Division error
33
f
Errors in C++
4. Logical errors
On compilation and execution of a program, desired output is not
obtained when certain input values are given. These types of errors which
provide incorrect output but appears to be error free are called logical
errors.
Wrong logic de ining
34
f
Debugging
• Process of detecting and removing of existing and potential errors (also
called as ‘bugs’) in a software code that can cause it to behave
unexpectedly or crash.
35
f
Example (Find Errors)
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout < "C++ Programming";
6. cout << "C++! First lesson;
7. cout << "Welcome to Class"
8. return 0
9.
10. }
36
Example
1. #include <iostream>
2. using namespace std;
4. int main()
5. {
6. cout << "C++ Programming";
7. cout << "C++! First lesson\n";
8. cout << "Welcome to Class\n";
9. return 0;
10.
11. }
37
Comments //
//Text printing
38
Comments //
//Text printing
• You also may use comments containing one or more lines enclosed
in /* and */ . It is called Multiline comment.
39
Exercise
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout << "Hello" << "World" << "\n";
6. return 0;
7.
8. }
40
Exercise
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout << "Hello" <<"\n"<< "World" << "\n";
6. return 0;
7.
8. }
41
Exercise
1. #include <iostream>
2. using namespace std;
3. int Main()
4. {
5. cout << "Hello All, Welcome to our Module" << "\n";
6.
7. return 0;
8.
9. }
42
Tokens
• Tokens are the minimal chunk of program that have meaning to the
compiler – the smallest meaningful symbols in the language.
43
Basic language features
• Statement : A statement is a unit of code that does something –a basic
building block of a program.
44
C++ Data Types and Variables
Variables in C++
• Variables are containers for storing data values.
46
Identi iers (Variable names)
• A variable name (such as number1) is any valid identi ier that is not a
keyword.
47
f
f
ff
f
f
ff
Identi iers (Variable names)
• Spaces, punctuation marks, and symbols cannot be part of an identi ier.
48
f
f
f
f
Variable name or not
49