KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Introduction to C Programming
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
2440: 211 Interactive Web Programming Expressions & Operators.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
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.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Week 2 Introduction to Computer Programming/ C Programming Language 1 EKT120: Computer Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
UniMAP Sem II-10/11EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm (Part 2)‏
Lecture 2. Outline Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and.
UniMAP Sem I-09/10EKT150: Computer Programming1 Week 2 – Introduction to Computer and Algorithm.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
Chapter 2: Basic Elements of C++
Week 1 – Introduction to Computer and Algorithm (Part 2)‏
BASIC ELEMENTS OF A COMPUTER PROGRAM
INSPIRING CREATIVE AND INNOVATIVE MINDS
Chapter 2 - Introduction to C Programming
ITEC113 Algorithms and Programming Techniques
Revision Lecture
Chapter 2 - Introduction to C Programming
By: Syed Shahrukh Haider
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
OPERATORS in C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
OPERATORS in C Programming
Presentation transcript:

KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2

KUKUM Sem1-05/06EKT120: Computer Programming2 Outline Identifiers and reserve words Program comments Preprocessor directives Data types and type declarations Operators Formatted input and output Program debugging

KUKUM Sem1-05/06EKT120: Computer Programming3 Operators Operators tell the computer how to process the data. It connects data in the expression and equation Types of operators used in calculation and problem solving include Mathematical Relational Logical

KUKUM Sem1-05/06EKT120: Computer Programming4 Operators continue…. Two concepts related to operator are Operand Resultant Operands are the data being process Resultant is the answer that result when the operation is complete. Example = 12 Operands ara 5 and 7 Operator is + Resultant is 12 Operands can be constant or variable Data types of resultant and operands will depend on the operator

KUKUM Sem1-05/06EKT120: Computer Programming5 Types of Operator Mathematical operators Addition (+) Subtraction (-) Multiplication (*) Division (/) Integer division (\) Modulo division (MOD) Powers (^) Functions

KUKUM Sem1-05/06EKT120: Computer Programming6 Types of Operator Relational operators Equal to Less than Greater than Less than or Equal to The resultant of relational operation is logical data type i.e. TRUE or FALSE The next set of actions will depend on the result of the relational expression E.g. Balance > 500, if the expression TRUE withdraw 100, if the expression FALSE withdraw 50.

KUKUM Sem1-05/06EKT120: Computer Programming7 Types of Operators Logical Operator OR AND Not Use to connect relational expression (decision-making expression) To perform operation on logical data

KUKUM Sem1-05/06EKT120: Computer Programming8 Example of Operator OperatorComputer SymbolExample MathematicalOperationResultant Addition Subtraction-7.5 – Multiplication*8.0 * Division/9.0/ Integer Division\9\42 Modulo divisionMOD9 MOD 41 Power^3^29 Relational Equal To=5=7FALSE Less Than<5<7TRUE Greater Than>5>7TRUE Less than or equal to<=5< = 7FALSE Greater than or equal to >=5>=7FALSE Not Equal to<>5<>7TRUE Logical NotNOTNOT TRUEFALSE AndANDTRUE AND TRUETRUE OrORTRUE OR FALSETRUE

