Conditionals. Conditional Execution A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
true (any other value but zero) false (zero) expression Statement 2
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Program Control Dilshad M. Shahid New York
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
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.
The If/Else Statement, Boolean Flags, and Menus Page 180
Visual C++ Programming: Concepts and Projects
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
E-1 University of Washington Computer Programming I Lecture 6: Conditionals © 2000 UW CSE.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
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.
G-1 10/4/99 CSE / ENGR 142 Programming I Conditionals © 1999 UW CSE.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
1 if / else statements. 2 Conditionals “If you eat your vegetables, then you can have dessert.” “If you do your homework, then you may go outside to play,
ICT Introduction to Programming Chapter 4 – Control Structures I.
142 H -1 Conditionals Essential feature for a computer language Conditional Statement: allows the computer to choose between several paths depending on.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Control statements Mostafa Abdallah
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Decision Making and Branching
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
CSE202: Lecture 5The Ohio State University1 Selection Structures.
ECE 122 Feb. 8, if Statement A condition is an expression that can be either true, or false. “if” statement allows a program to make a decision.
Introduction to Programming Using C Basic Logic. 2 Contents Conditional programming If statement Relational operators Compound statements Loops While.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Rational Expressions relational operators logical operators order of precedence.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Data Types and Expressions
Selections Java.
Factoring if/else code
Conditions and Boolean Expressions
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
EPSII 59:006 Spring 2004.
Relational and Logical Operators
CprE 185: Intro to Problem Solving (using C)
Relational and Logical Operators
Lecture 9: Implementing Complex Logic
Presentation transcript:

Conditionals

Conditional Execution A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression – if the withdrawal is more than the bank balance, then print an error –if today is my birthday, then add one to my age –if my grade is greater than 3.5, then give ‘A’ as final grade

Conditional statements in C C supports several forms for conditionals: –if –if-else –if-elsif-elsif-…-else –And more… You as a programmer must decide which form to use. General semantics: the conditional statement depends on a boolean condition, the guard; if this condition is true the statement that depends on it will get executed;

Conditional Statement in C if (condition) statement; The statement is executed if and only if the condition is true. if (x < 100) x = x + 1; if (withdrawalAmount > balance) printf( “NSF check.\n”); if (temperature > 98.6) printf(“You have a fever.\n”);

Conditional statements in C if (condition) statement1; else statement2; Semantics: if condition is true, statement1 gets executed, otherwise statement2 gets executed. if (value >= 70) passed = passed + 1; else notPassed = notPassed + 1;

Conditional statements in C if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; : : else if (conditionK) statementK; else statement; The first condition that is true (from top to bottom) will make its statement execute. If all conditions are false last statement executes.

Conditional Example if (grade >= 90) countAs = countsAs + 1; else if (grade >= 80) countBs = countBs + 1; else if (grade >= 70) countCs = countCs + 1; else if (grade >= 60) countDs = countDs + 1; else countFs = countFs + 1;

Flow of execution x = y + 10; (s1) if ( x < 100) x = x + 1; (s2) y = y – 10; (s3) S2 depend on the condition (X < 10) S1 and S3 do not depend on the condition (x < 10) x<100 Executes s1 true false executes s2 Does not execute s2 Executes s3

Conditional expressions Also called "logical" or "Boolean" expressions Made up of variables, constants, arithmetic expressions, and the "relational operators": Math symbols:,,=, in C:, >=, ==, != Conditional expression examples: airTemperature > <= body_temperature maritalStatus == ‘M’ divisor != 0

Value of conditional expressions In C conditional expressions are evaluated to yield an integer. –If the resulting integer value is 1, it is equivalent to TRUE. –If the resulting integer value is 0 it is equivalent to FALSE. An arithmetic relational operator (==, >=, !=) will yield 1 when the relation is true and 0 when false. Any integer expression can be used as a conditional expression: –If its value is not zero, it is true –Otherwise it its false. Thus we can write: if (value) equivalent to: if (value !=0)statement;

Formation of complex conditional expressions Boolean operators && stands for the and || stands for the or ! Stands for the negation operator Not Examples (x >= 0) && (x <=10) (y 10) !(x == 0) ! ( (x>=0) && (x <=10))

Boolean Tables for &&, ||, ! Be1 && be2 be1 || be !0 yields 1 !1 yields 0

DeMorgan’s Laws ! ( be1 && be2) == (!be1) || (!2be) ! ( be1 || be2) == (!be1) && (!2be)

Precedence rules of expressions with boolean operators Boolean ! Has higher precedence than && && has higher precedence than || &&, and || are left associative. !x && y equivalent to (!x) && y y && z || u equivalent to (y && z) || u z1 || z2 || z3 equivalent to (z1|| z2) || z3 z1 && z2 && z3 equivalent to (z1&& z2) && z3 ! Has higher precedence than, = &&, || have lower precedence than, = !x < y equivalent to (!x) < y X && y < z equivalent to x && (y < z)

Warning Notice that when we write: if (be) statement1; statement2; the boolean expression is only affecting statement1. Statement2 will always execute after the if is executed. What if we need to perform more than one statement based on a condition?

Blocks Use a block to form a group of statements, which will groups together statements so that they are treated as a single statement: { statement1; statement2; …. } With this notion we can write more complex conditional statements.

Example If (withdrawal <= balance){ balance = balance - withdrawal; monthyCharge = monthlyCharge + 1.0; } Else { monthlyPenalty = monthlyPenalty ; } To note: A single statement ends with semicolon A block does not have semicolon after ‘}’ A block statement can have zero or more statements Its best to use block statements in conditionals, it helps you reduces the number of errors

Pitfalls with if’s Using ‘=‘ instead of ‘==‘ It will not yield a compilation error And accompaning statement might or might not execute. In any case, it is a BUG hard to find!!!!!!!!!!! if ( value = average) statement ;

Pitfalls with if’s Althought the following statement will not give a syntactic error it is incorrect if ( low <= value <= high) value = value * 2; You should write it as: if (low <= value && value <= high) value = value * 2;

Pitfalls with if’s the operator & is different from && the operator | is different from || & and | are not boolean operators!!!!! if used by mistake, you will get no syntax errors, but program will most likely produce incorrect results.

Pitfalls with if’s When using doubles do not compare them for equality (==) or inequality (!=) When using either of these two operators with doubles you might get incorrect results due to accuracy of doubles! 30.0* (1.0/3.0) == 10.0 May yield false! 1 + ½. + 1/3. + ¼. +…..+ 1/n. Will most likely not be equal to 1/n. + 1/(n.-1) + … + ½. + 1 for a large value of n.