0% found this document useful (0 votes)
15 views

ITC Lect 08 (C++ - II)

Uploaded by

komega173
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

ITC Lect 08 (C++ - II)

Uploaded by

komega173
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture 08: C++ CS 101: Introduction to Computing

C++

Shahab Haider

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++ Comments
Written between /* and */ or following a //.
3 #include <iostream>
Improve program readability and do not cause
4 the computer to perform any action.
5 int main()
6 { preprocessor directive
7 std::cout << "Welcome to Message C++!\n"; to the C++ preprocessor.
Lines beginning with # are preprocessor
8 directives.
9 return 0; // indicate that program #include <iostream> tells the preprocessor
ended successfully
to include the contents of the file <iostream>,
10} C++
whichprograms
includescontain one oroperations
input/output more functions,
(such as
one of which must be
printing to the screen). main
Parenthesis are used to indicate a function
Welcome to C++! int means that main "returns" an integer value.
Prints the string of characters contained
More in Chapter 3. between
the quotation marks.
return is a way to exit a function
from a function. A leftstd::cout,
brace { begins
The entire line, including thethe
<< body of every
return 0, in this case, means function andto a right brace and
} ends it.
operator, the string "Welcome C++!\n"
that the program terminated
the semicolon (;), is called a statement.
normally.
All statements must end with a semicolon.
Lecture 08: C++ CS 101: Introduction3to Computing
A Simple Program:
Printing a Line of Text
• 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
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 08: C++ CS 101: Introduction4to Computing
Simple Program:
Printing a Line of Text
Escape Sequence Description

\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.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

• There are multiple ways to print text


– Following are more examples

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


1 // Fig. 1.4: fig01_04.cpp
2 // Printing a line with multiple statements
3 #include <iostream>
4
5 int main()
6 {
7 std::cout << "Welcome ";
8 std::cout << "to C++!\n";
9
10 return 0; // indicate that program ended successfully
11}

Welcome to C++!

Unless new line '\n' is specified, the text


continues on the same line.
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4
5 int main()
6 {
7 std::cout << "Welcome\nto\n\nC++!\n";
8
9 return 0; // indicate that program ended successfully

10}

Welcome
to

C++!
Multiple lines can be printed with one
statement.
Lecture 08: C++ CS 101: Introduction7to Computing
Another Simple Program:
Adding Two Integers
• 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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction8to Computing
Another Simple Program:
Adding Two Integers
• >> (stream extraction operator)
– When used with std::cin, waits for the user to input a value and
stores the value in the variable to the right of the operator
– The user types a value, 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)
– Example:
sum = variable1 + variable2;

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


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
10 std::cin >> integer1;
Notice how std::cin is used to get user
// 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; // indicate that program ended successfully std::endl flushes the buffer and
17} prints a newline.

Enter first integer Variables can be output using std::cout << variableName
45
Enter second integer
72
Lecture 08: C++ CS 101: Introduction10
to Computing

Memory Concepts
• Variable names
– Correspond to locations in the computer's memory
– Every variable has a name, a type, a size and a value
– Whenever a new value is placed into a variable, it replaces the previous
value - it is destroyed
– Reading variables from memory does not change them
• A visual representation

integer1 45

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction to Computing

• It works in two steps


• Declaration
• Initialization

• Although you can use both at the same time too


• Rules?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction to Computing

• Rules for naming variables


• It can be alphabetic / alphanumeric.
• It may include A-Z , a-z, 0-9.
• It must not start with a digit e.g 3a while a3 is legal.
• Underscore can be used.
• It must not be a keyword or a special character except the
$.
• C++ is case sensitive so A3 is different from a3.
• It should be meaningful.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction13
to Computing

Arithmetic
• Arithmetic operators:
C ++ operation Arithmetic Algebraic C ++ expression
operator expression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y

Modulus % r mod s r % s
• Rules of operator precedence:
Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses 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.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction14
to Computing
Decision Making: Equality and Relational
Operators
Standard algebraic C ++ equality Example Meaning of
equality operator or or relational of C ++ C ++ condition
relational operator operator condition
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
 >= x >= y x is greater than or equal to y
 <= x <= y x is less than or equal to y

Equality operators
= == x == y x is equal to y
 != x != y x is not equal to y

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction15
to Computing
Decision Making: Equality and 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;

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


1 // Fig. 1.14: fig01_14.cpp

2 // Using if statements, relational

3 // operators, and equality operators

4 #include <iostream>

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

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

17 Enter two integers, and I will


18 if ( num1 == num2 ) tell you
19 cout << num1 << " is equal to " << num2 << endl;
the relationships The
they satisfy:
if statements test the truth of the
20
3 7 condition. If it is true, body of if
21 if ( num1 != num2 )
statement is executed. If not, body is
22 cout << num1 << " is not equal to " << num2 << endl; skipped.
23 3 isTo include
not multiple
equal to 7in a
statements
24 if ( num1 < num2 ) body, delineate them with braces {}.
25 cout << num1 << " is less than " << num2 << endl;

26 3 is less than 7
27 if ( num1 > num2 )

28 cout << num1 << " is greater than " << num2 << endl;

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 )
35 cout << num1 << " is greater than or equal to "
36 << num2 << endl;
37
38 return 0; // indicate that program ended successfully
39 }

Enter two integers, and I will tell you


the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7

Enter two integers, and I will tell you


the relationships they satisfy: 22 12
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12

Enter two integers, and I will tell you


the relationships they satisfy: 7 7
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
Lecture 08: C++ CS 101: Introduction to Computing

• What is the difference between instruction, statement and


expression?

• What is the difference between user defined and builtin word?

• Keyword , reserve word and builtin word.

• User defined word , identifier

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction to Computing

Errors
• Types are
• Syntax error
» Compiler error

• Linker error

• Semantic error
» Runtime error / Exception
» Logical error

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction20
to Computing
Decision Making: Equality and Relational
Operators
• if structure
– Test conditions truth or falsity. If condition met execute, otherwise ignore
• Equality and relational operators
– Lower precedence than arithmetic operators

• Table of relational operators on next slide

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction to Computing

Decisions
• If statement
• If else statement
• Switch statement

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 08: C++ CS 101: Introduction to Computing

References
Dietal and Dietal : How to Program C++
3rd Edition

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like