Lesson 01
Lesson 01
What is Programming?
Computer Programming is defined in wiki books as:
“Computer programming is the craft of writing useful, maintainable, and extensible source code which can
be interpreted or compiled by a computing system to perform a meaningful task.”
Computers are very helpful machines, but we need to program them before using. They can’t do anything
on their own. In order to get a task performed by a computer “you (or someone else) have to tell it exactly
- in excruciating detail - what to do. Such a description of "what to do" is called a program, and
programming is the activity of writing and testing such programs.”
In order to give instructions to the computer, we need to speak to them in a language they understand. Once
we are able to do that we can write instructions for computer. Just like any common language computer
languages also have some vocabulary and grammar. While using those languages it is essential to comply
with the language grammar and use correct vocabulary. Otherwise computer won’t understand us; it may
end up doing something wrong or may get angry at us and throw errors and warnings.
Details about evolution of computer languages can be seen in the book.
Ungraded Assignment:
i. Find differences between Programmers and Users
ii. Create a list of modern programming languages with their preferred targets
Page 1 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Line
Description
No
Pre Processor Directive
to make available facilities from file iostream.h into this program
1 iostream is used when it is required to show some output on monitor, or take input from the
keyboard.
This directive will essentially be present in all the codes we will develop in this course
namespace is an advanced idea, for now just consider it as part of library
2
This line will also be present in all codes
3 A blank space; it has no effect on processing. Used just to make the code look tidy
this is the main function definition (main is a name and reserved keyword for C++)
Every C++ program must have a function called main to tell it where to start. A function is a
named sequence of instructions for computer to execute in order in which they are written. A
function has four parts:
i. A return type (here it is int)
4
ii. Name (main here)
iii. Parameter list (empty here)
iv. Function body, enclosed in a set of curly braces { }, which lists the actions called
statements that the function is to perform.
Understanding endl
les_01_code_02.cpp
1. #include<iostream>
2. using namespace std;
3.
4. int main()
5. {
6. cout<<"Hello"<<endl<<"world!";
7. return 0;
8. }
Page 2 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Exercise
Predict output of the following statements
Statement Output
Ungraded Task
Write a program that prints the following
Bonus Material
IEEE Spectrum Top Ten Programming languages
https://spectrum.ieee.org/computing/software/the-2017-top-programming-languages
Page 3 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
• Names of functions
• Names of arrays
• Names of variables
• Names of classes
Data types are important to understand because certain operations can only be performed on certain data
types. Following chart shows the broad categories of data types available in C++
Data Types
Float (double)
e.g. 3.1413, 53.7
• int can store number hold upto 4 bytes can store number from -2,000,000,000 to +2,000,000,000
(approximately)
• unsigned int can hold up to 4 bytes (only positive numbers) but that doubles the positive range,
ranges between 0 – 4,000,000,000
Data Types for floating point numbers
Page 4 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Only a single character can be stored in a variable of type char, char values are stored as an integer, based
on ASCII value of character e.g. ‘a’
char was important data type when systems had less memory, now a days memory in the systems is ample
so char is not much useful now.
Boolean Data
bool
bool is another data type, it holds true (1) or false (0) values.
Boolean bool
Character char
Integer int
Valueless void
Several of the basic types can be modified using one or more of these type modifiers:
• signed
• unsigned
• short
• long
The following table shows the variable type, how much memory it takes to store the value in memory, and
what is maximum and minimum value which can be stored in such type of variables.
Page 5 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Variables
Variables are named objects with a specific type. (Object is a region of memory with a type.) Variables
can be used to store data of a certain type which can later be used in the program. A variable must be
declared using a legal keyword (listed in table above) and a legal name. Following are the rules to assign
variable names:
Page 6 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
2.
3. using namespace std;
4.
5. int main()
6. {
7. int num1;
8. num1 = 2;
9.
10. char sym;
11. sym = '@';
12.
13. string name;
14. name = "Leo Tolstoy";
15.
16. cout<<"You inserted "<<num1<<" as num1"<<endl;
17. cout<<"You inserted "<<sym<<" as sym"<<endl;
18. cout<<"You inserted "<<name<<" as name"<<endl;
19.
20. return 0;
21. }
Output
les_01_code_04.cpp
1. #include<iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. int num2 = 2.5; // Initialization statement
8. cout<<num2;
9.
10. return 0;
11. }
Output
Page 7 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Common Mistakes
les_01_code_06.cpp
1. #include<iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. int num1 = 7;
8. cout<<Num1;
9.
10. cout<<num2;
11. int num2;
12.
13. strings name;
14.
15. return 0;
16. }
Page 8 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Interactive Computing
Often it is required in a program to take input from the user in run time. This idea of interacting with the
user is called interactive computing. This can be done in C++ thorough standard input stream object cin
(of namespace std) and the stream extraction operator >> (also called get from operator). The following
code will help you to understand the idea.
les_01_code_07.cpp
1. #include<iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. int num1;
8. cout<<"Please enter the value of num1 : ";
9. cin>>num1;
10. cout<<"You Entered : "<<num1;
11.
12. return 0;
13. }
Output
Page 9 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Possible Outputs
i. Entering values separated by space (whitespace is delimiter for cin)
Page 10 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Output
Note that whitespace is delimiter for the cin so everything you typed after Leo is lost
Remedy
Use getline() function to input strings
les_01_code_10.cpp
1. #include<iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. string name;
8. cout<<"Please enter the name : ";
9. getline(cin,name);
10. cout<<"Name you entered is : "<<name;
11.
12. return 0;
13. }
Page 11 of 12
EE-163(C&P) Lesson - I EED MHUH, FR
Output
Run 1
Run 2
Page 12 of 12