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

ITC Lect 08 (C++ - II)

1. The document discusses a C++ lecture that covers basic C++ concepts like comments, functions, input/output streams, variables, and arithmetic. 2. It provides examples of simple C++ programs that print text, get user input, perform addition of integers, and demonstrates memory concepts and variable usage. 3. The lecture also covers C++ topics like comments, functions, input/output streams, variables, escape sequences, and arithmetic operators.

Uploaded by

sayed Tamir jan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

ITC Lect 08 (C++ - II)

1. The document discusses a C++ lecture that covers basic C++ concepts like comments, functions, input/output streams, variables, and arithmetic. 2. It provides examples of simple C++ programs that print text, get user input, perform addition of integers, and demonstrates memory concepts and variable usage. 3. The lecture also covers C++ topics like comments, functions, input/output streams, variables, escape sequences, and arithmetic operators.

Uploaded by

sayed Tamir jan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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

C++

Dr. Zahid Halim

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++ Comments
3 #include <iostream> Written between /* and */ or following a //.
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 #include <iostream> tells the preprocessor
that program
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 main
printing to the screen).
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,
The entire line, including brace { begins 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; //
8
declaration
9 std::cout << "Enter first integer\n"; //
Notice how std::cin is used to get user
10 std::cin >> integer1;
prompt input. //
11 std::cout
read an integer << "Enter second integer\n"; //
12 std::cin >> integer2;
prompt //
13 suminteger
read an = integer1 + integer2; //
14 std::cout
assignment << "Sum is " << sum << std::endl;
of sum
15
// print sum
16 return 0; // indicate that program endedflushes the buffer and
std::endl
17 }
successfully 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: Introduction11
to Computing

Arithmetic
• Arithmetic calculations
– Use * for multiplication and / for division
– Integer division truncates remainder
• 7 / 5 evaluates to 1
– Modulus operator returns the remainder
• 7 % 5 evaluates to 2
• Operator precedence
– Some arithmetic operators act before others (i.e., multiplication before
addition)
• Be sure to use parenthesis when needed
– Example: Find the average of three variables a, b and c
• Do not use: a + b + c / 3
• Use: (a + b + c ) / 3
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 08: C++ CS 101: Introduction12
to Computing

Arithmetic
• Arithmetic operators:
C++ o p era tio n Arithm etic Alg eb ra ic C++ exp ressio n
o p era to r exp ressio n
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: Introduction13
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: Introduction14
to Computing
Decision Making: Equality and Relational
Operators
Sta nd a rd a lg e b ra ic C++ e q ua lity Exa m p le Me a ning o f
e q ua lity o p e ra to r o r o r re la tio na l o f C++ C++ c o nd itio n
re la tio na l o p e ra to r o p e ra to r c o nd itio n

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>
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
17
Enter two integers, and I will
18 if ( num1 == num2 ) tell you
19 cout << num1 << " is equal tothe
" << relationships
num2 << endl; they
The satisfy:
if statements test the truth of the
20
21 if ( num1 != num2 )
3 7 condition. If it is true, body of if
statement is executed. If not, body is
22 cout << num1 << " is not equal to " << num2 << endl;
23
3skipped.
is not
To include multipleequal
statementstoin a 7
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

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like