Immanuel Training Institute proudly presents free software courses
To get certificate Contact: iits.certificate@gmail.com
CHAPTER 1 Introduction and structure of C program
CHAPTER 2 Data types, Constants, Variables and I/O Statements
CHAPTER 3 Operators and Expressions
CHAPTER 6 String Manipulations
CHAPTER 8 Structure, Unions and Enumerated Data types
CHAPTER 4
CONTROL STATEMENTS
In C, usually the
control flows from one instruction to the next instruction. This control flow
from one command to the next is called sequential control flow. But, in most C programs the programmer may want to
skip instructions or repeat a set of instructions repeatedly when writing
logic. This can be referred to as sequential control flow. The declarations in
C let programmers make such decisions which are called decision-making or
control declarations.
There are four types of control
statements in C:
· Conditional/Decision
making statements
· Selection/Switch
statement
· Iteration
statements
· Unconditional/Jump
statements
·
CONDITIONAL/DECISION MAKING
The if, if-else, nested
if-else statements are used to carry out a logical test and then take one of
two possible actions depending on the outcome of the test (ie, whether the
outcome is true or false).
v
If
Syntax:-
if(condtion is true)
{
True statements;
}
Ex:-
main()
{
int i;
printf(“enter the number<10”);
scanf(“%d”,&i);
if(i<10)
printf(“the entered number is %d<10”, i);
}
v
If-Else
Syntax:-
if(condtion )
{
True statements;
}
else
{
False statement;
}
Ex:-
main()
{
int i;
printf(“enter the number<10”);
scanf(“%d”,&i);
if(i<10)
printf(the entered number is less than 10”);
else
printf(the entered number is greater than 10”);
}
v
Nested if
Syntax:-
if(condition 1)
{
if(condtion 2)
{
True statements2;
}
else
{
False statement 2;
}
}
else
{
False statement 1; }
Ex:- main()
{
int i;
printf(“enter the number”);
scanf(“%d”,&i);
if(i<10)
printf(the entered number is less than 10”);
else{
if(i==10)
printf(the entered number is equal to 10”);
else
printf(the entered number is greater than 10”);
}
}
·
SELECTION/SWITCH
A switch statement is used for multiple way selections that will
branch into different code segments based on the value of a variable or
expression. This expression or variable must be of integer data type.
switch(expression)
{
case constant 1:
Block;
break;
case constant 2:
Block;
break;
.
.
.
default:
default block;
}
Ex:
main()
{
int a=1;
switch(a)
{
case 1:
printf(“ I am 1\n”);
break;
case 2:
printf(“ I am 2\n”);
break;
default:
printf(“ I am 3\n”);
}
}
·
ITERATION
Iteration provide a way to repeat commands and control how many times they are repeated
v
for
Syntax:-
for(initialize counter; test condition; increment/decrement counter)
{
Body of the loop;
….
}
Ex:-
int i,n, sum;
for(i=1;i<=10;i++)
{
printf(“enter any number”)
scanf(“%d”,&i);
if(n<0)
continue;
}
v
While
Syntax:-
while(condition)
{
…………..
Body of the condition
………….
}
Ex
:-
int i, j;
i=1;
while(i<0)
{
j-1;
i++;
}
printf(“ enter the max value is %d”,j);
printf(“the value after the overflow is %d”,i);
}
v
Do while
Syntax :
Do
{
…………………..
Body of the condition;
…….……………
}while(condition);
Example:-
main()
{
int i, n;
printf(“enter the number:”);
scanf(“%d”,&n);
i=0;
do
{
printf(“the number is %d”n);
i=i+1;
}while(i<n);
}
v
Break;
1. It is used to come out of the loop instantly. When
a break statement is encountered inside a loop, the control directly comes out
of loop and the loop gets terminated. It is used with if statement,
whenever used inside loop.
2. This can also be used in switch case control
structure. Whenever it is encountered in switch-case block, the control comes
out of the switch-case.
Syntax:-
break;
Ex:-
main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==6)
break;
printf(“%d”,i);
}
}
v
Continue
The continue statement is used inside loops. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration, skipping the execution of statements inside the body of loop for the current iteration.
Syntax:
Continue;
Ex:-int i,n, sum;
for(i=1;i<=10;i++)
{
printf(“enter any number”)
scanf(“%d”,&i);
if(n<0)
continue;
else
sum=sum+n;
}
Printf(“sum is ….%d”,sum);
}
v
Goto
The goto statement is rarely used because it makes
program confusing, less readable and complex. Also, when this is used, the
control of the program won’t be easy to trace, hence it makes testing and
debugging difficult.
Syntax:
goto label;
……….
……..
Label:
Ex:-
main()
{
int a,b;
printf(“\n enter the two numbers”);
scanf(“%d%d”,&a,b);
if(a==b)
goto equal;
else{
Printf(“\n a and b or not equal”);
exit(0);
}
equal:
printf(“a and b are equal”);
} EXAMPLE PROGRAMS:
1. Write
a program to find biggest number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the two nos.:");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("A
is big");
}
getch();
}
2. Write
a program to check the given two numbers are equal or not
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter any two values:");
scanf("%d%d",&a,&b);
if(a==b)
{
printf("A
& B both are equal");
}
else
{
printf("Not
equal");
}
getch();
}
3. Write
a program to find biggest number among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter any three nos.:");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
{
printf("A
is big");
}
else
{
if(b>c)
{
printf("B
is big");
}
else
{
printf("C
is big");
}
}
getch();
}
4. Example
program for if-else condition.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the no.(1-4):");
scanf("%d",&a);
if(a==1)
printf("One");
else
if(a==2)
printf("Two");
else
if(a==3)
printf("Three");
else
if(a==4)
printf("Four");
else
printf("Invalid
entry");
getch();
}
5. Example
program for switch condition.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the no.(1-4):");
scanf("%d",&a);
switch(a)
{
case
1:
printf("One");
break;
case
2:
printf("Two");
break;
case
3:
printf("Three");
break;
case
4:
printf("Four");
break;
default:
printf("Invalid
entry");
}
getch();
}
6. Write
a program to find factorial of a number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,f;
clrscr();
printf("Enter the no.:");
scanf("%d",&n);
f=1;i=1;
while(i<=n)
{
f=f*i;
i++;
}
printf("The factorial value of %d is
%d",n,f);
getch();
}
7. Write
a program to display multiplication table for given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
printf("Enter the value for i:");
scanf("%d",&i);
j=1;
do
{
k=i*j;
printf("%d x %d = %d\n",i,j,k);
j++;
}while(j<=12);
getch();
}
8. Write
a program to print fibonacci series.
#include<stdio.h>
#include<conio.h>
void main()
{
int f1,f2,f3,n,i;
clrscr();
printf("Enter the no.of lines:");
scanf("%d",&n);
f1=1;f2=1;
printf("%d\n%d\n",f1,f2);
for(i=3;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf("%d\n",f3);
}
getch();
}
9. Example
program for goto condition.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the a,b values:");
scanf("%d%d",&a,&b);
c=a+b;
printf("The addition of c is %d",c);
goto l1;
printf("The increment value of c is
%d",++c);
l1:getch();
}
10. Example
program for continue condition.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,n1;
clrscr();
printf("Enter the no.:");
scanf("%d",&n1);
for(n=10;n>0;n--)
{
if(n==n1)
continue;
printf("%d\n",n);
}
getch();
}
11. Example
program for break condition.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,n1;
clrscr();
printf("Enter the no.:");
scanf("%d",&n1);
for(n=10;n>0;n--)
{
if(n==n1)
{
printf("Countdown
aborted");
break;
}
printf("%d\n",n);
}
getch();
}
12. Write
a program to check given number is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
Int n;
clrscr();
printf("Enter the no.:");
scanf("%d",&n);
n=n%2;
if((n==0))
printf("Even");
else
printf("Odd");
getch();
}
13. Write
a program to check given is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year:");
scanf("%d",&year);
year=year%4;
if(year==0)
printf("Leap
year");
else
printf("Not
leap year");
getch();
}
14. Example
program for For-loop condition.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter the no.:");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d\n",i);
getch();
}
15. Example
program for nested For-loop condition.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the no.:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",j);
}
printf("\n");
}
getch();
}