0% found this document useful (0 votes)
9 views21 pages

3 2

Uploaded by

Nabeel Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views21 pages

3 2

Uploaded by

Nabeel Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Enumeration

 Data type defined by users.


enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
week today;
today = Wednesday;
cout << "Day " << today+1;
Input and output

 For input and output, we use cin and cout respectively.


 Syntax for using cout is
cout<<“Hello world”;
or
cout<<name_variable;
 Where << is called insertion operator.
 Syntax for cin is
cin>>name_variable;
 Where >> is extraction operator.
cin and the get Function

• The get function


− Inputs next character (including whitespace)
− Stores in memory location indicated by its
argument
• The syntax of cin and the get function:

varChar
− Is a char variable
− Is the argument (parameter) of the function
char ch1, ch2; What is stored in ch1,
int num; ch2 and num if the input
is:
cin >> ch1 >> ch2 >> num;
A 25

char ch1, ch2; What is stored in ch1, ch2


int num; and num if the input is:
A 25
cin.get(ch1);
cin.get(ch2);
cin >> num;

char ch1, ch2;


int num; What is stored in ch1, ch2
and num if the input is:
cin >> ch1; A 25
cin.get(ch2);
cin >> num;
Arithmetic Operators
• C++ arithmetic operators:
− + addition
− - subtraction
− * multiplication
− / division
− % modulus operator
• +, -, *, and / can be used with integral and floating-
point data types.
• Operators can be unary (one operand) or binary (two
operands)
 % is modulus operator and is used to calculate remainder of a division.
 10%2 will results in 0
 13%2 will results in 1
 4%5 will results in 4
 20%7 will result in 6
 #include <iostream>
 #include<conio.h>
 using namespace std;

 void main()
 {
 cout << "-2 + 5 = " << -2 + 5 << endl;
 cout << "10 - 20 = " << 10 - 20 << endl;
 cout << "2 * 7 = " << 2 * 7 << endl;
 cout << "5 / 2 = " << 5 / 2 << endl;
 cout << "5.0 / 2 = " << 5.0 / 2 << endl;
 cout << "5 / 2.0 = " << 5 / 2.0 << endl;
 cout << "34 % 5 = " << 34 % 5 << endl;
 cout << "4 % 6 = " << 4 % 6 << endl;

 getch();
 }
Relational Operators

 Use to compare two variables/values.


 They return true(1) or false (0).
Comparing Characters
Logical Operators

 Use to combine logical expressions


Order of Precedence
• All operations inside of () are evaluated first
• *, /, and % are at the same level of
precedence and are evaluated next
• + and – have the same level of precedence
and are evaluated last
• When operators are on the same level
− Performed from left to right (associativity)
• 3 * 7 - 6 + 2 * 5 / 4 + 6 means
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
Expressions

• If all operands are integers


− Expression is called an integral expression
• Yields an integral result
• Example: 2 + 3 * 5
• If all operands are floating-point
− Expression is called a floating-point
expression
• Yields a floating-point result
• Example: 12.8 * 17.5 - 34.50
Mixed Expressions

• Mixed expression:
− Has operands of different data types
− Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
Mixed Expressions (continued)

• Evaluation rules:
− If operator has same types of operands
• Evaluated according to the type of the operands
− If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
− Entire expression is evaluated according to
precedence rules
What is the output?
 #include <iostream>
 #include<conio.h>
 using namespace std;

 void main()
 {
 cout << "3 / 2 + 5.5 = " << 3 / 2 + 5.5 << endl;
 cout << "10.6 / 2 + 5 = " << 10.6 / 2 + 5 <<
endl;
 cout << "4 + 5 / 2.0 = " << 4 + 5 / 2.0 << endl;
 cout << "4 * 3 + 7 / 5 - 12.5 = "
 << 4 * 3 + 7 / 5 - 12.5
 << endl;

 getch();
 }

You might also like