1 / 60

Introduction to Programming

Introduction to Programming. Muhammad Irfan. Communication:. Means “sharing of information” If both person understand each other its called effective communication.

beck
Télécharger la présentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Muhammad Irfan

  2. Communication: • Means “sharing of information” • If both person understand each other its called effective communication. • If one person understand English and other understand Chinese then one of these have to learn one of the language to understand each other. • If we want to command on Computer we have to learn Computer language.

  3. History of “C” Language: • Machine language • Assembly Language • FORTRAN • COBOL • CPL • BCPL • B • C • C++ • Java

  4. Machine Language: • It consists of 0,1 • It is also called as pure binary language. Because it only contains 100% 0,1. • It is also called as; • LLL ( Low Level Language) • MOL ( Machine Oriented Language) • It is difficult to understand, learn, write and debug. • A program written in machine language runs fast, because computer understand 0,1 very easily. • A person who works in machine language is called as SYSTEM EXPERT.

  5. Assembly Language: • In assembly language 100% of machine language was taken out of which 20% was converted into English like symbols called as (Mnemonics). • Where as the remaining 80% was still 0,1. • A translator was used with assembly language which was capable to convert the 20% Mnemonics into 0,1 form. • That translator was called as Assembler. • A program written in assembly language runs fast but slow as compared to machine language due to translator (Assembler).

  6. FORTRAN: • Stands for FORmulaTRANslation • It is such a language which is used in a field where we need fast and accurate calculation. • FORTRAN is used in the following fields; • Engineering • Mathematics • Statistics • Chemistry • Physics etc…….

  7. COBOL: • Stands for COmmonBusiness Oriented Language. • It is such a language in which we can develop software, which will solve our problems regarding business. • It can be purely applied to business field. CPL: • Stands for Combined Programming Language. • 75 % portion of FORTRAN and 75% portion of COBOL were combined with each other.

  8. CPL: • And CPL was introduced. • But it was flopped due to it’s huge size. BCPL: • Stands for Basic Combined Programming Language. • Basic and important features from FORTRAN and some basic and important features from COBOL were combined with each other. • And they introduced a new language called as BCPL. • It got flopped due to its limitation.

  9. “B” Language: • Ken Thompson was researcher at A.T & T’s (American Telephone & Telegraph’s) Bell Lab. • Ken Thompson wrote language. • Ken Thompson thought what should be the name of my language. • From Bell he took the “B” and kept the name of language “B” Language. • Telephone was discovered by “ Alexander Graham Bell”.

  10. “C” Language: • Denis Ritchie was researcher of AT & T’s Bell Lab. • Denis Ritchie took permission from Ken Thompson and modified “B” language. • A new language was introduced. • He also thought what should be the name of my language now. • From alphabetical series after “B” there comes “C” and he kept the name of language as “C” language. • Denis Ritchie gave C-language to ANSI (American National Standard Institution).

  11. “C” Language: • ANSI registered C-language that it can work in every field e.g. Engineering, Mathematics, Business etc… • Programmer at that time left other languages and adopted C-language. • This language was introduced in 1972. • It is also called as fundamental language. • C-language consists of 90 to 95 % English. • A translator is used that converts that English into machine understandable form.

  12. “C” Language: • That translator is called as “Compiler”. • There are three categories of language. • H.L.L ( High Level Language) • M.L.L ( Middle Level Language) • L.L.L (Low Level Language) • C-language has the feature of H.L.L and L.L.L, that’s why also called as M.L.L.

  13. “C” Language Character Set: • A… Z • a....z • #,_,+,-,<,>,=,!......... Variable: • A named memory location, which holds something, is called as variable. • Variable always get created inside “RAM”. Variable Types: • There are three main types of variable;

  14. Variable Types: • Integer variable (2 bytes): that space in memory, which stores integer values is called as integer variable e.g. (1,2,3.....) • Float | Real variable (4 bytes) : That space in memory which stores float values, called as float variable, e.g. (9.8, 88.12, 77.4……) • Character variable (1 byte): That space in memory, which stores character values is called as character variable e.g. (‘1’, ‘A’, ‘@’……..)

  15. Constant: • “That thing, which get store inside variable, is called as Constant”. Constant Types: • There are three main types of Constant; • Integer Constant: (1,2,3,4,5,6………………..) • Float | Real Constant: (9.8, 8.12, 3.12……..) • Character Constant: (‘A’, ‘B’, ‘2’, ‘!’,……..) Note: Any single character enclosed in single commas is called character Constant, e.g. ‘A’ ‘1’, ‘@’. ‘9’……)

  16. Data Type: • “These are reserved word, use to create or declare variables of different types inside the memory”. Data Type:Meaning: int Stands for Integer, use to make integer variable. float Stands for Float, use to make float variable. char Stands for Character, use to make character variable.

  17. Data Type: How to declare or create variable using Data Type; Data Type:Variable Name: int x; float y; char c; Note: Compiler check each data and convert to machine language, which only consist of 0,1.

  18. Rules for Naming variable: • Variable name may consists of; • Alphabets • Numbers • And a special symbol _ . • Variable name must start from an alphabet or _. • Reserved words can’t be the name of variable. • No spaces allowed in the name of variable.

  19. C-Language Programming Structure: main () { Statement 1; Statement 2; Statement 3; Statement n; }

  20. Starting for this (main ()) and Ending with } is called main block. This symbol ({) is called start of program and This symbol (})is called end of program. • We will write the C-language program in the editor. • Save the file by any name have extension .c • File name length may not exceed from 8 characters. • File name may not have space inside it.

  21. Output Function: “If we want to print something over the screen, we will use the following function; printf(“Allah”); main () { clrscr(); printf(“text”); }

  22. File name.obj File save by the any name e.g. (first.c). • Now we need to compile our program. • What happens when we compile program; • .obj is (Machine Readable form) • .exe is (Executable file) • .bak is (Backup file) File name.c Source Code Translator (Compiler) File name.exe File name.bak

  23. Note: When ever we want to write C-language, we have to remind three things; 3. Output 2. RAM 1. Editor or Source code

  24. main() { clrscr(); printf(“1st line”); printf(2nd line”); printf(3rd line”); } Note: when we compile it on the user screen it will come on one line until that 80 columns finished so for break through the normal sequence we use (escape sequence).

  25. Escape Sequences: “These are special character, preceded by \, and are used to break the normal sequence of printing.” Escape Sequence;Meaning; \n Go to next line. \t Go ahead 8 character (tab) \a Alert (beep sound) \” To print “ \’ To print ‘ \\ To print \

  26. main() { clrscr(); printf(“\”1st lint\””); printf(“\n\’2nd line\’”); printf (“\n\\3rd line\\”); printf(“\n\n\n\n\t\t\t\t\t\tK\aA\aR\aD\aA\aN); } Note: do it practical and check the difference of each option.

  27. Operators: “Operators are special symbols, on to which the compiler performs a specific action, the action performed depends, upon the nature of the operator used.” Types: There are three types of operators; • Unary Operator, • Binary Operator, • Ternary Operator.

  28. Unary Operators: “That operators which perform action over single operand, are called as Unary Operators.” • ++ (Increment Operator), • -- (Decrement Operator), • - (Negation), • ! (Not). • Binary Operators: “That operator which perform action over two operands, are called as Binary Operator.”

  29. Binary Operators: • Assignment Operator (=) • Arithmetic Operator (+,-,*,/,%) • Relational Operator (<,>,<=,>=,==,!=) • Logical Operator (&&,||,!) • Compound assignment Operator (+=,-=,*=,/=,%=) • Ternary Operator: Those operators which perform action over three operands, are called as Ternary Operators.” • (?:) The only Ternary Operators used in C-language

  30. Assignment Operator; • Denoted as = • Used to copy whatever on it’s RIGHT SIDE TO LEFT SIDE • There must be variable on the left side of assignment operator. • On right side we can write (Variable, Constant, or Expression)

  31. Format Specifier: “These are character preceded by %, and are used to; • Access value from variable, • Store value inside variable (During Run Time) Format Specifier;Meaning; %d Use with Integer Variable, %f Use with Float Variable, %c Use with Character Variable. Note: Have a look of practical in next slide.

  32. main() { int a; float b; char c; clrscr(); a=24; b=21.10; c=‘!’; printf(“%d”,a); printf(“\n%f”,b); printf(“\n%c”,c); }

  33. Simple calculator practical (Statistic) main() { int n1,n2 int sum,sub,mul,div; n1=100; n2=5; sum=n1+n2; sub=n1-n2; mul=n1*n2; div=n1/n2; clrscr(); printf(“Value of n1 is : %d”,n1); printf(“\nValue of n2 is : %d”,n2); printf(“\nAddition is : %d”,sum); printf(“\nSubtraction is : %d”,sub); printf(“\nMultiplication : %d”,mul)’ printf(‘\nDivision : %d”,div); }

  34. Input Function: “In C-language scanf function is used to store value inside a variable.” Syntax: “scanf (“Format Specifier”, Address operator variable name); e.g. (“%d”,&a); Note: In next slide (scanf) option practical.

  35. Simple calculator practical (dynamic) main() { int n1,n2,n3; int res; clrscr(); printf (“Enter 1st value :”); scanf(“%d”,&n1); printf(“\nEnter 2nd value :”); scanf(“%d”,&n2); printf(“\nEnter 3rd value :”); scanf(“”%d”,&n3); res=(n1+n2)*n3; printf(“\n%d + %d + %d = %d”,n1,n2,n3,res); }

  36. Note: In exam these kind of questions can be included; • E.g. write a program, which accepts, two values from keyboard, and then prints it’s sum, sub, mul, or div result. • E.g. write a program, that accepts one value from keyboard, and then print it’s cube or square result. Cube: 2*2*2=8 Square:2*2=4 C-language first preferred to breaket () C-language second preferred to / * C-language third preferred to + -

  37. Square practical: main() { int num,res; clrscr(); printf(“A num plz”); scanf(“%d”,&num); res=num*num; printf(“\nSquare of %d is %d”,num,res); }

  38. Cube practical: main() { int num,res; clrscr(); printf(“A num plz”); scanf(“%d”,&num); res=num*num*num; printf(“\nCube of %d is %d”,num,res); }

  39. How to find circle area: main() { int r,a; clrscr(); printf(“Enter the rediuse :”); scanf(“%d”,&r); a=3.14*r*r; printf(“\nThe area of circle is %d”,a); printf(“\nThe area of circle having rediuse %d is %d”,r,a,); getche(); }

  40. How to find length & width of rectangle: main() { int a,l,w; clrscr(); printf(“Enter the length :”); scanf(“%d”,&l); printf(“Enter the width :”); scanf(“%d”,&w); a=l*w; printf(“\nThe total area of rectangle is :%d”,a); getche(); }

  41. How to find obtain marks and percentage: main() { int OS,CA,IC,IP; int total; float per; clrscr(); printf(“Operating System :”); scanf(“%d”,&OS); printf(“Computer Application :”); scanf(“%d”,&CA); printf(“Introduction to Computer :”); scanf(“%d”,IC); printf(“Introduction to Programming :”); scanf(“%d”,&IP); total=OS+CA+IC+IP; per=total*100.0/400.0; printf(“Total marks :%d”,total); printf(“\nPercentage is :%f”,per); getche(); }

  42. Enter 4 digits number which gives reverse order number: main() { int num,d1,d2,d3,d4; clrscr(); printf(“Enter 4 digit number :”); scanf(“%d”,&num); d1=num%10; num=num/10; d2=num%10; num=num/10 d3=num%10; d4=num/10; printf(“Number in reverse order is :%d%d%d%d”,d1,d2,d3,d4); getch(); }

  43. If-else: In case of “if” when the condition is true, it perform some task, and if the condition is false, nothing happens. e.g. If(2>1) { printf(“hello”); } Output: hello If(2<1) { printf(“bye”) } Output: Blank Screen

  44. Write a program, which accept one value and give you the positive, negative and zero result: main() { int num; clrscr(); printf(“Enter A Number :”); scanf(“%d”,&num); if(num>0) { printf(“\nPostive”); } if(num<0) { printf(“\nNegative) } if(num==0) { printf(“\nZero”) } getche(); }

  45. “C” Language: If-else cont…… If-else: Now we want to perform some task when the condition is false, in such a case we make use of else. e.g. clrscr(); If(condition) { printf(“hello”); } else { printf(“bye”); }

  46. Write a program, which accept a single value from Keyboard, then find out either the entered number is EVEN or ODD. main() { int num; clrscr(); printf(“Enter a number :”); scanf(“%d”,&num); if(num%2==0) { printf(“\nNumber is EVEN”); } else { printf(“\Number is ODD”); } }

  47. Write a program, which accept marks obtained in four subject. Calculate it’s total, Calculate it’s percentage and then Calculate it’s Grade.

  48. Switch-case: “Switch-case is also one of the decision control construct, in this a number of cases is written to switch we apply a single value, that value is compared to the cases, with whom the condition gets executed.” If none of the case gets satisfied, default is execute automatically. e.g. Syntax of switch: switch (Variable / Constant) { case 1: case 2: case 3: . . case n: default?: }

  49. Switch-case: Show case without break. main() { int n; clrscr(); printf(“A number plz :”); scanf(“%d”,&n); switch(n) { case 1: printf(“\nI am case 1”); case 2: printf(“\nI am case 2”); case 3: printf(“\nI am case 3”); default: printf(“\nDefault Executed”); } getche(); }

  50. Switch-case: (break) option will help us to stop on our desire location where our case is. main() { int n; clrscr(); printf(“A number plz :”); scanf(“%d”,&n); switch(n) { case 1: printf(“\nI am case 1”); break; case 2: printf(“\nI am case 2”); break; case 3: printf(“\nI am case 3”); break; default: printf(“\nDefault Executed”); } getche(); }

More Related