Getting Started With Coding

25
Getting Started With Coding By: Mr. Baha Hanene Chapter 3

description

Chapter 3. Getting Started With Coding. By: Mr. Baha Hanene. Learning Outcomes. We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in C programs. (L02). Contents. Introduction to Programming Stages & Steps of Writing Programs - PowerPoint PPT Presentation

Transcript of Getting Started With Coding

Page 1: Getting Started With Coding

Getting Started With Coding

By:

Mr. Baha Hanene

Chapter 3

Page 2: Getting Started With Coding

2

Learning OutcomesWe will cover the learning outcome 02 in this chapter i.e.

Use basic data-types and input / output in C programs. (L02)

Page 3: Getting Started With Coding

3

ContentsIntroduction to ProgrammingStages & Steps of Writing ProgramsPreprocessor DirectivesMain FunctionOutput StatementVariables in CAssignment StatementsSample ProgramsReferences

Page 4: Getting Started With Coding

4

Programming IntroductionA language which is used to instruct

computer to perform different tasks according to the requirements.

Every programming language has following basic things.

Declarations Assignments Control Statements I/O (Input/output)

Statements

Page 5: Getting Started With Coding

5

Programming IntroductionAny programming language consists of its

own keywords and syntax rules that distinguishes it from other programming languages.

If you want to program in a specific language, you need the compiler of this specific language.

For example, C language compiler translates programs written in C language to machine language.

Page 6: Getting Started With Coding

6

Programming StyleKeep it clear & simpleKeep it shortThink it through in advance

Page 7: Getting Started With Coding

7

Stages & Steps of Writing ProgramsSpecificatio

nDesign

Coding

Testing

Documentation

Maintenance

Page 8: Getting Started With Coding

8

Preprocessor DirectivesIn C programming whenever you see lines

that begin with a in column 1 are called preprocessor directives (commands)

The #include<stdio.h> directive allows the processor to include basic Input / Output in the program at this point.

#

Page 9: Getting Started With Coding

9

stdio.hThis header file we include in all programs

where we need to use basic I/O commands e.g.

printf(“”); scanf(“”);Some functions are very difficult and very

long to write, these header files contains built in functions to provide us help in writing the programs.

Page 10: Getting Started With Coding

10

void main( )Every C program has a main function, this is

where the program starts it’s execution, as this is the first function to execute in the whole c program.

The reserved word “void” shows that this function will not return any value.

Each C program can contain only one main function e.g.void main()

Page 11: Getting Started With Coding

11

Function BodyA left brace (curly bracket) -- { -- begins the

body of every function. A corresponding right brace -- } -- ends the function body.

void main(){

…………

}BO

DY

Main Function

Page 12: Getting Started With Coding

12

printf(“”);This line is called a C statementprintf(“”); is a function which is used to instruct

computer to display something on computer screenAll text, special symbols you will type inside

printf(“”); between the double quotations i.e. “……..” will be displayed as it is on your computer screens except few commands like /t, /n, %i etc.e.g.

printf(“Welcome at KIC”);

Notice that the above line ends with a Semicolon ; (every C statement ends with a semicolon i.e. ;)

Page 13: Getting Started With Coding

13

Sample Program Explanation#include<stdio.h>

void main()

{

printf(“Hello to programming”);printf(“\n Welcome at KIC”);printf(“\t at Fall 2010/11”);

}

Header File used for Input / Output

Main Function Necessary to start any C program

Output Statement used to display on screen

Function Start

Function End

\t\n Start New

Line

Give Space between text

Page 14: Getting Started With Coding

14

Variables in CIn computer programming, a variable is a

name given to some known or unknown quantity or value, for the purpose of allowing the name to be used independently of the value it represents

[Data Type] [Variable Name] ;Data Types: In C programming there are more data types but we generally use the following data types. (more details in next chapter)

1. int2. float 3. char

Page 15: Getting Started With Coding

15

Variables in CVariable Name: You can give any name to the

variables in c but there are few rules for writing variable names. (A-Z) The first character must be a letter Space is not allowed between names of identifiers (&*^%$#@) Special symbols are not allowed in

identifier names - Hyphens are not allowed in identifier names

however underscores _ can be used (int, float, private etc.) The reserve words are not

allowed in identifier names.

Page 16: Getting Started With Coding

16

Variables in CSome valid identifiers / variable nameshello j9 box22a get_data bin3d4

countSome reserve words (can’t be used as

identifier):private else static int const float

Page 17: Getting Started With Coding

17

Variables in Cvelocityfredatime-of-day inttax_ratex24xfirst value radio@4emp.

velocityfredatime_of_day INVALIDtax_ratex2x4first_value radio4emp

RIGHTRIGHTRIGHT

RIGHTRIGHTWRONG

WRONGWRONGWRONG

Page 18: Getting Started With Coding

18

Variables in CHow & where we write variables in C

programming….?We usually declare all variables inside main

function for example:

#include<stdio.h>

void main(){ int avar ; int salary ; float percentage ;}

Data Type Variable Name

Semi Colon (every C statement ends with a semicolon ;)

Page 19: Getting Started With Coding

19

Sample Program Multiple Variable Declaration

Any Questions

????

?????

Assignment

Statements

Page 20: Getting Started With Coding

20

Assignment StatementsBefore using variables in assignment statements you

should define / declare them first e.g. int a;Assignment Statements

first_number = 35; second_number = 21; sum = first_number + second_number;

The first two statements assigns the values 35 and 21 to the variables first_number and second_number respecitively

The next statement assigns the sum of the two values to the variable sum.

Page 21: Getting Started With Coding

21

Sample Program Header File

Main Function

Function Body

Variable Declaration

Assignment Statements

Screen Output

Page 22: Getting Started With Coding

22

Displaying Variable ValueDo you see something different in normal

printf statements and these which we have seen in the last sample program???

What are these

things????

%d These are called Format Specifiers used to display the value stored in the variable i.e. Inside computer memory

Page 23: Getting Started With Coding

23

Sample Program Output

Page 24: Getting Started With Coding

24

The Logical FlowSource Program

CompileSyntax Error

OK

Object Code/ Program

Run Run-time Error

OK

ResultLogical Error

OK

ENDContinue

Page 25: Getting Started With Coding

25

Referenceshttp://en.wikipedia.org