Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture4.

Similar presentations


Presentation on theme: "Lecture4."— Presentation transcript:

1 Lecture4

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

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

4 Pre-defined and User-defined Functions
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.

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

6 Pre-defined Functions
Function call in an Assignment Statement Syntax Y = function name(argument); Example Y = sqrt(x); function call x y square root computation function sqrt

7 Pre-defined Functions
Examples x = -5 y = abs(x) y = 5 x = 90 y = abs(x) + 2 y = 92 x = 10 y = sqrt(x + 6) y = 4 x = 2.25 y = sqrt(x) y = 1.25

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 Relational and Equality Operators
== equal to != not equal to Relational < less than > greater than <= less than or equal to >= grater than or equal to

10 Logical Operators The && Operator (and)
Operand1 Operand2 Operand1 && Operand 2 nonzero(true) nonzero(true) 1(true) nonzero(true) 0(false) (false) 0(false) nonzero(true) 0(false) 0(false) 0(false) (false)

11 Logical Operators The || Operator (or)
Operand1 Operand2 Operand1 && Operand 2 nonzero(true) nonzero(true) 1(true) nonzero(true) 0(false) (true) 0(false) nonzero(true) 1(true) 0(false) 0(false) 0(false)

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

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

14 Examples on Relational, Equality, and Logical Operators
x = y= z=2 Expressions Evaluation Value x==1 || x== || (True) (x==1) || (x==3) || 1 1 (True) !(z>=2) !(1) (False) !!(y==4) !!(1) !(0) (True) (z>=2) && (x<10) && 1 1 (True) (y!=4) (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 single man whose 55 years old or older C language: status=='s'&&gender=='m' &&age>=55

16 if Statement Syntax if (condition) statement;
if (condition) statement; else statement; Examples if (num_stud==25) printf("The class is full"); else printf("The class is not full"); if (score>=90) grade='A';

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 Compound if Statement Syntax if (condition) {statements}
if (condition) {statements} else {statements}

21 Comparing if Statement Syntax
Simple if statement if (condition) statement; if (condition) statement; else statement; Compound if statement if (condition) {statements} if (condition) {statements} else statements}

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.


Download ppt "Lecture4."

Similar presentations


Ads by Google