Upload
yogeshsp
View
1.737
Download
1
Tags:
Embed Size (px)
DESCRIPTION
this presentation teach u c language and it brlon to yogesh kumar
Citation preview
Fundamental structure
of programming in 'c'
Presented by:
Mr. Yogesh KumarM.Sc. I Sem.(Bioinformatics)
‘C’ language is simple as compare to the English language
Here is steps comparison…Steps in learning English language
Steps in learning ‘c’
Getting started with 'c'
alphabets words sentences
paragraph
Alphabets , digits
Special symbols
Constants,
variableskeywords
instructions
program
Before planning a program we need to define its logic
(the correct sequence of instructions need to solve the
problem at hand).the term algorithm is often used to refer to the logic of a program.
It is a step by step description of how to arrive at the solution of the given problem.(exp..)
*Write a program to find the larger of two given numbers.Algorithm.1.Input two numbers a and b2.Assign big=a3. If (b>big)the big=b4.Output big5.stop
Concept of Algorithm
Flowchart is the diagrammatic representation of programs and algorithm . it is generally used to understand the program and to solve the program
It uses boxes of different shapes to denote different types of instructions.
The process of drawing a flowchart for an algorithm is often referred to flowcharting
Concept of flowchart
Only a few symbols are needed to indicate the necessary operations in a flowchart. These basic flowchart symbols have been standardized by the American National standards institute (ANSI).
As shown……
flow lines
connector
Basic flowchart symbols
start processing Input/output
decision
example.1
no yes
Flowchar to print first 10 multiple of a given natural numbers using while
start
Read num
i=1
Isi<=1
0
Write(i*num)
i=i+1
stop
Tokens in ‘c’
The alphabets ,numbers and special symbols when properly combined form constants , variables & keywords
Alphabets A,B,C………..Z a , b, c ……....z Digits 0,1,2,3……….9Special symbols ~, ’, !, @, #, %, ^, &, =,|,?,
/,
[ ] : ; ””,’ <> ., { }
constant’s , variable’s & keyword’sConstant, variables & keywords
Variables and constant in ‘c’.constant is an entity that doesn’t change x
x =3
x x=5 we can’t change 3&5 so it is called constant Here at one memory location we can store one value ,we can’t over write it as against this 3&5 do not change , hence are known as constant
3
5
PRIMARY CONSTANT
Integer constantReal constantCharacter
constant
ArrayPointerStructureUnionEnum.etc
Types of c constants1.Primary constant 2.Secondary constant
SECONDARY CONSTANT
As we saw earlier , an entity that may vary during program execution is called a variable.
Type of variable used in program depend on the type of constant stored in it.
Float/real, integer ,or character constantVariable name are name given to locations in
memory
Exp….float=6.0 (4 byte)Int =6 (2 byte)char=‘a’ (1 byte)
Types of c variables
Keywords are the words whose meaning has
already been explained to the c compiler The keywords cannot be used as variable name if
we do so, we are typing to assign new value to keywords which is not allowed by compiler
There are 32 keywords available in cSome are …….short return double switch register signedlong void float union static unsignedint break if goto continue volatilechar default for do sizeof enum else case while auto extern typedef
..etc
C keywords
• structure•class
• integer• float •Character (char)
•int %d •float %f•char %c
•Array•Pointer•Function•String
DATA TYPES
User define data type Fundamental data type Derived data type
'C' operators
Arithmetic operators
+ - * / %
purpose
AdditionSubtractionMultiplication
DivisionRemainder after division
example
7+5=12 7-5=27*5=35
8/2=47%5=2
ARITHMETIC OPERATORS
&& Means logical AND
|| Means logical OR
! Means logical NOT
LOGICAL OPERATOR
RELATIONAL OPERATORS
> Means greater than
< Means less than
== Means equal to
>= Means greater than equal to
<= Means less than equal to
!= Means not equals to
‘C’ has two most useful operators Increment operator ++ ,(m++)or m=m+1
Decrement operator -- ,(m--) or m=m-1 1. m++ simply increase the value by 12. m– -simply decrease the value by 1
Conditional operatorA ternary operator “?” is used in c to construct conditional
expression of the form
Expression1 ? Expression2 : expression3;
Increment and decrement operators
Example of conditional operator #include<stdio.h> #include<conio.h> void main() { float x,y,big; printf("enter the two values\n"); scanf("%f%f",&x,&y); big=(x>y? x:y); printf("the largest number is %f\n",big); } Run Enter the two number 12 45.70 the largest number is 45.70
TYPES OF PROGRAMMING IN 'C'
Decision statements1. if statement2. if else statement3. switch statement Repetitive statements:1. while loop2. do while3. for loop
Decesion making and branching
If statement (DECESSION
STATEMENT)
The general form of if statement is
if ( condition) { Statement-block }
If condition is true the statement block will executed otherwise the statement block will skipped.
#include<stdio.h>#include<conio.h>void main(){float a,b,big;printf(“enter two numbers\n”);scanf(“%f%f”,&a,&b);big=a;if(b>big) big=b;printf(“larger number is:%f”,big);}
RUNEnter two numbers12.5 45.0Larger number is 45.0
if statement example
ENTER
false
true
next statement
condition
Statement-block
if (condition) { statement-1; } else { statement-2; }
if .....else statement
enter
false true
next statement
Condition?
Statement-1Statement-2
The switch statement test the value of given expression against a list of case values
General form switch(expression)
{
case val-1:
statement-1
break;
case val-2:
statement-2;
break;
default:
default-statement;
break;
switch statement
#include<stdio.h>#include<conio.h>void main(){char grade;switch(grade){case ‘A’:printf(“passed with first division”);break;case ‘B’:printf(“passed with second division”);break;case ‘C’:printf(“conditional pass”;break;default:printf(“fail”);break;}getch();}
Example of switch statement
There are two types of repetitive structures
1. Conditional controlled (in this body is repetitively executed until the given condition become true)
a) while statementb) do while statement
2. Counter controlled (in this the number of time
the set of statement is executed ex.. )For loopa) while statement while(condition)
{
Statement(s);
}
Repetitive structures
1. evaluate the condition.2. If the condition true then execute the statement(s)and repeat step 1.3. If the condition is false then the control is transferred out of loop.Exp…..#include<stdio.h>main(){ int num=1,s=0; While(num<=10){ sum +=num; num+1=1;} printf(“sum of first 10 natural numbers : %d ”, s); getch();} RUN Sum of first 10 natural num is : 55
The sequence of operation in while loop
no yes
Flowchar to print first 10 multiple of a given natural numbers using while
start
Read num
i=1
Isi<=1
0
Write(i*num)
i=i+1
stop
In while statement, condition is evaluated first. Therefore the body of loop may not be executed at all if condition is not satisfied at the very first attempt. But in do loop, condition is evaluated at the end. Therefore the body of the loop is executed at lest once in this statement
The general form of this statement is.. do { Statement(s); } while (condition); printf(“…………….”); }
Do while statement
#include<stdio.> main( ) { int num=1,s=0; do { s +=num; num +=1; } while(num<=10); printf(“sum of first natural no is %d”,s) ; getch(); }Run:Sum of 10 natural number is 55.
(do while )..exp..sum of first 10 natural number
• The difference b/w. while & do while loopI. In while the condition is tested before
executing body of loop. /In do while the condition is tested after executing the body
II. Body of do loop is executed at lest once but body of while loop may not be executed at all. /In while , If initial the condition is not satisfied the loop will to get executed even once
While loop verses Do loop
There are situation where you want to have a statement or group of statements to be executed numbers of time and the number of repetition does not depend on the condition but it is simply a repetition up to a certain numbers. The best way of repetition is a for loop. The general form of the loop for single statement is:
for ( initialization ; condition ; increment) { statement(s); }
The for loop statement
#include<stdio.h> main( ) { int I,s=0; for (i=1;1<=10;i++) s=s+1; printf(“sum of first natural number
is:%d”,s); getch( ); } Run:Sum of first 10 natural number is : 55
For loop example to print 10 natural number
Loop construct can be nested or embedded within one another. The inner loop must be completely embedded with the outer loop . There should no overlapping of loops.
**Demonstration of nested loops** #include<stdio.h>
void main()
{
int r,c,sum; for (r=1;r<=3;r++) /*outer loop*/
{
for(c=1;c<=2;c++) /*inner loop*/
{
sum=r+c;
printf(“r=%d c=%d sum=%d\n”,r,c,sum);
}}
getch();
}
Output
r=1 c=1 sum=2
r=1 c=2 sum=3
r=2 c=1 sum=3
r=2 c=2 sum=4
Nested for loop
The loop that we have used so far executed the statements within them a finite number of times. However, in real life programming, one comes across a situation when it is not known beforehand how many times the statements in the loop are to be executed. This situation can be programmed as shown below:
/* execution of a loop an unknown number of times*/#include<stdio.h>
void main()
{
Char another;
Int num;
do
{
printf(“enter a number”);
scanf(“%d”,&num);
printf(“square of%d is %d”, num, num*num);
Printf(“\n want to enter another number y/n”);
Scanf(“%c”,&another);
}while(another==‘y’);
}
getch();
}
Output
Enter a number 5
Square of 5 is 25
Want to enter another number y/n y
Enter a number 7
Square of 7 is 49
Want to enter anotehr number y/n n
Odd loop
THANK YOU ……. .