170 likes | 301 Views
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 <math.h> for math functions #include <stdlib.h> for rand function. Examples of Function Calls. rand
E N D
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 <math.h> for math functions • #include <stdlib.h> for rand function
Examples of Function Calls • rand • ranNum = 1 + rand()%99; • ranYearNinties = 1990 + 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
if (<boolean expr>) 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. Standard If Statement
if (<boolean expression>) { 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 Block of Statements
if (<boolean expr1>) <stmts1> else if (<boolean expr2>) <stmts2> else if (<boolean expr3>) <stmts3> else <stmtsn> 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. Else-If Clause
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.