Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators



Similar documents
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Selection Statements

arrays C Programming Language - Arrays

Lecture 2 Notes: Flow of Control

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Boolean Expressions & the if Statement

Chapter 5. Selection 5-1

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Two-way selection. Branching and Looping

Conditions & Boolean Expressions

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Sources: On the Web: Slides will be available on:

Informatica e Sistemi in Tempo Reale

Conditionals (with solutions)

Example of a Java program

I PUC - Computer Science. Practical s Syllabus. Contents

JavaScript: Control Statements I

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Conditional Statements Summer 2010 Margaret Reid-Miller

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

Object Oriented Software Design

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

=

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Chapter 8 Selection 8-1

Chapter 2: Elements of Java

TIP: To access the WinRunner help system at any time, press the F1 key.

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Statements and Control Flow

if and if-else: Part 1

Stacks. Linear data structures

General Software Development Standards and Guidelines Version 3.5

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Tutorial on C Language Programming

COMP 110 Prasun Dewan 1

Chapter 3 Operators and Control Flow

While Loop. 6. Iteration

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Binary Adders: Half Adders and Full Adders

Python Evaluation Rules

Fundamentals of Programming

6. Control Structures

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Semantic Analysis: Types and Type Checking

Tom wants to find two real numbers, a and b, that have a sum of 10 and have a product of 10. He makes this table.

6.S096 Lecture 1 Introduction to C

Lecture 4: Writing shell scripts

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

The C Programming Language course syllabus associate level

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

Object Oriented Software Design

Lecture Notes on Linear Search

(Refer Slide Time: 00:01:16 min)

CS 241 Data Organization Coding Standards

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

1. Boolean Data Type & Expressions Outline. Basic Data Types. Numeric int float Non-numeric char

Python Programming: An Introduction To Computer Science

Compilers I - Chapter 4: Generating Better Code

Microsoft Excel Tips & Tricks

Boolean Data Outline

Topic 11 Scanner object, conditional execution

Chapter One Introduction to Programming

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

ChE-1800 H-2: Flowchart Diagrams (last updated January 13, 2013)

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Excel: Introduction to Formulas

Computer Programming I

Exercises for Design of Test Cases

J a v a Quiz (Unit 3, Test 0 Practice)

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Course Title: Software Development

Decision Logic: if, if else, switch, Boolean conditions and variables

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

Writing Control Structures

Grade 6 Math Circles. Binary and Beyond

COS 217: Introduction to Programming Systems

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Let s put together a Manual Processor

PL / SQL Basics. Chapter 3

Creating Basic Excel Formulas

So far we have considered only numeric processing, i.e. processing of numeric data represented

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Lecture 1 Introduction to Java

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Parallel and Distributed Computing Programming Assignment 1

5 Arrays and Pointers

Problem Solving Basics and Computer Programming

Exercise 4 Learning Python language fundamentals

Data Structure with C

Transcription:

Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero THEN the Average is Sum divided by Count Conditions specified using logical data Outline II. Program Basics G. s 3. Binary operators relational operators ==!= < > <= >= logical operators &&! 7. Logical data H. Statements 3. If 4. If-Else matching 5. Switch default break Logical Data in C no explicit logical type int and char values can be treated as logical values two possible logical values 0 or \0 - anything - specific operators produce logical values Logical s Relational operators: operators to compare two values syntax: expr1 relop expr2 expressions must be of same type (any) Logical operators: operators to logically combine logical values produce complex combinations of conditions operators apply to logical values Relational Operators Relational Examples Operator X == Y X!= Y X < Y X > Y X <= Y X >= Y Meaning X equal to Y X not equal to Y X less than Y X greater than Y X less than or equal to Y X greater than or equal to Y A: 4 B: 3 C: 2.5 A < B 0 () A < B 1 () When chars compared, ASCII codes compared A!= a 1 () Note, expressions must be of same type But, implicit conversions will be done B > C 1 (), B converted to float 1

Combined s Complex combinations of operations can be used: 4 < 3 + 2 4 < 5 1 () Why isn t expression evaluated as (4 < 3) + 2? Relational operators have lower precedence than arithmetic operators, higher than assignment Have left-associativity Operator Precedence > < >= <= 10 =!= 9 Complex Example 10 % 4 * 3-8 <= 18 + 30 / 4-20 2 * 3-8 <= 18 + 30 / 4-20 6-8 <= 18 + 30 / 4-20 6-8 <= 18 + 7-20 -2 <= 18 + 7-20 -2 <= 25-20 -2 <= 5 1 () Logical Operators Simple (relational) logical expressions or depending on values compared Compound logical expressions built out of logical combinations of simple expressions Examples: I am standing in a lecture hall AND the moon is made of green cheese is, but I am standing in a lecture hall OR the moon is made of green cheese is Logical Syntax: LogExpr1 LogicalOp LogExpr2 (&& ) LogicalOp LogExpr (!) (2 < 3) && (17!= 29) /* && - AND */ 1 && 1 /* AND */ 1 /* */ AND (&&) and OR ( ) AND (&&) of two logical expressions: resulting compound expression when both expressions OR ( ) of two logical expressions: expression when either expression Truth table: E1 E2 E1 && E2 E1 E2 1 () 1 () 1 () 1 () 1 () 0 () 0 () 1 () 0 () 1 () 0 () 1 () 0 () 0 () 0 () 0 () Logical Examples month is 2, year is 1999 (month == 2) && ((year % 4) == 0) 1 () && ((year % 4) == 0) 1 && ( 3 == 0) 1 && 0 () 0 () day is 6 (day == 0) (day == 6) 0 () (day == 6) 0 1 () 1 () 2

