Lecture4.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Representation of Data Types Comparing double and int data types Using integers are faster and more precise round-off errors when using doubles The range.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Problems Reads a letter grade from user and displays the corresponding range for that letter grade (A  , B  80-89, C  70-79, D  60-69, otherwise.
Chapter#3 Part1 Structured Program Development in C++
Recap……Last Time [Variables, Data Types and Constants]
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture2.
Decision making If.. else statement.
Arithmetic Expressions
CSE101-Lec#5-6 Operators.
Computer Science 210 Computer Organization
CSE 220 – C Programming C Fundamentals.
Function Topic 4.
INC 161 , CPE 100 Computer Programming
INSPIRING CREATIVE AND INNOVATIVE MINDS
presented BY : DURGESH KKHANDEKAR 1st semester
Chapter 2 - Introduction to C Programming
Sequence, Selection, Iteration The IF Statement
TMF1414 Introduction to Programming
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Relational Operations
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
INC 161 , CPE 100 Computer Programming
FUNCTIONS EXAMPLES.
Chapter 2 - Introduction to C Programming
Functions in C Mrs. Chitra M. Gaikwad.
Introduction to C Programming
Formatted and Unformatted Input/Output Functions
Introduction to Programming
ICS 3U Tuesday, September 21st.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Functions Chapter 3 of the text Motivation:
Lec 7.
Lec 5.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Lec8.
Decision making If statement.
Chapter 2 - Introduction to C Programming
Lecture3.
Control Structures Lecture 6.
Introduction to Problem Solving and Programming
Chapter 2 - Introduction to C Programming
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Lecture 9: Implementing Complex Logic
CPS125.
Presentation transcript:

Lecture4

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

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

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.

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.

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

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

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

Relational and Equality Operators == equal to != not equal to Relational < less than > greater than <= less than or equal to >= grater than or equal to

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

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 single man whose 55 years old or older C language: status=='s'&&gender=='m' &&age>=55

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';

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

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

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}

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.