KUKUM Sem1-05/06EKT120: Computer Programming9 Operator Hierarchy (Precedence) Order of OperationOperand Data TypeResultant Data Type ( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy 1. Functions Mathematical Operators 2. PowerNumerical 3. \, MODNumerical 4. *, /Numerical 5. +, -Numerical Relational Operators 6. =,, =, <>Numerical, string, character Logical Logical Operator 7. NOTLogical 8. ANDLogical 9. ORLogical

KUKUM Sem1-05/06EKT120: Computer Programming10 Expression & Equation Expression and Equation make up the instructions in the solution to a computer problem Expression processes data (the operand) through the operators. E.g. LENGTH * WIDTH Equation stores the resultant of an expression in a memory location in the computer through the (=) sign (assignment symbol) E.g. AREA = LENGTH * WIDTH The resultant of expression LENGTH * WIDTH will be stored in a location called AREA

KUKUM Sem1-05/06EKT120: Computer Programming11 Normal equation Appropriate Computer Equation X* (3*Y+4)-4*Y/(X+6) Setting up Numerical Expression X+6 X(3Y+4) - 4Y

KUKUM Sem1-05/06EKT120: Computer Programming12 Evaluating Mathematical Expression Consider 5*(X+Y)-4*Y/(Z+6) Value of X = 2, Y=3, Z=6 Evaliation 5 * (X + Y) – 4 * Y / (Z+6) OperationResultant 1. X+Y5 2. Z * resultant of * Y12 5. Resultant of 4 / resultant of Resultant of 3 – Resultant of 524

KUKUM Sem1-05/06EKT120: Computer Programming13 Evaluation Relational Expression Expression OperationResultant 1. A < BFALSE 2. C OR DTRUE 3. NOT the resultant of 1TRUE 4. Resultant of 3 AND Resultant of 2TRUE NOT (A < B) AND (C OR D) A = 4, B=2 C = TRUE D = FALSE

KUKUM Sem1-05/06EKT120: Computer Programming14 Getting Started With C //Program name : program1.c //Programmer : Salina //This program Print Welcom to KUKUM //Calculate A = B * K; //Print AnswerA #include int main(void) { float A, B; const float K=0.05; printf(“Welcome To KUKUM”); B = 400; A = B * K; printf(“\nValue of A : %5.2f”, A); return 0; } The terms void indicates we receive nothing from OS. int at the beginning return an integer to OS Variable & constant declaration begin end Return 0(int) to OS body Comments Preprocessor directives

KUKUM Sem1-05/06EKT120: Computer Programming15 Identifiers & Reserve Words Identifiers labels for program elements, such as variable and constant name case sensitive can consists of capital letters[A..Z], small letters[a..z], digit[0..9], and underscore character _ First character MUST be a letter or an underscore No blanks Reserve words cannot be used as identifiers Reserve words already assigned to a pre-defined meaning eg: delete, int, main, include, double, for, if, float etc. (Pleased refer text book) We cannot use the reserve word as our variable.

KUKUM Sem1-05/06EKT120: Computer Programming16 Program comments Starts with /* and terminate with */ OR Character // start a line comment, if several lines, each line must begin with // Comments cannot be nested /* /* */*/

KUKUM Sem1-05/06EKT120: Computer Programming17 Preprocessor directives An instruction to pre-processor Standard library header (p154,Deitel) E.g. #include for std input/output #include Conversion number-text vise-versa, memory allocation, random numbers #include string processing

KUKUM Sem1-05/06EKT120: Computer Programming18 Data Types & Mem. Alloc. Data TypeDescription Size (bytes) char A single character. Internally stored as a coded integer value (refer to ASCII table ). 1 int Integer quantity. Can be represented in signed or unsigned form (with the unsigned keyword). 4 float Floating-point number. Set of real numbers.4 double A more precise version of float. Has larger dynamic range and better representation of decimal points. 8 bool Boolean representation of logic states. Can only be assigned true (1) or false (0). 1

KUKUM Sem1-05/06EKT120: Computer Programming19 Type declarations Before we can use the variables in the program, we must declare them first float income; float net_income;  int index =0, count =0;  char ch=‘a’, ch2;  const float epf = 0.1, tax = 0.05; float income, net_income; Declare and initialize Named constant declared and initialized

KUKUM Sem1-05/06EKT120: Computer Programming20 Types of operators Types of operators are: Arithmetic operators (+, -, *, /, %) Relational operators (>, =, <=, !=) Logical operators (&&, ||) Compound assignment operator (+=, -=, *=, /=, %=) Binary operators: needs two operands Unary operators: single operand Bitwise operators: executes on bit level

KUKUM Sem1-05/06EKT120: Computer Programming21 Arithmetic Operators Used to execute mathematical equations The result is usually assigned to a data storage (instance/variable) using assignment operator ( = )

KUKUM Sem1-05/06EKT120: Computer Programming22 C Operators OperatorC Language SymbolExample MathematicalOperationResultant Addition Subtraction-7.5 – Multiplication*8.0 * Division/9.0/ Integer Division (Quotion\9\42 Modulo division%9 % 41 Power^3^29 Relational Equal To==5==7FALSE Less Than<5<7TRUE Greater Than>5>7TRUE Less than or equal to<=5< = 7FALSE Greater than or equal to>=5>=7FALSE Not Equal to!=5!=7TRUE Logical Not!NOT TRUEFALSE And&&TRUE AND TRUETRUE Or||TRUE OR FALSETRUE

KUKUM Sem1-05/06EKT120: Computer Programming23 Exercise on arithmetic operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder)

KUKUM Sem1-05/06EKT120: Computer Programming24 Relational and Logical Operators Previously, relational operator: >, =, <=, ==, != Previously, logical operator: &&, || Used to control the flow of a program Usually used as conditions in loops and branches

KUKUM Sem1-05/06EKT120: Computer Programming25 More on relational operators Relational operators use mathematical comparison (operation) on two data, but gives logical output e.g1 let say b = 8, if (b > 10) e.g2 while (b != 10) e.g3 if(kod == 1) print(“Pegawai”); Reminder: Don’t confuse == (relational op.) with = (assignment op.)

KUKUM Sem1-05/06EKT120: Computer Programming26 More on logical operators Logical operators are manipulation of logic e.g1 let say b=8, c=10, if ((b > 10) && (c<10)) e.g2 while ((b==8) ||(c > 10)) e.g3 if ((kod == 1) && (salary > 2213))

KUKUM Sem1-05/06EKT120: Computer Programming27 Truth table for && (logical AND) operator exp1exp2exp1 && exp2 false truefalse truefalse true

KUKUM Sem1-05/06EKT120: Computer Programming28 Truth table for || (logical OR) operator exp1exp2exp1 || exp2 false true falsetrue

KUKUM Sem1-05/06EKT120: Computer Programming29 Compound assignment operator To calculate value from expression and store it in variable, we use assignment operator (=) Compound assignment operator combine binary operator with assignment operator E.g. val +=one; is equivalent to val = val + one; E.g. count = count -1; is equivalent to count -=1; count--; --count;

KUKUM Sem1-05/06EKT120: Computer Programming30 Unary Operators Obviously operating on ONE operand Commonly used unary operators Increment/decrement { ++, -- } Arithmetic Negation { - } Logical Negation { ! } Usually using prefix notation Increment/decrement can be both a prefix and postfix

KUKUM Sem1-05/06EKT120: Computer Programming31 Unary Operators (Eg.) Increment/decrement { ++, -- } prefix:value incr/decr before used in expression postfix:value incr/decr after used in expression Logical Negation { ! } bool isDinnerTime = true; bool isLunchTime = !isDinnerTime; val=5; cout<<++val; Output: 6 val=5; cout<<--val; Output: 4 val=5; cout<<val++; Output: 5 val=5; cout<<val--; Output: 5

KUKUM Sem1-05/06EKT120: Computer Programming32 Operator Precedence & symbol in C language Order of OperationSymbol in C ( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy 1. Functions Mathematical Operators 2. Power^ 3.Integer division, modulo\, % 4. Multiplication, division*, / 5. Addition, substraction+, - 6. equal, less than, greater than, less than equal, greater than equal, not equal ==,, =, != Logical Operator 7. NOT! 8. AND&& 9. OR||

KUKUM Sem1-05/06EKT120: Computer Programming33 Formatted Output with printf To display result of the program can be done by using keyword printf and operator as shown below: printf(“formating”,variable) ; //this is variable declaration int a, b;//Line 1 char grade;//Line 2 //examples of input and output statements a = 25; grade = ‘A’;//Line3 printf(“a = ”,a) ; //Line 4 printf(“Enter two integers: ”) ;//Line 5 scanf (“%d%d”,&a,&b) ; printf(“The numbers you entered are %d %d”,a,b);//Line 6 printf(“Your grade is %c “,grade); //Line 10

KUKUM Sem1-05/06EKT120: Computer Programming34 Formatted Output with printf-cont

KUKUM Sem1-05/06EKT120: Computer Programming35 End Week 1 – Session 2 Q & A!