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

CS101

1) The document discusses a basic C++ program that prints "Welcome to C++" to the screen. It explains the key components of the program like main function, std::cout, and return 0. 2) It then explains some basic C++ concepts like comments, variables, data types, input/output streams, and memory concepts with visual representations. 3) Finally, it briefly discusses arithmetic operations in C++ like addition, subtraction, multiplication, division, modulus, and rules of operator precedence.

Uploaded by

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

CS101

1) The document discusses a basic C++ program that prints "Welcome to C++" to the screen. It explains the key components of the program like main function, std::cout, and return 0. 2) It then explains some basic C++ concepts like comments, variables, data types, input/output streams, and memory concepts with visual representations. 3) Finally, it briefly discusses arithmetic operations in C++ like addition, subtraction, multiplication, division, modulus, and rules of operator precedence.

Uploaded by

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

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

C++

Dr. Shahab Ansari

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.
5int main()
6{ preprocessor directive
7 std::cout << "Welcome to C++!\n"; Message to the C++ preprocessor.
Lines beginning with # are preprocessor
8 directives.
9 return 0; // indicate that program #includeended<iostream>
successfully tells the preprocessor
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 andtoa 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
5int 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
5int 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
5int 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: Introduction11
to Computing

Arithmetic
• Arithmetic operators:
C ++ o p e ra tio n Arith m e tic Alg e b ra ic C ++ e xp re ssio n
o p e ra to r e xp re ssio 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: Introduction12
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: Introduction13
to Computing
Decision Making: Equality and Relational
Operators
Sta n d a rd a lg e b ra ic C ++ e q u a lity Exa m p le Me a n in g o f
e q u a lity o p e ra to r o r o r re la tio n a l o f C ++ C ++ c o n d itio n
re la tio n a l o p e ra to r o p e ra to r c o n d 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: Introduction14
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>

6using std::cout; // program uses cout

7using std::cin; // program uses cin


Notice the using statements.
8using 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

20
cout << num1 << " is equal to " << num2 << endl;
the relationships The
they satisfy:
if statements test the truth of the
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;
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

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


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

Quiz 2

– Write a C++ program to input 5 numbers using while loop and find there
sum, average, and product.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like