Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the.

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

Chapter 4 - Control Statements
The if-else Statements
CSC 142 G 1 CSC 142 Conditionals [Reading: chapter 5]
Logic & program control part 2: Simple selection structures.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Conditional Statements If these were easy then everyone would them!
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CPS120: Introduction to Computer Science Decision Making in Programs.
# 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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Structured Programming
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.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Programming Languages Programming languages are a compromise between spoken language and formal math. They allow humans to communicate with computers at.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
I F S TATEMENTS Tarik Booker CS 290 California State University, Los Angeles.
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Selections Java.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
IF statements.
Topics The if Statement The if-else Statement Comparing Strings
JavaScript: Control Statements.
Topics The if Statement The if-else Statement Comparing Strings
Types, Truth, and Expressions (Part 2)
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Summary Two basic concepts: variables and assignments Basic types:
CSC215 Lecture Control Flow.
Presentation transcript:

Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the necessary include –#include for math functions –#include for rand function

Examples of Function Calls rand –ranNum = 1 + rand()%99; –ranYearNinties = rand()%10; math –difference = abs(price-cost); –phi = (1 + sqrt(5))/2; –distance = sqrt(pow(x,2)+pow(y,2));

Conditional Expressions Relational Operators –> (greater than) –< (less than) –>= (greater than or equal to) –<= (less than or equal to) –== (equal to – notice the TWO equal signs) –!= (not equal to)

Conditional Expressions Allow comparison of two primitive types Examples –b*b >= 4*a*c –money < 1000 –score != 21 Always evaluate to 1 or 0 –1 represents true –0 represents false

Complex Boolean Expressions Boolean Operators –&& (and) –|| (or) –! (not) Used to put together conditional expressions Examples –(age >= 21) && (age <= 65) –(b != 0) && (a/b > 0)

And, Or, Not and (&&) –Only true if both clauses are true –IT IS INCORRECT TO DO SOMETHING LIKE 21 <= age <= 65. The proper expression is (21 <= age) && (age <= 65). or (||) –True as long as at least one clause is true –Checks conditions in order, from left to right. not (!) –Negates value of the original expression

Standard If Statement if ( ) stmt1; else stmt2; stmtA; stmtB; Evaluate the boolean expression If it is true, execute stmt1 If it is not, execute stmt2 Then, in both cases, proceed to stmtA and continue.

Block of Statements if ( ) { stmt11; stmt12;. stmt1n; } else { stmt21; stmt22;. stmt2m; } stmtA; stmtB; Same as before But if the boolean expression is true, statements 11 through 1n all get executed in order Otherwise, if it is false, statements 21 through 2m all get executed in order In all cases, one or the other set of statements get executed, never both, and never neither. Afterwards, stmtA is always executed, regardless of the value of the boolean expression

Else-If Clause if ( ) else if ( ) else if ( ) else stmtA Now, we check boolean expressions in order When we find the FIRST true one, we execute the statement corresponding to it. If none are true, we execute the statement in the else clause Then, we continue to stmtA.

Notes about if statement All else if and else clauses are optional No “else if” or “else” is possible without a preceding and matching “if”. A block of statements can be used for any clause, but is not necessary. They can be mixed with clauses that don’t have a block. Be very careful about forming boolean expressions. Their order and form matter.

Nested If Statements Definition: an if statement inside of ANY clause of an if statement. Matching else problem –if no curly braces {} are used, this can occur –two ifs, but only one else. –Rule – the else ALWAYS matches the inner most if

Matching-Else Problem Example if (month == 4) if (day > 15) printf("Your taxes are late.\n"); else printf("File your taxes by 4/15.\n"); If month is NOT 4 –Nothing gets printed If month is 4 and day is 15 or less –File your taxes by 4/15 is printed If month is 4 and day is 16 or more –Your taxes are late is printed

Forgetting {} if (month == 4) printf(“It is close to tax time.\n”); printf(“Finish by the 15th.\n”); else printf(“It is not tax month.\n”); This is a syntax error Finish by the 15th would always print, and no statement is allowed to start with else. Curly braces for the if fix it.

Forgetting {} if (month == 4) printf(“It is close to tax time.\n”); else printf(“It is not tax month.\n”); printf(“Find something else to do.\n”); This is perfectly valid. But, the statement, “Find something else to do” will always print no matter what.

Difference between 1 if and separate ifs With one if statement, only one clause of statements can be executed With several separate if statements, one after another, there is the potential for multiple if statements to be executed

Example of separate ifs if (grade >= 90) –printf(“A\n”); if (grade >= 80) –printf(“B\n”); if (grade >= 70) –printf(“C\n”); if (grade >= 80) –printf(“D\n”); Else –printf(“F\n”);

One Last Note Order of Operations of && is greater than || Easiest to just use extra parentheses.