more on input output input stream : a sequence of characters from an input device (like the...

23
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream : A sequence of characters from the computer (the program running) to an output device (like the monitor). Standard Input Device: a reference to the keyboard. Standard Output Device: A reference to the monitor. 1

Upload: brendan-woods

Post on 31-Dec-2015

258 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

More on Input Output

• Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running).

• Output Stream : A sequence of characters from the computer (the program running) to an output device (like the monitor).

• Standard Input Device: a reference to the keyboard.

• Standard Output Device: A reference to the monitor.

1

Page 2: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

More on Input Output

• The #include statements are also called headers

• The header #include <iostream> includes the standard streams to the standard output and standard input devices as long as we have the statement:

• namespace std; or the alternative

– std::cout;– std::cin;

2

Page 3: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

More on Input Output

• The symbol >> stands for extraction operator.– It is a binary operator

– The left operand is a stream and the right operand is a memory location.

– i.e

double payRate;

cin>>payRate;

The computer inputs the number typed on keyboard and stores it in memory location identified as payRate.

3

Page 4: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Selection (if-then-else)

• Programming Has 3 Types of Control:Sequential (normal): Control of Execution

Proceeds One after the OtherSelection (if-then-else): Control Proceeds

Dependent on ConditionsIteration (looping): Control Repeated until

Condition Met

4

Page 5: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

C++ if Statement Syntax• Syntax

if (condition) statement;

• If Condition Is True, Statement Is Executed• If Condition Is False, Statement Is Not Executed• If Multiple Actions Are Required for Statement, a Compound

Statement Must Be Used:if (condition) { statement; statement;}

5

Page 6: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Conditional Operators

• Relational Operators: < , > , >= , <=• Equality Operators: == , !=

6

Page 7: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Selection Examples

if (age < 30)

cout << “You are very very Young” << endl;

if (grade == ‘A’)

cout << “Congratulations!” << endl;

if (grade != ‘F’)

cout << “You passed!” << endl;

7

Page 8: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

else Statement

• Syntax:if (condition)

{statement(s); //condition true

}else {

statement(s); //condition false

}8

Page 9: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

if-else Example

if (myGrade >= 60){ cout << “You passed!” << endl;}else{ cout << “How about them Cubs?” << endl;

}9

Page 10: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Compound Statements

• Compound Statement: One or More Statements within a Set of Curly Braces

• Must Use Compound Statement if More than One Statement Is Supposed to be under Control of if or else Conditional

• Include Curly Braces after if so if other Statements Are Added in Future, Curly Braces Are Already There

10

Page 11: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Nested if Statementsif (myGrade >= 80)

if (myGrade >= 90)

cout << “You have an A!” << endl;

else

cout << “You have a B!” << endl;

else

cout << “We’ll give you a C.” << endl;

11

Page 12: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

if/else (Cascaded)

• Sequence of if/else Statements• Example:if (myGrade > 90) cout << “A!” << endl;else if (myGrade > 80) cout << “B!” << endl; else if (myGrade > 70) cout << “C!” << endl; else cout << “Oh-oh!” << endl;

12

Page 13: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Boolean Type• A Boolean Value Is One of Either “True” or “False”• In C++, Type bool• Example:

bool done = false; . . .if (currentLetter == ‘Z’) done = true;else done = false;…if (done) return(0); . . .

13

Page 14: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Boolean Type

• if/else Conditionals Must Evaluate to True or False, and Are Therefore Called Boolean Expressions

• In C++, Any Non-Zero Value Is Considered True; Any Expression Evaluating to Zero Is Considered False

14

Page 15: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Logical Operators

• A Logical Operator Is One Used to Further Specify True or False in an Expression

• Connects Two or More Expressions Together• && Is Logical “AND”• || Is Logical ‘OR”• &&: Both Operands Must Be True for Entire

Expression to Be True• ||: Only One (or Both) of the Operands Must

Be True for Entire Expression to Be True

15

Page 16: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Logical Operators

if (numStudents > MIN && numStudents < MAX)

classRun = true;

if (numStudents > MAX || numInstructors == 0)

classRun = false;

16

Page 17: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Operator Precedence• ()• ! (not)• *, /, %• +, -• <, <=, >, >= (Relational Operators)• ==, != (Equality Operators)• && (Logical AND)• || (Logical OR)• = (ASSIGNMENT)

17

Page 18: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Order of Operations

• Precedence: Level of Importance of OperationsMultiplicative Operators Have Higher Precedence than

Additive Operators:*, /, % Higher+, - Lower

• Associativity: Order of Operation for Equal Level PrecedenceMost Operators Have Left-to-Right AssociativityUse Parentheses to Force Differing Precedence of

Operations

18

Page 19: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Multiple Logical Operators

if ( num1 > MAX || num2 == 0 && num3 == 0)

cout << “num1 is MAX or something is 0”;

cout << “I think…..” << endl;

19

Page 20: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Switch Statements

• Also Called Switch/Case Statement

• Just Case in Other Languages

• Selects Among Several Different Actions

• Can Only Select from Integer or Character

• If an Integer Value Is Matched, Statements under Control of that Case Block Are Executed

20

Page 21: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Switch/Case Exampleint numPassengers;cout << “Enter Passengers: “;cin >> numPassengers;switch(numPassengers){ case 2: rate = BASERATE * 0.80; break; case 4: rate = BASERATE * 0.75; break; case 5: rate = BASERATE * 0.55; break; default: rate = BASERATE; break;}

21

Page 22: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Switch Case Example char menuItem; cout << "Enter Menu Selection: "; cin >> menuItem; switch(menuItem) { case 'O': /* code to do ordering goes here*/ break; case 'C': /* code to do checkout goes here*/ break; default: cout << “Unknown option” << endl; break; }

22

Page 23: More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream

Announcements

• Exam 1 Tuesday July 14.• 25 minutes• 10 % of Total Grade• Covers Everything through Lectures 03 and o4

– All Terms (Underlined Items)– Variable Declaration, Initialization, and Assignment– Constant Declaration– Expressions– Operators– Input (cin) and Output (cout)– if/else and else if selections– Logical Operators– Switch

23