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

Chap02 InputOutput and Operators P

This document covers fundamental concepts of C++ programming, including input/output operations, arithmetic operators, and decision-making using if statements. It provides examples of simple C++ programs, such as printing text and adding integers, while explaining data types and operator precedence. Additionally, it includes multiple-choice questions to test understanding of the material presented.

Uploaded by

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

Chap02 InputOutput and Operators P

This document covers fundamental concepts of C++ programming, including input/output operations, arithmetic operators, and decision-making using if statements. It provides examples of simple C++ programs, such as printing text and adding integers, while explaining data types and operator precedence. Additionally, it includes multiple-choice questions to test understanding of the material presented.

Uploaded by

Saja shamas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

University of Windsor

C++ Programming
Input/Output and Operators

Chapter 2 of C++ How to Program, 10/e

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 1


© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 2
2.2 First Program in C++: Printing a Line of Text

Write a C++ program that displays the message 'Welcome to C++!' on the
screen.

#include <iostream> // enables program to output data to the screen

int main() { The backslash (\) is called an escape character.


std::cout << "Welcome to C++!\n"; // display message

return 0; // indicate that program ended successfully


} // end function main

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 3


2.3 Modifying Our First C++ Program

1 // Fig. 2.3: fig02_03.cpp


2 // Printing a line of text with multiple statements.
3 #include <iostream> // enables program to output data to the screen
4
5 // function main begins program execution
6 int main() {
7 std::cout << "Welcome ";
8 std::cout << "to C++!\n";
9 } // end function main

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 4


2.2 Escape Characters

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 5


#include <iostream> // enables program to output data to the screen

// function main begins program execution


int main() {
std::cout << "Welcome\nto\n\nC++!\n";
} // end function main

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 6


2.6 C++ Arithmetic Operators

• Integer division (i.e., where both the numerator and the denominator are integers) yields
an integer quotient.
– Any fractional part in integer division is discarded (i.e., truncated)—no rounding occurs.
© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 7
2.6 Precedence of Arithmetic Operators

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 8


2.6 Order of Evaluation

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 9


2.4 Another C++ Program: Adding Two Integers
1 // Fig. 2.5: fig02_05.cpp
2 // Addition program that displays the sum of two integers.
3 #include <iostream> // enables program to perform input and output
4
5 // function main begins program execution
6 int main() {
7 // declaring and initializing variables
8 int number1{0}; // first integer to add (initialized to 0)
9 int number2{0}; // second integer to add (initialized to 0)
10 int sum{0}; // sum of number1 and number2 (initialized to 0)
11
12 std::cout << "Enter first integer: "; // prompt user for data
13 std::cin >> number1; // read first integer from user into number1
14
15 std::cout << "Enter second integer: "; // prompt user for data
16 std::cin >> number2; // read second integer from user into number2
17
18 sum = number1 + number2; // add the numbers; store result in sum
19
20 std::cout << "Sum is " << sum << std::endl; // display sum; end line
21 } // end function main
© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 10
Fundamental Data Types

C++ is a statically typed language which mean that you must


int age = 30;
declare the data type of a variable when you define it.
double pi = 3.14159 265359 ;
char grade = 'A';

The compiler enforces that the variable is used in a manner


consistent with its declared type. Better performance and
higher code security

Phyton is a dynamically typed language which mean that you age = 30


, you typically do not need to explicitly declare the data type name = "Alice"
of a variable when you define it. x = 5
x = "Hello"
The data type of a variable is assigned based on the value it
Easier to write codes
holds at runtime, while the program is executing.

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 11


Fundamental Data Types

Hierarchy of Data Types


largest data type long double
double
float
unsigned long int
long int
unsigned int
int
smallest data type char, short int

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 12


Fundamental Data Types Size and Range

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 13


Multiple Choice Questions
Which of the following is the correct way to declare a long long variable in C++?

a) long long int x;


b) int long long x;
c) long long x;
d) long long x int;

