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.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Announcements Quiz 1 Posted on blackboard Handed Back (and Gone Over)Next Monday “Only a Quiz” Counts 5% of Final Grade Exam 1 Counts 10%
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Flow of Control Part 1: Selection
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 3 Selection Statements
Chapter 4: Control Structures I (Selection)
Chapter 3 Control Statements
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Control Structures I (Selection)
Selection (if-then-else)
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Selection Control Structure
Announcements Exercise Sets 2 and 3 Due Thursday.
Programming Fundamental
Presentation transcript:

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

More on Input Output The #include statements are also called headers The header #include 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

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

Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else): Control Proceeds Dependent on Conditions Iteration (looping): Control Repeated until Condition Met 4

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 ; } 5

Conditional Operators Relational Operators:, >=, <= Equality Operators: ==, != 6

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

else Statement Syntax: if ( condition ) { statement(s) ; //condition true } else { statement(s) ; //condition false } 8

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

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

Nested if Statements if (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

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

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

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

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

Logical Operators if (numStudents > MIN && numStudents < MAX) classRun = true; if (numStudents > MAX || numInstructors == 0) classRun = false; 16

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

Order of Operations Precedence: Level of Importance of Operations Multiplicative Operators Have Higher Precedence than Additive Operators: *, /, % Higher +, - Lower Associativity: Order of Operation for Equal Level Precedence Most Operators Have Left-to-Right Associativity Use Parentheses to Force Differing Precedence of Operations 18

Multiple Logical Operators if ( num1 > MAX || num2 == 0 && num3 == 0) cout << “num1 is MAX or something is 0”; cout << “I think…..” << endl; 19

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

Switch/Case Example int 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

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

Announcements Exam 1 Tuesday July 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