Lecture 1 - Introduction to Computer Programming Cs217
Lecture 1 - Introduction to Computer Programming Cs217
Introduction
Lecture No. 1
• Software
• Programs that run a computer
Making Software Load into
memory and
start execution
Problem
Executable file
Develop
Algorithm
Compile the code
Express Algorithm in the
form of pseudo code or
flow chart Write algorithm in
C/C++/Java … Language
5. Load ..
..
Primary
Memory
6. Execute CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
..
executes.
..
..
Introduction to C++ Programming
• C++ language
• Facilitates a structured and disciplined approach to computer program design
• std::cout
• Standard output stream object
• “Connected” to the screen
• std:: specifies the "namespace" which cout belongs to
• std:: can be removed through the use of using statements
• <<
• 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
A Simple Program: Printing a Line of Text
Escape Sequence Description
3 #include <iostream>
5 int main()
6 {
11 }
Welcome to C++!
• Variables
• Location in memory where a value can be stored for use by a
program
• Must be declared with a name and a data type before they
can be used
• Some common data types are:
• int - integer numbers
• char - characters
• double - floating point numbers
• Example: int myvariable;
• Declares a variable named myvariable of type int
• Example: int variable1, variable2;
• Declares two variables, each of type int
Another Simple Program: Adding Two Integers
• = (assignment operator)
• Assigns value to a variable
• Binary operator (has two operands)
• Example:
sum = variable1 + variable2;
1 // Fig. 1.6: fig01_06.cpp
2 // Addition program
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 user
10 std::cin >> integer1; // read an integer
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
// indicate that program ended successfully flushes the buffer and
17 } prints a newline.
Modulus % r mod s r % s
()
• Rules ofParentheses
operator precedence:
Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.
Decision Making: Equality/Relational Operators
• using statements
• Eliminate the need to use the std:: prefix
• Allow us to write cout instead of std::cout
• To use the following functions without the std:: prefix, write
the following at the top of the program
using std::cout;
using std::cin;
using std::endl;
1 // Fig. 1.14: fig01_14.cpp
2 // Using if statements, relational
3 // operators, and equality operators
4 #include <iostream>
5
6 using std::cout; // program uses cout
7 using std::cin; // program uses cin Notice the using statements.
8 using std::endl; // program uses endl
9
10 int main()
11 {
12 int num1, num2;
13
14 cout << "Enter two integers, and I will tell you\n"
15 << "the relationships they satisfy: ";
16 cin >> num1 >> num2; // read two integers
Enter two integers, and I will tell you
17
the relationships they satisfy: 3 7
18 if ( num1 == num2 )
19 cout << num1 << " is equal to " << num2 << endl;
20
The if statements test the truth
21 if ( num1 != num2 ) of the condition. If it is true,
22 cout << num1 << " is not equal to " << num2 << endl; 3body of if
is not statement
equal to 7 is
23 executed. If not, body is
24 if ( num1 < num2 ) skipped.
25 cout << num1 << " is less than " << num2 << endl; 3 is less than 7
26 To include multiple statements
27 if ( num1 > num2 ) in a body, delineate them with
28 cout << num1 << " is greater than " << num2 << endl;
braces {}.
29
30 if ( num1 <= num2 )
31 cout << num1 << " is less than or equal to " 3 is less than or equal to 7
32 << num2 << endl;
33
34 if ( num1 >= num2 )
37
39 }
• Text Books:
• “Object Oriented Programming in C++”, Author (s): Robert
Lafore.
• “C++ How to program”, Author (s): M. Deitel & Dietel.
• “Turbo C Programming”, Author (s): Robert Lafore.
• Questions?