What happens when an int variable is assigned a value larger than the maximum limit of the int type in
C++?

a) It results in a compilation error


b) The variable is assigned the value zero
c) The value wraps around to the minimum value for the type
d) The variable holds the maximum possible value for an int

Max int value: 2147483647 +1


Value after overflow: -2147483648
© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 14
Multiple Choice Questions
1. What is the default value of an uninitialized int variable in C++?
o a) 0
o b) Undefined
o c) NULL
o d) 1

2. Which of the following is NOT a valid data type in C++?


o a) int
o b) bool
o c) string
o d) number
© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 15
Multiple Choice Questions

3. A char variable in C++ can hold a single character and has a size of 2 bytes.
True / False

4- C++ supports automatic type conversion between all primitive types.


True / False

5- The bool data type can only hold true or false values.
True / False

6- In C++, the type modifier long is used to increase the storage size of data types like
_________ and _________.

7- What is the significance of using const with a variable in C++?

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 16


2.6 Decision Making (if Statement)

• If the condition is true (i.e., the condition is met) the statement in the body of
the if statement is executed.

• If the condition is false (i.e., the condition isn’t met) the body statement is not
executed.

• Whether the body statement is executed or not, after the if statement


completes, execution proceeds with the next statement after the if statement.

• Conditions in if statements are formed by using the equality operators and


relational operators.

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 17


if Selection Statement
if (condition)
{
// Code to be executed if the Syntax
condition is true
}

if ( grade >= 60 ) {
std::cout << "Passed\n" ;
} // end if

Rectangles for actions - Diamonds for decision points

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 18


2.6 Relational and Equity Operators

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 19


2.6 Decision Making Example

Write a program to read two integers of A and B and determine the


relationship they satisfy as follows.

A is equal to B
A is not equal to B

A is less than B
A is greater than B

A is less than or equal to B


A is greater than or equal to B

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 20


2.7 Comparing Integers using if Statements
1 // Fig. 2.13: fig02_13.cpp
2 // Comparing integers using if statements, relational operators
3 // and equality operators.
4 #include <iostream> // enables program to perform input and output
5
6 using std::cout; // program uses cout
7 using std::cin; // program uses cin
8 using std::endl; // program uses endl
9
10 // function main begins program execution
11 int main() {
12 int number1{0}; // first integer to compare (initialized to 0)
13 int number2{0}; // second integer to compare (initialized to 0)
14
15 cout << "Enter two integers to compare: "; // prompt user for data
16 cin >> number1 >> number2; // read two integers from user
17
18 if (number1 == number2) {
19 cout << number1 << " == " << number2 << endl;
20 }
© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 21
2.7 Comparing integers using if statements (cont.)
22 if (number1 != number2) {
23 cout << number1 << " != " << number2 << endl;
24 }
25
26 if (number1 < number2) {
27 cout << number1 << " < " << number2 << endl;
28 }
29
30 if (number1 > number2) {
31 cout << number1 << " > " << number2 << endl;
32 }
33
34 if (number1 <= number2) {
35 cout << number1 << " <= " << number2 << endl;
36 }
37
38 if (number1 >= number2) {
39 cout << number1 << " >= " << number2 << endl;
40 }
41 } // end function main
© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 22
2.7 Comparing integers using if statements(cont.)

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 23


2.7 Decision Making: Equality and Relational Operators

• using declarations eliminate the need to repeat the std:: prefix.

– Can write cout instead of std::cout, cin instead of std::cin and endl instead of
std::endl, respectively, in the remainder of the program.

• Many programmers prefer to use the declaration

using namespace std;

Each if statement’s body is enclosed in a pair of braces, { }, creating what’s called


a compound statement or a block that may contain multiple statements.

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 24


2.7 Precedence and Associativity of the Operators

© 2016 Pearson Education GENG 2320 Engineering Software Fundamentals R. Rashidzadeh 25

You might also like