1 / 24

Functions, Logical Operators, and If Statements

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.

blee
Télécharger la présentation

Functions, Logical Operators, and If Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture4

  2. Lecture Outline • Functions • Logical Operators • If statement • Compound if Statement • Exercise • Debugging • Exercise

  3. Functions • A function is a subprogram that performs a specific task. • Functions you know: • printf("Hi"); • scanf("%i",&number);

  4. 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

  5. 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

  6. 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

  7. 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)

  8. 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); }

  9. 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

  10. 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

  11. 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

  12. The ! Operator (not) Operand1 Operand2 nonzero(true) 0(false) 0(false) 1(true) Logical Operators

  13. Operator Precedence Operator Precedence function calls highest ! + - & (unary operators) * / % + - < <= >= > == != && || = lowest

  14. 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)

  15. 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

  16. 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

  17. 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!");

  18. 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

  19. 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");}

  20. Syntax if (condition) {statements} if (condition) {statements} else {statements} Compound if Statement

  21. 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

  22. 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.

  23. 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);}

  24. 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.

More Related