Computer programming chapter ( 4 )

24
Prepaid By: Eng. Ibrahim Elewah Higher Technological Institute 10 th of Ramadan City 6 th of October Branch Electrical and Computer Engineering Department Main Reference HTI Student Book and “C For Dummies” by Dan Gookin 2 nd Edition Lecture Notes in 1

Transcript of Computer programming chapter ( 4 )

Page 1: Computer programming chapter ( 4 )

LOGO

Prepaid By: Eng. Ibrahim Elewah

Higher Technological Institute 10th of Ramadan City6th of October Branch

Electrical and Computer Engineering Department

Main Reference

HTI Student Book and “C For Dummies”

by Dan Gookin 2nd Edition

Lecture Notes in

1

Page 2: Computer programming chapter ( 4 )

Course Contents

Introduction

C Programming Higher Technological Institute2

1

Program Development2

The Essential of C Programs3

Manipulating Data with Operators 4

Reading from and Writing to Standard I/O5

Decision6

Iteration7

Arrays8

C Functions9

Page 3: Computer programming chapter ( 4 )

Course Contents

C Programming Higher Technological Institute3

Manipulating Data with Operators 4

Page 4: Computer programming chapter ( 4 )

Course Contents

C Programming Higher Technological Institute4

Errors Types

Arithmetic Assignment Operators

Unary Minus Operator

The Cast Operator

Manipulating Data with Operators 4

Page 5: Computer programming chapter ( 4 )

Error Types

C Programming Higher Technological Institute5

Syntax

Execution

Logic

Page 6: Computer programming chapter ( 4 )

Error Types

They are errors that youhave made in the form

Or syntax of the language

o Spelling a keyword incorrectly

o Errors that are detectedduring running of a programare shown in a dialog box thatappears and the error ishighlighted in the program.

C Programming Higher Technological Institute6

Syntax

Example

Page 7: Computer programming chapter ( 4 )

Error Types

If the program has no syntax errors. The computer can execute it.

During execution, execution errors may be detected.

o If a number is divided by zerocauses an execution error.

Whenever an execution erroris detected, the computer displays adialog box with an error messageand stops executing the program.

C Programming Higher Technological Institute7

Execution

Example

Page 8: Computer programming chapter ( 4 )

Error Types

If the output of the programdoes not agree with what isexpected, this is logic error.

The computer cannot detectsuch an error because it does notknow the logic of the programshould be.

So, it is your responsibility todetect logic errors in the program.

C Programming Higher Technological Institute8

Logic

Page 9: Computer programming chapter ( 4 )

Error Types

C Programming Higher Technological Institute9

Syntax

Execution

Logic

Page 10: Computer programming chapter ( 4 )

Arithmetic Assignment Operators

o Here the statement causes the value of the right-hand-operand to be assigned to the memory location of the left-hand-operand.

o Statement writes the value of the right-hand-operand (5) into the memory location of the integer variable a (which is the left-hand-operand in this case).

C Programming Higher Technological Institute10

left-hand-operand = right-hand-operand

a=5 ;

b=a=5 ;

Page 11: Computer programming chapter ( 4 )

Assignment and Arithmetic Operators

Operator Description

+ = Addition Assignment Operator

- = Subtraction Assignment Operator

* = Multiplication Assignment Operator

/ = Division Assignment Operator

% = Remainder Assignment Operator

C Programming Higher Technological Institute11

Page 12: Computer programming chapter ( 4 )

Equivalence of Statements

Statement Equivalence

x+ = y; x = x + y;

x- = y; x = x - y;

x* = y; x = x * y;

x/ = y; x = x / y;

x% = y; x = x % y;

C Programming Higher Technological Institute12

z = z * x + y ; z *= x + y ; ?

Page 13: Computer programming chapter ( 4 )

Equivalence of Statements

Statement Equivalence

x+ = y; x = x + y;

x- = y; x = x - y;

x* = y; x = x * y;

x/ = y; x = x / y;

x% = y; x = x % y;

C Programming Higher Technological Institute13

z = z * x + y ; z *= x + y ; ≠

Page 14: Computer programming chapter ( 4 )

