Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout << “You have an A!” << endl; else cout << “You have a B!” <<

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
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)
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.
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.
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.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
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):
Basic Of Computer Science
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
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,
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
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.
Branching statements.
Chapter 3 Selection Statements
Chapter 3 Control Statements
Week 3 Part 2 Kyle Dewey.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
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)
Control Structures – Selection
Expression Review what is the result and type of these expressions?
Selection (if-then-else)
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Chapter 7 Conditional Statements
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Flow Control Statements
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
Structured Program Development in C++
Decisions, decisions, decisions
Life is Full of Alternatives
Control Statements Paritosh Srivastava.
CprE 185: Intro to Problem Solving (using C)
Selection Control Structure
Announcements Exercise Sets 2 and 3 Due Thursday.
Presentation transcript:

Quiz 1 Exam 1 Next Week

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;

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;

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);... 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

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

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

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

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

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

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

Switch/Case Example const double BASERATE = ; double rate; 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; }

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

Announcements Exam 1 Next Week in Lecture Everything through Lab 4 50 minutes 10 % of Total Grade Covers Everything through Lecture this Week –Quiz 1 Topics (terminology, variables, constants, basics) –if-then-else –Compound statements –Relational operators –Logical operators –Switch/Case Statements