142 H -1 Conditionals Essential feature for a computer language Conditional Statement: allows the computer to choose between several paths depending on.

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

Introduction to C Programming
CSC 142 G 1 CSC 142 Conditionals [Reading: chapter 5]
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Python Programming Language
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
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
Computer Science 1620 Programming & Problem Solving.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
E-1 University of Washington Computer Programming I Lecture 6: Conditionals © 2000 UW CSE.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
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.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
CONDITIONAL STATEMENTS OVERVIEW.  Many times we want programs to make decisions  What drink should we dispense from the vending machine?  Should we.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
G-1 10/4/99 CSE / ENGR 142 Programming I Conditionals © 1999 UW CSE.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
31/01/ Selection If selection construct.
Decision Statements, Short- Circuit Evaluation, Errors.
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
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Conditionals. Conditional Execution A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
I F S TATEMENTS Tarik Booker CS 290 California State University, Los Angeles.
Chapter 3 Control Statements
Sequence, Selection, Iteration The IF Statement
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.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Control Structures – Selection
Topics The if Statement The if-else Statement Comparing Strings
Conditionals & Boolean Expressions
Selection CIS 40 – Introduction to Programming in Python
Data Types, Identifiers, and Expressions
Writing a program with conditionals
Summary Two basic concepts: variables and assignments Basic types:
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
The Java switch Statement
Life is Full of Alternatives
CprE 185: Intro to Problem Solving (using C)
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

142 H -1 Conditionals Essential feature for a computer language Conditional Statement: allows the computer to choose between several paths depending on the value of a variable or expression: e.g. if today is my birthday, add 1 to my age if the income is less than $30,000, take for the tax rate 10% else take 15% C Syntax: if (temperature > 98.6) printf("you have a fever.\n"); if ( condition ) statement; executed if and only if the condition is true

142H -2 Flow Chart In terms of a flow chart, if (x < 100) x = x+1; translates into x = x+1 True x< 100 False Writing a conditional expression: what follows the if Also called logical or boolean expressions Made of variables, constants, arithmetic expressions and relational operators > for greater than < for less than <= for less than or equal to != for not equal to == for equal to >= for greater than or equal to

142 H -3 Use boolean operators to write conditions && for AND || for OR ! for NOT Recall the logical table: F F T T T F F F T T T F P, Q are conditions T and F stand for True and False Examples temperature > 90.0 && humidity > 50.0 !(salary<30000 || exemptions<4) number_of_children==3 && age>35 Check the precedence order (inside cover of text) to be safe (and clear), use parentheses

142 H-4 Value of conditional expressions Recall that expressions have a value. What is the value of a conditional expression? Can think of it as TRUE or FALSE (however, there is no such type in C… but there is in C++) In Reality: it is an integer in C, FALSE is 0 TRUE is non zero (frequently 1, and always 1 if the result of the relational operator) Try these (but don’t use them!) if (0) printf(“0 is true”); if (!0) printf(“0 is false”); if (0.9) printf(“is printf executed?”);

Syntax of the if statement (1) 142 H-5 Examples: temperature = 99.5; if (temperature>98.6) printf(“You have a fever”); pulse = 80.; control flow temperature = 98.1; if (temperature>98.6) printf(“You have a fever”); pulse = 80.; control flow (printf is not executed)

if (temperature>98.6) { printf(“You have a fever”); aspirin = aspirin - 2; } A block can contain any number of statements any kind of statements Can have no statements in a block: if (x>100) {} /* OK */ Can also have more than 1 statement use a compound statement (also called “block”) 142 H-6 Syntax of the if statement (2) block

Some Examples (1) 142 H-7 _ An ATM program if (balance >= withdrawal) { balance = balance - withdrawal; dispense_funds(withdrawal); } function called to give the money to the ATM user. Can’t omit the parentheses (compilation error) What happens if we omit the braces {}? dispense_funds is executed even when balance is less than withdrawal !

/* one version */ if (x>=0) abs=x; if (x<0) abs=-x; /* another version */ abs = x; if (x<0) abs=-x; double abs(double x) { if (x<0) x=-x; return x; } As a function: local variable. Nothing is changed in main _ Computing an absolute value Some Examples (2) 142 H-8

142 H-9 if - else statement to compute the absolute value of x: if (x>=0) abs = x; else abs = -x; printf(“Absolute value of %.2f is %.2f”,x,abs); TrueFalse Control Flow: get here whether the condition is true or false x >= 0 abs = xabs = -x printf...

142 H-10 Formatting if statements Style: if (condition) { statement1; statement2;... } else { statement3; statement4;... } e.g. #define BILL_SIZE if (balance >= withdrawal) { balance = balance - withdrawal; dispense_funds(withdrawal); } else { if (balance >= BILL_SIZE) printf(“Try a smaller amount”); else printf(“Sorry, you’re really broke!”); }

142 H-11 if statement and { } (1) Consider: if (x==5) if (y==5) printf( “Both are 5\n” ); else printf( “x is 5, but y is not\n” ); else if (y==5) printf( “y is 5, but x is not\n” ); else printf( “Neither is 5\n” ); OK

if statement and { } (2) However: if (x==5) if (y==5) printf(“Both are 5\n”); else printf(“Is anybody 5?\n”); Which if is it associated to? if (x==5) { if (y==5) printf(“Both are 5\n”); } else printf(“Is anybody 5?\n”); Write 142 H-12 This one!

142 H-13 Matching elses with ifs Each else matches some if in the same block if { if if else { if if else else} else } else Within the same block, read the ifs and elses left to right, matching each else to the closest unmatched if Some ifs may not be matched to any else if if if else else if if else if code within { } unmatched