240 likes | 251 Views
Learn about functions, logical operators, and if statements in C programming. Understand how to define and call pre-defined and user-defined functions, use arithmetic functions, and evaluate expressions with relational, equality, and logical operators.
E N D
Lecture Outline • Functions • Logical Operators • If statement • Compound if Statement • Exercise • Debugging • Exercise
Functions • A function is a subprogram that performs a specific task. • Functions you know: • printf("Hi"); • scanf("%i",&number);
Pre-defined Function • Is a function that is already defined in one of the many C libraries. • It is included in the program through the preprocessor directive • statement #include< > and used in the program to perform a • specific task. • Ex: #include<stdio.h> Defines printf & scanf functions User-defined Function • Is a function that is created by the user. • It is defined before the main program through a function prototype • and called upon in the main program to perform a specific task. Pre-defined and User-defined Functions
Arithmetic Functions • Are functions that perform arithmetic operations. • They are usually located in the math.h or stdlib.h libraries. Examples • abs(x) is a function that returns the absolute value of its integer • argument. • sqrt(x) is a function that returns the square root of x. Pre-defined Functions
Syntax Y = function name(argument); Example Y = sqrt(x); function call function sqrt square root computation x y Pre-defined Functions Function call in an Assignment Statement
y = 5 y = 92 y = 4 y = 1.25 Pre-defined Functions Examples • x = -5 • y = abs(x) • x = 90 • y = abs(x) + 2 • x = 10 • y = sqrt(x + 6) • x = 2.25 • y = sqrt(x)
Square Root Program /* Performs a square root computation */ #include <stdio.h> /* definitions of printf, scanf */ #include <math.h> /* definition of sqrt */ int main(void) { float number; /* input/output variable */ /* Get a number and display its root. */ printf("Enter a number: "); scanf("%f", &number); number = sqrt(number); printf("The square root of this number is %.2f\n", number); return(0); }
Equality == equal to != not equal to Relational < less than > greater than <= less than or equal to >= grater than or equal to Relational and Equality Operators
The && Operator (and) Operand1 Operand2 Operand1 && Operand 2 nonzero(true) nonzero(true) 1(true) nonzero(true) 0(false) 0(false) 0(false) nonzero(true) 0(false) 0(false) 0(false) 0(false) Logical Operators
The || Operator (or) Operand1 Operand2 Operand1 && Operand 2 nonzero(true) nonzero(true) 1(true) nonzero(true) 0(false) 1(true) 0(false) nonzero(true) 1(true) 0(false) 0(false) 0(false) Logical Operators
The ! Operator (not) Operand1 Operand2 nonzero(true) 0(false) 0(false) 1(true) Logical Operators
Operator Precedence Operator Precedence function calls highest ! + - & (unary operators) * / % + - < <= >= > == != && || = lowest
Examples on Relational, Equality, and Logical Operators x = 3 y=4 z=2 Expressions Evaluation Value x==1 || x==3 0 || 1 1 (True) (x==1) || (x==3) 0 || 1 1 (True) !(z>=2) !(1) 0 (False) !!(y==4) !!(1) !(0) 1(True) (z>=2) && (x<10) 1 && 1 1 (True) (y!=4) 0 0(False)
Examples • English language: x less than or equal to 0 • C language: x<=0 • English language: grade not equal to 'A' • C language: grade != 'A' • English language: an exam score of 90 and above or quiz • score of 95 and above • C language: exam>=90 ||quiz>=95 • English language: a singleman whose 55 years old or older • C language: status=='s'&&gender=='m' &&age>=55
Syntax if (condition) statement; if (condition) statement; else statement; Examples if (score>=90) grade='A'; if (num_stud==25) printf("The class is full"); else printf("The class is not full"); if Statement
More Examples • if (choice==1) printf("Your choice is 1"); • if (score>=90 && attendance==95) grade='A'; • if (day==29 || day==30) printf ("Happy Eid"); • if (mood=='g') • printf("Have a good day!"); • else • printf("Have a bad day!");
Exercise 1 Write a program that accepts a type of fruit from the user and displays the market the user should go to based on the following: apples Azizia all other fruits Fruit Market
Exercise 1 #include<stdio.h> int main(void) {char fruit; printf("Enter a fruit: "); scanf("%c", &fruit); if (fruit=='a') printf("Go to Azizia!\n"); else printf ("Go to the fruit market!\n");}
Syntax if (condition) {statements} if (condition) {statements} else {statements} Compound if Statement
Simple if statement if (condition) statement; if (condition) statement; else statement; Compound if statement if (condition) {statements} if (condition) {statements} else statements} Comparing if Statement Syntax
Exercise 1 Write a program that calculates the discount, if any, on a sale. Sales of $100 and over are eligible for a 10% discount. The program should ask the user what the amount of their purchase is and calculates and displays the discount, if there is any, or else it will display a message stating that there is no discount.
Exercise 1 #include<stdio.h> int main(void) {float price; printf("Enter the price: "); scanf("%f", &price); if (price>=100) { price=price*0.9; printf("The price after the discount is: %.2f\n", price); } else printf("There is no discount\n"); return(0);}
Debugging • Debugging is the process of finding errors in a program. • Diagnostic printfs are statements placed in areas of the program • where a programmer ‘thinks’ she made a mistake. It can be used • to view the variable contents at any time.