Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators.

Slides:



Advertisements
Similar presentations
CS&E 1111 ExIFs IFs and Nested IFs in Excel Objectives: Using the If function in spreadsheets Nesting If functions.
Advertisements

Selection (decision) control structure Learning objective
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Conditional Statements If these were easy then everyone would them!
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
If Statements & Relational Operators, Part 2 Programming.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Basic Of Computer Science
Flow of Control Part 1: Selection
CONTROLLING PROGRAM FLOW
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
22/11/ Selection If selection construct.
Chapter 4 Selection: the if-else and switch-case instructions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
1 SELECTION using IF and IF-ELSE Chapter 4. 2 Agenda Background  One Way selection (if) Two Way selection (if-else) Compound Statements Nested if-else.
Chapter Making Decisions 4. Relational Operators 4.1.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
1 CS161 Introduction to Computer Science Topic #8.
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,
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
COMP Loop Statements Yi Hong May 21, 2015.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Decision making If.. else statement.
Selection (also known as Branching) Jumail Bin Taliba by
Bill Tucker Austin Community College COSC 1315
Decisions Chapter 4.
Selection—Making Decisions
Chapter 4: Making Decisions.
Control Structures – Selection
Dr. Clincy Professor of CS
CSC530 Data Structure - Decision
Dr. Clincy Professor of CS
Making Logical Decisions (IF-THEN-ELSE)
Dr. Clincy Professor of CS
Chapter#3 Structured Program Development in C++
Chapter 7 Conditional Statements
Decision making If statement.
Lecture 6: Conditionals AP Computer Science Principles
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Dr. Clincy Professor of CS
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Dr. Clincy Professor of CS
Presentation transcript:

Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators logical expressions If statement if with true part if with true and false parts nested if

What Kinds of Questions Can I Ask? Is the sky blue? What is the meaning of Life? Unfortunately..It's not that easy! Then WHAT? 1. Are two values = =equal(BE CAREFUL!) !=not equal >greater >=greater than or equal <less than <=less than or equal Examples: a > 4, b = = 3 + d 2. Using those for more complicated &&and | |or Examples: ( a > b ) | | ( c <=d) (a > 3) && ( a < 12) means 3<a<12

RELATIONAL They're the easiest to learn.. NATURAL. expression RELATIONAL_OP expression 3 > a b = = c is ok but (2.3 + b) = = c is better ( b * 2 + d) ! = (23 - x) Result is either 1 (true) or 0 (false)

and A logical operator written &&. Also returns 1(true) or 0(false). BUT it expects inputs to be 1(true) or 0(false). Both inputs must be true for the result to be true. ENGLISH Are you awake and in cs230? Answer may be FALSE! Are you alive and in cs230? Answer is TRUE! Truth Table for && (and) Fig. 2.28, pg 107 expression1 expression2 expression1 && expression 2 false (0) false (0) false (0) false (0) true (1) false (0) true (1) false (0) false (0) true (1) true (1) true (1)

or A logical operator written | |. Also returns 1(true) or 0(false). It expects inputs to be 1(true) or 0(false). If either input or both are true the result is true. ENGLISH Are you a male or female? Answer TRUE for everyone! Are you in Richmond or in Newport News? Answer is TRUE! Truth Table for | | (or) Fig. 2.29, pg 107 expression1 expression2 expression1 || expression 2 false (0) false (0) false (0) false (0) true (1) true (1) true (1) false (0) true (1) true (1) true (1) true (1)

Some Examples of Logical Expressions wxyz logical expressionRESULT w > 41 (t) w > (y * 2)1 (t) (y - z) > (11 - w)0 (f) (w > 1) && ( x < 1)0 (f) (x > w) | | ( y > z)1 (t) Be careful about and/or/= =. Try this: answer true for all 21 and 22 year olds (age = 21) and (age = 22) should be (age = = 21) || (age = = 22) English can be misleading. We associate reasonable interpretations. 1.Wrong = / == 2. and instead of or 3. English instead of ||

if Statements One of the numerous places logical expressions are used. A fundamental program building-block. "if" allows us to ask questions. if (logical expression) do_this_when_true; Examples: if (radius < 0.0 ) cout << "Illegal value for radius"; // more than one statement in true part if(radius < 0.0 ) { cout << "Illegal value for radius"; radius = 0.0; } // p.80 example if (result == 1) passes = passes +1; else failures = failures +1;

Nested if Used in mutually exclusive (only one) case Examples: either freshman, soph, junior, senior either grade is A, B, C, D, F, I. either chevy, ford, chrysler which income tax bracket c++ Example: p. 65 (Chapter 2) book only has body of function // function to return correct letter grade // based on numerical test score char letter_grade( float score) { if (score >=90) return 'A'; else if (score >=80) return 'B'; else if (score >=70) return 'C'; else if (score >=60) return 'D'; else return 'F'; } New type Score (85.3) Letter grade (B)

Conclusion Understanding the operation of if is not too bad. Understanding how to use it will take practice. Most programs will use them. Labs will review them. When I lecture on program design, this will be addressed. For Now: Ignore switch and break statement. Practice exercises to understand how to read if and how to read/write logical expressions.