Lec-03 - W3 (Layout of C++ Program)
Lec-03 - W3 (Layout of C++ Program)
Programming
Lecture – w3
Outline
#include <iostream>
using namespace std;
Program starts here
int main()
{
Variable_Declarations
Statement_1
Statement_2
…
Statement_Last
return 0;
} Program ends here
Layout of a C++ Program
Statement_1
Statement_2 executable statements
…
Statement_Last
➢<<
✓stream insertion operator
✓value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
std::cout << " Welcome to C++!\n"
➢\
✓escape character
✓indicates that a “special” character is to be output
Sample C++ Program
Escape Description
Sequence
\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.
1. #include <iostream>
2. int main()
3. {
4. std::cout << "Welcome ";
5. std::cout << "to C++!\n";
6. return 0; // indicate that program ended successfully
7. }
Welcome to C++!
1. #include <iostream>
2. int main()
3. {
4. 7 std::cout << "Welcome\nto\n\nC++!\n";
5. return 0; // indicate that program ended
successfully
6. }
Welcome
to
C++!
Sample C++ Program
1 // Program_01.cpp
3 #include <iostream>
5 int main()
6 {
10 }
Another Simple Program: Adding Two
Integers
➢>> (stream extraction operator)
✓When used with std::cin, waits for user to input a value and
stores the value in the variable to the right of the operator.
✓user types number, then presses the Enter (Return) key to send
the data to the computer
✓Example:
int myVariable;
std::cin >> myVariable;
waits for user input, then stores input in myVariable
➢= (assignment operator )
• assigns value to a variable
• binary operator (has two operands)
sum = variable1 + variable2;
Addition operator
Another Simple Program: Adding Two
1 // Fig. 1.6: fig01_06.cpp
2 // Addition program
Integers
3 #include <iostream>
4
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 std::cout << "Enter first integer\n"; // prompt
Notice how std::cin is used to get
10 std::cin >> integer1; // read an integer
user input.
11 std::cout << "Enter second integer\n"; // prompt
12 std::cin >> integer2; // read an integer
13 sum = integer1 + integer2; // assignment of sum
14 std::cout << "Sum is " << sum << std::endl; // print sum
15
16 return 0; std::endl flushes the buffer
// indicate that program ended successfully
17 } and prints a newline.
#include <iostream>
using namespace std;
int main()
{
int number1, number2, sum;
return 0;
}
Testing and Debugging
➢Bug
A mistake/error in the program
➢Debugging
The process of eliminating bugs in a program
Testing and Debugging
product =…………………..?
return 0;
}
Thank You