Short-Circuit Aspect && and are short-circuit operators - if the first operand determines the value, the second operand is not evaluated first operand of && is 0, result is 0 without evaluating second operand first operand of is 1, result is 1 without evaluating second operand second operand evaluated when needed Negation Arithmetic negation: - ( 4 * A * C ) Operator minus (-) negates numeric value Logical negation Logical operator (!) negates logical value E! E 1 () 0 () 0 () 1 () Example:! (3 > 4)! 0 () 1 () Complex s s can be built using arithmetic, relational and logical operators!((-5 >= -6.2) (7!= 3) && (6 == (3 + 3))) operator precedence, associativity + - (unary)! & * 15 right (typename) 14 right * / % 13 left + - (binary) 12 left < > <= >= 10 left ==!= 9 left && 5 left 4 left assignments 2 right Example!((-5 >= -6.2) (7!= 3) && (6 == (3 + 3)))!( 1 () (7!= 3) && (6 == (3 + 3)))!( 1 1 () && (6 == (3 + 3)))!( 1 1 && (6 == 6 ))!( 1 1 && 1 () )!( 1 1 () )! 1 () 0 () Decision Making Problem: input two numbers and store the largest in Big Num1: 17 Num2: 53 Big: 53 Algorithm 1. Input Num1 and Num2 2. Store largest of Num1 and Num2 in Big 2.1 Store Num1 in Big 2.2 IF Num2 > Num1 THEN store Num2 in Big IF Statement Syntax: if (LogicalExpr) Statement Program context: 1; if (LogicalExpr) 2; 3; Order of execution: LogicalExpr 1 () 0 () 1 1 2 3 3 3

