2. Program..
Sequence of instructions written for specific purpose
Like program to find out factorial of number
Can be written in higher level programming languages that
human can understandhuman can understand
Those programs are translated to computer understandable
languages
Task will be done by special tool called compiler
Version_Sep_2013
C
SharpC
ode.org
3. Compiler will convert higher level language code to lower
language code like binary code
Most prior programming language was ‘C’
Rules & format that should be followed while writingRules & format that should be followed while writing
program are called syntax
Consider program to print “Hello!” on screen
Version_Sep_2013
C
SharpC
ode.org
4. void main()
{
printf(“Hello!”);
}}
Void main(), function and entry point of compilation
Part within two curly brackets, is body of function
Printf(), function to print sequence of characters on screen
Version_Sep_2013
C
SharpC
ode.org
5. Any programming language having three part
1. Data types
2. Keywords
3. Operators3. Operators
Data types are like int, float, double
Keyword are like printf, main, if, else
The words that are pre-defined for compiler are keyword
Keywords can not be used to define variable
Version_Sep_2013
C
SharpC
ode.org
6. We can define variable of data type
Like int a; then ‘a’ will hold value of type int and is called
variable
Programs can have different type of statements likePrograms can have different type of statements like
conditional statements and looping statements
Conditional statements are for checking some condition
Version_Sep_2013
C
SharpC
ode.org
7. Conditional Statements…
Conditional statements controls the sequence of
statements, depending on the condition
Relational operators allow comparing two values.
1. == is equal to1. == is equal to
2. != not equal to
3. < less than
4. > greater than
5. <= less than or equal to
6. >= greater than or equal to
Version_Sep_2013
C
SharpC
ode.org
8. SIMPLE IF STATEMENT
It execute if condition is TRUE
Syntax:
if(condition)
{{
Statement1;
.....
Statement n;
}
Version_Sep_2013
C
SharpC
ode.org
9. main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b) {
printf(“n a is greater than b”); }printf(“n a is greater than b”); }
}
OUTPUT:
Enter a,b values:20 10
a is greater than b
Version_Sep_2013
C
SharpC
ode.org
10. IF-ELSE STATEMENT
It execute IF condition is TRUE.IF condition is FLASE it execute ELSE part
Syntax:
if(condition)
{
Statement1;Statement1;
.....
Statement n;
}
else
{
Statement1;
.....
Statement n;
}
Version_Sep_2013
C
SharpC
ode.org
11. main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b) {
printf(“n a is greater than b”); }
else {else {
printf(“nb is greater than b”); }
}
OUTPUT:
Enter a,b values:10 20
b is greater than a
Version_Sep_2013
C
SharpC
ode.org
12. IF-ELSE IF STATEMENT:
It execute IF condition is TRUE.IF condition is FLASE it checks ELSE IF part
.ELSE IF is true then execute ELSE IF PART. This is also false it goes to ELSE
part.
Syntax:
if(condition)
{
Statementn;Statementn;
}
else if(condition)
{
Statementn;
}
else
{
Statement n;
}
Version_Sep_2013
C
SharpC
ode.org
13. main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b) {
printf(“n a is greater than b”); }
else if(b>a) {else if(b>a) {
printf(“nb is greater than b”); }
else {
printf(“n a is equal to b”); }
}
OUTPUT:
Enter a,b values:10 10
a is equal to b
Version_Sep_2013
C
SharpC
ode.org
14. NESTED IF STATEMENT
To check one conditoin within another.
Take care of brace brackets within the conditions.
Synatax:
if(condition)
{{
if(condition)
{
Statement n;
}
}
else
{
Statement n;
}
Version_Sep_2013
C
SharpC
ode.org
15. main()
{
int a,b;
printf(“n Enter a and b values:”);
scanf(“%d %d ”,&a,&b);
if(a>b) {
if((a!=0) && (b!=0)) {
printf(“na and b both are +ve and a >b); }
else {else {
printf(“n a is greater than b only”) ; } }
else {
printf(“ na is less than b”); }
}
Output:
Enter a and b values:30 20
a and b both are +ve and a > b
Version_Sep_2013
C
SharpC
ode.org
16. Switch..case
The switch statement is much like a nested if .. else statement.
switch statement can be slightly more efficient and easier to read.
Syntax :
switch( expression )
{
case constant-expression1:case constant-expression1:
statements1; break;
case constant-expression2:
statements2; break
default :
statements4; break;
}
Version_Sep_2013
C
SharpC
ode.org
17. main()
{
char Grade = ‘B’;
switch( Grade )
{
case 'A' :
printf( "Excellentn" ); break;
case 'B' :
printf( "Goodn" ); break;
case 'C' :case 'C' :
printf( "OKn" ); break;
case 'D' :
printf( "Mmmmm....n" ); break;
default :
printf( "What is your grade anyway?" );
break;
}
}
Version_Sep_2013
C
SharpC
ode.org
18. Looping…
Loops provide a way to repeat commands and control how many
times they are repeated.
C provides a number of looping way.
while loopwhile loop
A while statement is like a repeating if statement.
Like an If statement, if the test condition is true: the statements
get executed.
The difference is that after the statements have been executed,
the test condition is checked again.
If it is still true the statements get executed again.
This cycle repeats until the test condition evaluates to false.
Version_Sep_2013
C
SharpC
ode.org
19. Basic syntax of while loop is as follows:
while ( expression )
{
Single statement or Block of statements;Single statement or Block of statements;
}
Will check for expression, until its true when it gets false
execution will be stop
Version_Sep_2013
C
SharpC
ode.org
20. main()
{
int i = 5;
while ( i > 0 ) {
printf("Hello %dn", i );
i = i -1; }
}}
Output
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
Version_Sep_2013
C
SharpC
ode.org
21. Do..While
do ... while is just like a while loop except that the test
condition is checked at the end of the loop rather than the
start.
This has the effect that the content of the loop are alwaysThis has the effect that the content of the loop are always
executed at least once.
Basic syntax of do...while loop is as follows:
do {
Single statement or Block of statements; }
while(expression);
Version_Sep_2013
C
SharpC
ode.org
22. main()
{
int i = 5;
do{
printf("Hello %dn", i );
i = i -1; }
while ( i > 0 );
}}
Output
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
Version_Sep_2013
C
SharpC
ode.org
23. For Loop…
for loop is similar to while, it's just written differently. for
statements are often used to proccess lists such a range of
numbers:
Basic syntax of for loop is as follows:Basic syntax of for loop is as follows:
for( initialization; condition; increment)
{ Single statement or Block of statements;
}
Version_Sep_2013
C
SharpC
ode.org
24. main()
{
int i; int j = 5;
for( i = 0; i <= j; i ++ )
{
printf("Hello %dn", i );
}
}}
Output
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Version_Sep_2013
C
SharpC
ode.org
25. Break & Continue…
C provides two commands to control how we loop:
break -- exit form loop or switch.
continue -- skip 1 iteration of loop.
Break is used with switch caseBreak is used with switch case
Version_Sep_2013
C
SharpC
ode.org
26. main()
{
int i; int j = 5;
for( i = 0; i <= j; i ++ ) {
if( i == 3 )
{
break;
}}
printf("Hello %dn", i ); }
}
Output
Hello 0
Hello 1
Hello 2
Version_Sep_2013
C
SharpC
ode.org
27. Continue Example..
main()
{
int i; int j = 5;
for( i = 0; i <= j; i ++ )
{
if( i == 3 )
{{
continue;
}
printf("Hello %dn", i );
}
}
Output
Hello 0
Hello 1
Hello 2
Hello 4
Hello 5 Version_Sep_2013
C
SharpC
ode.org
28. Functions…
A function is a module or block of program code which
deals with a particular task.
Making functions is a way of isolating one block of code
from other independent blocks of code.from other independent blocks of code.
Functions serve two purposes.
1. They allow a programmer to say: `this piece of code does a
specific job which stands by itself and should not be mixed
up with anything else',
2. Second they make a block of code reusable since a function
can be reused in many different contexts without repeating
parts of the program text.
Version_Sep_2013
C
SharpC
ode.org
29. int add( int p1, int p2 ); //function declaration
void main()
{
int a = 10;
int b = 20, c;
c = add(a,b); //call to function
printf(“Addition is : %d”, c);printf(“Addition is : %d”, c);
}
int add( int p1, int p2 ) //function definition
{
return (p1+p2);
}
Version_Sep_2013
C
SharpC
ode.org
30. Exercise…
1. Find out factorial of given number
2. Check whether given number is prime number or not
3. Check whether given number is Armstrong number or
notnot
4. Calculate some of first 100 even numbers
5. Prepare calculator using switch
Version_Sep_2013
C
SharpC
ode.org