Example# include <stdio.h> main ( ){ int x, y, z; x =1; y =3; z= 10;printf (“Given x = %d, y = %d, and z = %d,\n”, x, y, z);x= x + y;printf ( “ x= x +y assigns %d to x;\n”, x);x = 1; x+= y ; printf ( “ x+= y assigns %d to x;\n”, x);x = 1; z = z* x + y;printf ( “z = z*x+y assigns %d to z;\n”, z); z = 10; z = z* (x + y);printf ( “z = z*( x+ y) assigns %d to z;\n”, z);z = 10; z *= x + y;printf ( “z *= x+ y assigns %d to z;\n”, z);return 0; }

C Programming Higher Technological Institute14

Page 15: Computer programming chapter ( 4 )

Unary Minus Operator

o Given an integer, you can get its negation by changing the sign of the integer by using, -, the minus operator, which is called the unary minus operator.

o Differ between the unary minus operator and the subtraction operator.

o The first – symbol is used as the subtraction operator while the second –symbol is the unary minus operator.

C Programming Higher Technological Institute15

x = 1.234 ; -x equals -1.234

z = x- -y; OR z = x- (-y);

Page 16: Computer programming chapter ( 4 )

Incrementing or Decrementing by ONE

C Programming Higher Technological Institute16

X++ ++X

X-- --X

IncrementP

ost-

Decrement

Pre

-

Page 17: Computer programming chapter ( 4 )

Example# include <stdio.h> main ( ){int x=5; printf (“ X++ %d ,\n”, x++);printf (“++X %d ,\n”, ++x);printf (“ X-- %d ,\n”, x--);printf (“ --X %d ,\n”, --x);return 0; }

C Programming Higher Technological Institute17

Page 18: Computer programming chapter ( 4 )

Relational Operators

Operator Description

== Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

C Programming Higher Technological Institute18

Page 19: Computer programming chapter ( 4 )

oAll relational expressions produce a result of either 0 or 1.

oGiven x = 3 and y = 5, for instance, the relational expression x < y gives a result of 1

C Programming Higher Technological Institute19

x * y < z + 3 (x * y) < ( z + 3 )

Relational Operators

Page 20: Computer programming chapter ( 4 )

Example# include <stdio.h> main ( ){ int x, y;

double z;

x = 7; y = 25; z = 24.46;

printf (“ Given x = %d, y = %d and z = %2f, \n”, x, y, z);printf ( “ x > = y produces: %d \n”, x > =y);printf ( “x = = y produces: %d \n”, x = =y);printf ( “x < y produces: %d \n”, x < y); printf ( “x > y produces: %d \n”, x > y);printf ( “x != y - 18 produces: %d \n”, x ! =y -18); printf ( “x + y ! = z produces: %d \n”, x +y ! = z); return 0; }

C Programming Higher Technological Institute20

Page 21: Computer programming chapter ( 4 )

This is algebraically True and is supposed to return 1.

o However, the expression returns 0 which means that the equal to relationship does not hold.

o This is because the truncation of the integer division – that is , 1 /2 – produces 0 not 0.5.

C Programming Higher Technological Institute21

1 / 2 + 1 / 2 = = 1 1 Or 0

Relational Operators

Page 22: Computer programming chapter ( 4 )

The Cast Operator

o You can convert one data type to a different oneby prefixing and cast operator to the operand.

o The general form of the cast operator is

o Here data-type specifies the data type you wantto convert to x is a variable ( or, expression)that contains the value of the current data type.

o You have to include the parentheses (and) tomake up a cast operator.

C Programming Higher Technological Institute22

(data-type) x

Page 23: Computer programming chapter ( 4 )

Example# include <stdio.h> main ( ){int x, y;

x = 7;

y = 5;

printf ( “ Given x = %d, y = %d, \n”, x, y);

printf ( “ x / y produces: %d \n”, x /y);

printf ( “(float) x / y produces: %f \n”, (float) x / y);

return 0 ;

}

C Programming Higher Technological Institute23

Page 24: Computer programming chapter ( 4 )

LOGO

Eng. Ibrahim Elewah

Higher Technological Institute 10th of Ramadan City6th of October Branch

Electrical and Computer Engineering Department

24

Main Reference

HTI Student Book and “C For Dummies”

by Dan Gookin 2nd Edition