Flow of Execution Flow Chart for our Problem 1 input Num1 input Num2 Logical 2 Store Num1 in Big Num2 > Num1 Store Num2 in Big 3 Code for Solution int findmax(int Num1, int Num2) { int Big; Big = Num1; if (Num2 > Num1) Big = Num2; return Big; Trace of Solution Trace of findmax(17,53): Num1 Num2 Big 17 53? Big = Num1; 17 if (Num2 > Num1) Big = Num2; 53 return Big; Trace of Solution Trace of findmax(9,6): Executing > 1 Statement in an If What if you want to execute > 1 stmt in If? Num1 Num2 Big 9 6? Big = Num1; 9 if (Num2 > Num1) return Big; 1 Logical 2 3 4 5 4

Does this work? Statement1; If (LogicalExpr) Statement2; Statement3; Statement4; Statement5; > 1 Stmt in an If No, the indenting is irrelevant, section P is: If (LogicalExpr) Statement2; Statement3; Statement4; P > 1 Stmt in an If (A solution) Solution - use a compound : Statement1; If (LogicalExpr) { Statement2; Statement3; Statement4; Statement5; Statement in If Any reasonable as the single in if expression assignment function call (printf, scanf, etc.) compound if Two-Way Selection Sometimes desirable for to be executed when Logical 3 1 Logical 2 4 Two-Way Selection with If Possible to use combinations of if: Statement1; if (LogicalExpr) Statement2; if (!(LogicalExpr)) Statement3; Statement4; But not efficient 1 Logical! Logical 4 2 3 Syntax: if (LogicalExpr) StatementA StatementB B If-Else Statement Logical A 5

FindMax with If-Else int findmax(int Num1, int Num2) { int Big; Big = Num1; /* 1 assign + */ if (Num2 > Num1) Big = Num2; /* 1 test + */ return Big; /* maybe 1 assign */ /* test plus 1 or 2 assigns */ int findmax(int Num1, int Num2) { int Big; if (Num2 > Num1) /* 1 test + */ Big = Num2; /* 1 assign or */ Big = Num1; /* 1 assign */ return Big; /* test plus 1 assign */ Using If-Else for Robustness float calcaverage(float sum, int count) { if (count == 0) return 0.0; return sum / count; Note return for each condition Compound Statements and If-Else if ((year % 4) == 0) { printf( Leap year\n ); numdays = 366; { printf( Not a leap year\n ); numdays = 365; Programming Tip: Compound Stmt Does not hurt to always use compound s for if, if- s if (LogicalExpr) { /* or s */ if (LogicalExpr) { /* (s) */ { /* (s) */ Easy to add s later Programming Tip: Indenting Use indenting to make code more clear indent s in function definition to clearly identify body of function indent (s) executed for if by fixed amount (2,3 chars) every time for ifs within an if indent further indent s(s) similarly may want to make {, match (on separate lines) Programming Tip: Conditions Code most likely conditions first Code positive conditions where possible Code parts of solution executed most often first 6

Multiway Selection Multiway Flow Chart Multiway if more than 2 alternatives Example: Student Score Message 0-55 Failing 56-65 Unsatisfactory 66-100 Satisfactory If-Else has two alternatives, to do multiway, string together If-Else s Score <= 55 Score <= 65 Satisfactory Failing Unsatisfactory Multiway with If-Else if (score <= 55) if (score <= 65) Indenting If-Else Combinations Multiway if- s sometimes indented: if (score <= 55) if (score <= 65) Rule for : matches most recent if Conditions Checked in Reverse Ordering Conditions Score > 65 Score > 55 Failing Satisfactory Unsatisfactory if (score > 65) if (score > 55) But must check conditions in correct order if (score > 55) if (score > 65) Score of 70 would produce Unsatisfactory 7

Multiway with If Statements Possible but inefficient: if (score <= 55) if ((score > 55) && (score <= 65)) if (score > 65) Program Robustness Example assumes score in interval [0,100] but doesn t check Add test: if ((score >= 0) && (score <= 100)) /* print message */ printf( Bad score: %d\n,score); Completed Code Nesting Example if ((score >= 0) && (score <= 100)) { if (score <= 55) if (score <= 65) printf( Bad score: %d\n,score); if (A > 0) if ((A % 2) == 0) S1 = S1 + A; S2 = S2 + A; if (A == 0) printf( A zero\n ); NS = NS + A; printf( All done.\n ); A == 0 NS = NS + A printf A > 0 (A % 2) == 0 S2 = S2 + A printf("all done") S1 = S1 + A Matching Else Example Matching Single Else X < 0 Easy to make mistake if (X < 0) Increment NegCount Y < 0 Increment PosCount if (Y < 0) PosCount++; NegCount++; Despite indenting, matches wrong if PosCount updated when X < 0, Y >= 0 8

Matching Single Else Solutions Choosing Conditions if (X < 0) if (Y < 0) PosCount++; ; /* empty */ NegCount++; if (X < 0) { if (Y < 0) PosCount++; /* compound stmt */ NegCount++; if (X >= 0) NegCount++; if (Y < 0) PosCount++; Selection Example C Statement One way X > 0 If Two way X > 0 X <= 0 Multiway 0 <= X <= 10 11 <= X <= 20 21 <= X <= 30 If-Else Nested If-Elses Another Multiway Method Consider another grading problem: Score Grade 9-10 A 7-8 B 5-6 C 0-4 F if ((score == 9) (score == 10)) grade = A ; if ((score == 7) (score == 8)) grade = B ; if ((score == 5) (score == 6)) grade = C ; grade = F ; Switch Statement More readable approach: switch switch (score) { case 9: case 10: grade = A ; case 7: case 8: grade = B ; case 5: case 6: grade = C ; case 0: case 1: case 2: case3: case 4: grade = F ; Switch Format switch () { case const1-1: case const1-2:...... case const2-1: case const2-2:...... default:...... Switch Rules must be integral type (no float) Case labels must be constant values No two case labels with same value Two case labels can be associated with same set of s Default label is not required At most one default label 9

Switch Rules Executing Switch Example Evaluating determine value of expression look for corresponding label if no corresponding label look for default if no corresponding label or default, do nothing execute all s from label to ALL s from label to??? switch (score) { case 9: case 10: grade = A ; case 7: case 8: grade = B ; case 5: case 6: grade = C ; score is 9: grade = A grade = B grade = C score is 7: grade = B grade = C score is 5: grade = C not quite what we want The break Statement break used to indicate a set of s is finished (and no more should be executed) syntax: break says to stop executing and go to the next (skipping any s in between) add after each set of cases default case The default case can be used to deal with robustness issues (score is < 0 or > 10) switch (score) { case 9: case 10: grade = A ; case 7: case 8: grade = B ; case 5: case 6: grade = C ; case 0: case 1: case 2: case3: case 4: grade = F ; default: printf( Bad score %d\n,score); Other Types Any integral type can be used as expression Cases must match char married; switch (married) { case S : case s : printf( single ); case D : case d : printf( divorced ); case M : case m : printf( married ); When Not to Use Switch Example: Case 0 <= X <= 100 switch (x) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 100: Better to use nested ifs 10