1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

INTRODUCTION LOGICAL OPERATIONS TRUTH TABLE AND RULES.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
If Statements & Relational Operators Programming.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Control Constructs Mechanisms for deciding when and how often an action should be taken.
Chapter 6 Horstmann Programs that make decisions: the IF command.
CSRU 1100 Logic. Logic is concerned with determining: Is it True? Is it False?
Assignment 2 Sample problems. Consider the following expression: ((False and not True) or False or (True and not True)) True False.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
Logic You will learn three common logical operations that will be applied in much of the course (spreadsheets, databases, web searches and both programming.
Computer Science 101 Boolean Algebra. What’s next? A new type of algebra – Helps us A new type of algebra – Helps us With logical reasoningWith logical.
Maths and the History of ICT
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Data types and their representation Jordi Cortadella Department of Computer Science.
Decision Structures and Boolean Logic
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
Logic Disjunction A disjunction is a compound statement formed by combining two simple sentences using the word “OR”. A disjunction is true when at.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
A-Level Computing#BristolMet Session Objectives#6 MUST understand and produce simple logic diagrams using the operations NOT, AND and OR SHOULD explain.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
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 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
Thinking Mathematically
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.
***** SWTJC STEM ***** Chapter 5 cg 57 Selection Construct Choices 1.One-way selection - Use simple if.  2.Two-way selection - Use if - else.  3.if blocks.
Boolean Logic in Programming. Boolean Logic Branching and looping routines both contain conditions that are either true or false. In 1854 George Boole.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Computer Programming Boolean Logic Trade & Industrial Education
Control statements Mostafa Abdallah
DIGITAL ELECTRONICS. Everything in digital world is based on binary system. Numerically it involves only two symbols 0 or 1. –0 = False = No –1 = True.
Decisions. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements for every run of the program.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
CS 106 Logic and Conditionals (Boolean Expressions)
Decisions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Decision Structures Michele Co CS 101-E. 2 UVa CS101E Spring 2007 Today if statements Logical expressions –Truth tables –Boolean algebra –DeMorgan’s laws.
Computer Programming Boolean Logic.
CPS120 Introduction to Computer Science
Morgan Kaufmann Publishers
Computer Science 210 Computer Organization
Chapter 4 : Control Construct.
Thinking Mathematically
A mechanism for deciding whether an action should be taken
Logic Gates.
Logic Gates Benchmark Companies Inc PO Box Aurora CO
4-1 LOGIC OPERATIONS In Chapter 3 we discussed the fact that data inside a computer is stored as patterns of bits. Logic operations refer to those operations.
Aberdeen Grammar School
Introduction To Robot Decision Making
Expression Review what is the result and type of these expressions?
Bools and simple if statements
3.4 Computer systems Boolean logic Lesson 2.
Computer Science 210 Computer Organization
Logic Gates.
Introduction To Robot Decision Making
ECS15 boolean.
Truth tables Mrs. Palmer.
COMS 261 Computer Science I
Algebra: Variables and Expressions
Computer Programming Boolean Logic Trade & Industrial Education
Evaluating Algebraic Expressions
Conditionals.
Presentation transcript:

1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.

Boolean Algebra Logical expressions have the one of two values - true or false A rectangle has three sides The instructor has a pleasant smile The branch of mathematics is called Boolean algebra Developed by the British mathematician George Boole in the 19th century Three key logical operators And Or Not

Boolean Algebra Truth tables Lists all combinations of operand values and the result of the operation for each combination Example PQ P and Q False FalseFalse False TrueFalse True FalseFalse True TrueTrue

Boolean Algebra Or truth table PQ P or Q False FalseFalse False TrueTrue True FalseTrue True TrueTrue

Boolean Algebra Not truth table Pnot P False True True False

Boolean Algebra Can create complex logical expressions by combining simple logical expressions Example not (P and Q) A truth table can be used to determine when a logical expression is true PQ P and Qnot (P and Q) False FalseFalseTrue False TrueFalseTrue True FalseFalseTrue True TrueTrueFalse

A Boolean Type C++ contains a type named bool Type bool has two symbolic constants true false Boolean operators The and operator is && The or operator is || The not operator is ! Warning & and | are also operators so be careful what you type

A Boolean Type Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = (P && Q); bool T = ((!Q) || R); bool U = !(R && (!Q));

Relational Operators Equality operators == != Examples int i = 32; int k = 45; bool q = (i == k); bool r = (i != k);

Relational Operators Ordering operators < > >= <= Examples int i = 5; int k = 12; bool p = (i < 10); bool q = (k > i); bool r = (i >= k); bool s = (k <= 12);

Operator Precedence Revisited Precedence of operators (from highest to lowest) Parentheses Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment

Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24

Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 Yuck! Do not write expressions like this!

Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24  However, for your information it is equivalent to

Conditional Constructs Provide Ability to control whether a statement list is executed Two constructs If statement  if  if-else  if-else-if Switch statement  Left for reading

The Basic If Statement Syntax if (Expression) Action If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression Action truefalse

Example if (Value < 0) { Value = -Value; }

Sorting Two Numbers cout << "Enter two integers: "; int Value1; int Value2; cin >> Value1 >> Value2; if (Value1 > Value2) { int RememberValue1 = Value1; Value1 = Value2; Value2 = RememberValue1; } cout << "The input in sorted order: " << Value1 << " " << Value2 << endl;

Semantics

What is the Output? int m = 5; int n = 10; if (m < n) ++m; ++n; cout << " m = " << m << " n = " n << endl;

The If-Else Statement Syntax if (Expression) Action 1 else Action 2 If Expression is true then execute Action 1 otherwise execute Action 2 if (v == 0) { cout << "v is 0"; } else { cout << "v is not 0"; } Expression Action 1 Action 2 true false

Finding the Max cout << "Enter two integers: "; int Value1; int Value2; cin >> Value1 >> Value2; int Max; if (Value1 < Value2) { Max = Value2; } else { Max = Value1; } cout << "Maximum of inputs is: " << Max << endl;

Finding the Max

Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice if-else-if statement  if-else statements “glued” together Switch statement  An advanced construct

An If-Else-If Statement if ( nbr < 0 ){ cout << nbr << " is negative" << endl; } else if ( nbr > 0 ) { cout << nbr << " is positive" << endl; } else { cout << nbr << " is zero" << endl; }

A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

cout << "Enter simple expression: "; int Left; int Right; char Operator; cin >> Left >> Operator >> Right; cout << Left << " " << Operator << " " << Right << " = "; switch (Operator) { case '+' : cout << Left + Right << endl; break; case '-' : cout << Left - Right << endl; break; case '*' : cout << Left * Right << endl; break; case '/' : cout << Left / Right << endl; break; default: cout << "Illegal operation" << endl; }