1. float fincome; float fnet_income; double dbase, dheight, darea; int iindex =0, icount =0; char...

87
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING Revision Dr. Nik Adilah Hanin Bt. Zahri [email protected] 1

Upload: jewel-webb

Post on 05-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

EKT150 INTRODUCTION TO COMPUTER PROGRAMMING

EKT150INTRODUCTION TO COMPUTER PROGRAMMING

Revision

Dr. Nik Adilah Hanin Bt. [email protected]

11Declaring Variablesfloat fIncome;float fNet_income;double dBase, dHeight, dArea;int iIndex =0, iCount =0;char cCh=a, cCh2;const float fEpf = 0.1, fTax = 0.05;float income, net_income;Declare and initializeNamed constant declared and initialized22Formatted Output with printfExample of printf() statementprintf(Sum is %d\n",dSum);Output:Sum is 66When the printf is executed, it starts printing the until it encounters a % character (conversion specifier) Calculations can be performed inside printf statementsprintf( "Sum is %d\n", integer1 + integer2 );

3Formatted Input with scanfscanf is a function in C which accept input from user usually from a keyboard. scanf( %d , &dA );This scanf statement has two arguments%d - indicates data should be a decimal integer&dA - location in memory to store variable

4Conversion Identifier for Formatted Input/OutputCommon Conversion Identifier used in printf and scanf functions.

printfscanfint%d%dfloat%f%fdouble%f%lfchar%c%cstring%s%s5Arithmetic OperatorsUsed to execute mathematical equationsThe result is usually assigned to a data storage (variable) using assignment operator ( = )E.g. sum = marks1 + marks2;

66Standard algebraic relational operatorC relational operatorExample of C conditionMeaning of C condition

===x == yx is equal to y!=x != yx is not equal to y>>x > yx is greater than y=x >= yx is greater than or equal to y= 90

print Grade is A

Flow ChartPseudo Codeint fMark;char cGrade;if(fMark >= 90) cGrade = 'A';printf(Grade is : %c\n, cGrade);Program

12Specifies an action to be performed both when the condition is true and when it is false

The syntax of two-way selection is: if (expression) statement1; elsestatement2;

If the value of the expression is true, statement1 is executed; if false, statement2 is executedSelection Structures : Two-way (if - else)Statement 1Statement 21213Selection Structures : Two-way (if - else)Example:

if students grade is greater than or equal to 60Print PasselsePrint FailPseudo Codetrue

false

print Failprint Passgrade >= 60Flow Chartif(fGrade >=60)printf(Pass);elseprintf(Fail);

Program1314The syntax is:if (exp1)Statement 1;else if (exp2)Statement 2;else if (exp3)Statement 3;elseStatement n;An if-else if control structure shifts program control, step by step, through a series of statement blocks.Selection Structures : Multi-Selection (if - else if)Example: TempDisplay>30 0Chot20-30 0CMild10-20 0CCold30 Print hottruefalsefTemp > 20Print mildtruefTemp >10Print coldPrint very coldtruefalsefalseif(fTemp > 30)printf( hot\n);

else if((fTemp >=20) && (fTemp=10) && (fTemp < 20))printf(cold\n);

elseprintf( very cold\n);16Selection Structures : Multi-Selection (if - else if)fTemp >30 Print hottruefalsefTemp > 20Print mildtruefTemp >10Print coldPrint very coldtruefalsefalseWhats wrong??17if (fTemp > 30)printf( hot\n);

if (fTemp >=20)printf( mild\n);

if (fTemp >=10)printf(cold\n);

if (fTemp 30)printf( hot\n);

else if (fTemp >=20)printf( mild\n);

else if (fTemp >=10)printf(cold\n);

elseprintf( very cold\n);

Source Code #1Source Code #2Nested ifWhen one control statement is within another, it is said to be nested

18if(exp1){statement1;

if(exp2) statement2;}if(exp1)if(exp2) statement1;

Example :

if (SumPurchase >= 1000) {if (FreeGiftCoupon ==1) printf( "Get discount and Free Gift.\n);elseprintf( "Get discount.\n); }elseprintf(No discount and Free Gift.\n);

19Nested ifswitch StructuresSimilar to if-else if control structureThe general form (syntax):switch (expression) { case value1: statements1; break; case value2: statements2; break; case valuen: statementsn; break; default: statements; }

20switch StructuresThe expression is usually an identifier. The value of the expression can be only integral. The expression is sometimes called the selector Its value determines which statement is selected for execution. A particular case value should appear only once. One or more statements may follow a case labeldo not need to use braces to turn multiple statements into a single compound statement. The break statement may or may not appear after each statement.

21Example:

switch (cGrade) { case 'A': printf("The grade is A.); break; case 'B': printf("The grade is B.); break; case 'C': printf("The grade is C.); break; case 'D': printf("The grade is D.); break; case 'F': printf("The grade is F.); break; default: printf("The grade is invalid.); } where, cGrade is a variable of the type charif the value of cGrade is 'A', the output will be The grade is A.22switch Structures23Repetition StructureSpecifies a block of one or more statements that are repeatedly executed until a condition is satisfiedThree types : whilefordo-while2324The while Repetition StructureProgrammer specifies an action is to be repeated while some conditions remain trueproduct