PPS Unit 2
PPS Unit 2
Operators are the symbols that instruct to perform some mathematical or logical
operations. Operators operate on certain data types called operands, and they form
a part of the mathematical or logical expressions. More complex expression use
operators.
Example# + - X / &&
Example# a+b-c
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
2.1.1 Operators
Arithmetic Operators
Increment and Decrement
Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Special Operators
These operators are used to perform some basic operations like addition,
substation, division, multiplication and modulo. These requires two operands to
perform its operation hence they are also called binary operators. They are also
knowing as mathematical operators.
Example#
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
a=22;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
b=2;
printf("\nAdd = %d",a+b);
printf("\nSub = %d",a-b);
printf("\nMul = %d",a*b);
printf("\nDiv = %d",a/b);
printf("\nModuls = %d",a%b);
getch();
}
Output:
Add = 24
Sub = 20
Mul = 44
Div = 11
Moduls = 0
These operators operate on one operand only. They are also known as unary
operators. Examples are increment (++), Decrement (--).
Two type of notation are used with these operators: postfix and prefix
Postfix notation
Here symbol is used after the operand means operator modify operand after it is
used.
Example#
a=5; a=5;
b=a++; b=a--;
After these two operations are After these two operations are
executed, a variable has a 6 and b has executed, a variable has a 4 and b has
5. 5.
Prefix notation
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Here symbol is used before the operand means operators modify operand first then
after it is used.
Example#
a=5; a=5;
b=++a; b=--a;
After these two operations are After these two operations are
executed, a variable has a 6 and b has executed, a variable has a 4 and b has
6. 4.
Assignment operator are used to assign value to any variable. It is denoted by ‘=’
sign. Assignment operator is a binary operator i.e. it operates on two values.
Example#
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
a=50;
printf("\na=%d",a);
a+=5;
printf("\na=%d",a);
a-=10;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
printf("\na=%d",a);
a*=2;
printf("\na=%d",a);
a/=2;
printf("\na=%d",a);
a%=5;
printf("\na=%d",a);
getch();
}
Output:
a =50
a =55
a =45
a =90
a =45
a =0
Relational operators are used to compare between two operands and evaluate as
either true or false. For example,# 1 or 0. If the condition is true than return 1
otherwise return 0. They are used in conjunction with logical operators and
conditional & looping statements.
Example#
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
a=22;
b=22;
c=5;
printf("\na = %d b = %d c = %d",a,b,c);
printf("\na>c = %d",a>c);
printf("\na<c = %d",a<c);
printf("\na<=b = %d",a<=b);
printf("\na>=b = %d",a>=b);
printf("\na!=b = %d",a!=b);
printf("\na==b = %d",a==b);
getch();
}
Output:
a = 22 b = 22 c = 5
a>c = 1
a<c = 0
a<=b = 1
a>=b = 1
a!=b = 0
a==b = 1
They are also known as binary operators. Logical operators return Boolean value.
Example#
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=10,b=20;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
printf("\na!=5=%d",a!=5);
getch();
}
Output:
a>=1 && a<=20= 1
a==1 || b==20 = 1
a!=5 = 1
Syntax#
Operand1 symbol(bitwise) operand2
Here operand1, operand2 are the variable or constant.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Output:
4
Syntax#
#include<stdio.h>
#include<conio.h>
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
void main()
{
int a,b,max;
clrscr();
a=5,b=6;
max=a>b?a:b;
printf(“Maximum=%d”,max);
getch();
}
Output:
Maximum=6
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf("\nEnter value of a=>");
scanf("%d",&a);
getch();
}
Output
Enter value of a=>3
3 is odd
The comma operator can be used to link the related expressions together. It allows
evaluating two or more distinct expressions together.
A comma-linked list of expressions is evaluated left to right and the value of the
right-most expression is the value of the combined expression. For example, the
statement
Value = (x = 2, y = 3, x +y);
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Since comma operator has the lowest precedence of all operators, the
parentheses are necessary.
sizeof() is a unary operator which returns the size of the variable passed as an
argument.
Example#
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nint=%d”,sizeof(int));
printf(“\nchar=%d”,sizeof(char));
printf(“\nlong=%d”,sizeof(long));
printf(“\nfloat=%d”,sizeof(float));
getch();
}
Output:
int=2
char=1
long=4
float=4
Example# a+b-c
Operands refer to the values and operator are symbol that represent particular
action.
Precedence
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Operator precedence determines which operator is performed first in an
expression with more than one operators with different precedence.
Associativity
Associativity is only used when there are two or more operators of same
precedence.
Example#
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
2.2.1 printf()
printf() function is used to print the “character, string, float, integer, octal and
hexadecimal values” onto the output screen.
Syntax#
printf("format specifers",value1,value2,..);
Example#
#include<stdio.h>
#include<conio.h>
main()
{
char ch = 'A';
char siteName[40] = "www.shyamsir.com";
float rsFloat = 22.778;
int rollno = 21;
double meterDbl = 33.7777889;
clrscr();
getch();
}
Output
Character is A
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
String is www.shyamsir.com
Float value is 22.778000
Integer value is 21
Double value is 33.777789
Octal value is 25
Hexadecimal value is 15
2.2.2 scanf()
Syntax#
scanf("format specifiers",&value1,&value2,.....);
Example#
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
char siteName[40];
float rsFloat;
int rollno;
double meterDbl;
clrscr();
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
getch();
}
Output:
Character is a
String is www.shyamsir.com
Float value is 23.445669
Integer value is 21
Double value is 12.888345
2.2.3 getch()
getch() takes a character input but does not display the character which is
entered.
Example#
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Output
Enter value of a:
a=c
2.2.4 getchar()
getchar() is a inbuilt function which is stored in “stdio.h” header file and it does
the same function as getch() the difference between both of them is that getchar()
gives the output of the entered character while getch() doesn’t.
Example#
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a;
printf(“Enter value of a: ”);
a=getchar();
printf(“a=%d”,a);
getch();
}
Output
Enter value of a: c
a=c
2.2.5 putchar()
Example#
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
char a;
printf(“Enter value of a: ”);
a=getchar();
putchar(a);
getch();
}
Output
Example#
The files that the programmer writes and store with .h extension means user define
The files that comes with our compiler means in built and extension with .h
#include
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
We can include header file in our program by including it with the C preprocessing
directive #include
#include Preprocessor Directives is used to include both system header files and
user defined header files in C Program.
Syntax#
#include<headefileName.h>
or
#include "headerfileName.h"
The #include directive works by directing the C preprocessor to scan the specified
file as input before continuing with the rest of the current source file.
1. Open a text editor and type a functions definition, like we define a new
function in C program.
2. Save this file with .h extension. Lets assume we saved this file as myCalc.h.
3. Copy myCalc.h header file to the same directory where other inbuilt header
files are stored.(Usually in c:\tc\bin)
#include<stdio.h>
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
#include<conio.h>
#include "myCalc.h"
main()
{
int sumans,subans;
clrscr();
sumans=Add(5,2);
subans=Sub(5,2);
printf("\nAdd = %d",sumans);
printf("\nSub = %d",subans);
getch();
}
Output
Add = 7
Sub = 3
2.4 PreProcessor
The C preprocessor is a macro preprocessor that transforms your
program before it is compiled. Preprocessor is just a text substitution tool
and it instructs the compiler to do required pre-processing before the
actual compilation.
#include<stdio.h>
#define PI 3.14
#ifdef TURBOC
#define INT_SIZE 16
#else
#define INT_SIZE 32
#endif
Processor Meaning
#define Substitutes a preprocessor macro.
#include Inserts a particular header from another file.
#undef Undefines a preprocessor macro.
#ifdef Returns true if this macro is defined.
#ifndef Returns true if this macro is not defined.
#if Tests if a compile time condition is true.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Syntax#
Example#
#define PI 3.14
#define N 200
It has no type. It is a simple text substitution. The text 3.14 or 200 will be dropped
in place wherever PI and N appears as a token.
Syntax#
Example#
It has no type. Macro can be type-neutral means it works with any data type. It's
inlined directly into the code, so there isn't any function call overhead.
#include<stdio.h>
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
#include<conio.h>
#define PI 3.14
#define Max(a,b)(a>b?a:b)
main()
{
float r=10;
clrscr();
printf("\nMax = %d",Max(5,2));
getch();
}
Output
Area of Rect = 314.00
Max = 5
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
2.5.1.1 if
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
2.5.1.1.1.1 If
Syntax#
if(condition)
{
Logic..
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n Enter value of a=");
scanf("%d",&a);
if(a==5)
{
printf("\n No. is 5");
}
getch();
}
Output:
Enter value of a=5
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
No. is 5
2.5.1.1.1.2 If…else
If the condition is TRUE then the control goes to between if and else block,
that is the program will execute the code between if and else statements,
else program will execute the code after else.
We can write only if block. The else if and else clauses are both optional.
Syntax#
if(condition)
{
Logic..
}
else
{
Logic..
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf( "\n Enter the value of a=");
scanf("%d",&a);
if(a>0)
{
printf("\n No is pos");
}
else
{
printf("\n No is neg");
}
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
getch();
}
Output:
Enter the value of a=4
No is pos
The most general way of doing this is by using the else if variant on then if
statement.
As soon as one of these gives a true result, the following statement or block
is executed, and no further comparisons are performed.
Syntax#
if(condition)
{
if(condition)
{
}
}
else
{
if(condition)
{
}
else
{
}
}
void main()
{
int a,b,c;
clrscr();
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
if(a>b)
{
if(a>c)
{
printf("\n%d is max",a);
}
else
{
printf(“\n%d is max”,c);
}
}
else
{
if(b>c)
{
printf("\n%d is max",b);
}
else
{
printf(“\n%d is max”,c);
}
}
getch();
}
Output:
Enter value of a=4
6 is max
There are cases when we would like to execute some more condition when
a condition is TRUE, and some other condition logic when the condition is
FALSE.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Syntax#
if(condition)
{
}
else if(condition)
{
}
else
{
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n Enter the value of a=");
scanf("%d",&a);
if(a%2==0)
{
printf("No is even");
}
else if(a%2!=0)
{
printf("No is odd");
}
else
{
printf(“No is zero”);
}
getch();
}
Output:
Enter the value of a=6
No is even
#include<stdio.h>
#include<conio.h>
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
void main()
{
int a,b,c,t;
clrscr();
printf("\nEnter marks in science=>");
scanf("%d",&a);
printf("\nEnter marks in maths=>");
scanf("%d",&b);
printf("\nEnter marks in computer=>");
scanf("%d",&c);
printf("\nTotal=%d",a+b+c);
t=a+b+c;
if(t<=100)
{
printf("\nC grade");
}
else if(t<=200)
{
printf("\nB grade");
}
else
{
printf("\nA grade");
}
getch();
}
Output:
Enter marks in science=>60
Total=201
A grade
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
2.5.1.2 Switch-case
When you are comparing the same expression to several different values it
gets complicated with if…else if…else so we can use the switch-case
statements as an alternative to the if-else statements.
If any case statement is satisfied, then the logic of that case will be
executed and then the control will come out of switch block. This one will
not happen with if-else block.
Remember that switch block use break statement at the end of each case.
C simply requires that we leave the block before it ends.
Each value is called a case, and the variable being switched on is checked
for each switch case.
Syntax#
switch(testexpression)
{
case expression1:
Logic..
break;
case expression2:
Logic..
break;
case expression N:
Logic..
break;
default:
break;
}
#include<stdio.h>
#include<conio.h>
void main()
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
{
int a;
clrscr();
printf("\nEnter option=>");
scanf("%d",&a);
switch(a)
{
case 1:
{
printf("\n jan");
break;
}
case 2:
{
printf("\n feb");
break;
}
case 3:
{
printf("\n March");
break;
}
case 4:
{
printf("\n April");
break;
}
case 5:
{
printf("\n May");
break;
}
default:
{
printf("\nWrong opt");
}
}
getch();
}
Output:
Enter option=>4
April
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char op;
clrscr();
printf("\n Enter no1:");
scanf("%d",&a);
printf("\n Enter no2:");
scanf("%d",&b);
fflush(stdin);
printf("\n Enter option A,M,S,D=>");
scanf("%c",&op);
switch(op)
{
case 'A':
{
printf("\n Add=%d",a+b);
break;
}
case 'M':
{
printf("\n Multi=%d",a*b);
break;
}
case 'S':
{
printf("\n Sub=%d",a-b);
break;
}
case 'D':
{
printf("\n Div=%d",a/b);
break;
}
default:
{
printf("Wrong option");
}
}
getch();
}
Output:
Enter no1:4
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Enter no2:6
Add=10
• We don't use those expressions to evaluate switch case, which may return
floating point values or strings.
• The case label values must be unique.
• The case label must end with a colon(:)
• The break is used to break out of the case statements.
Break is a keyword that breaks out of the code block,
usually surrounded by braces, which it is in.
• It isn't necessary to use break after each block, but if you
do not use it, then all the consecutive block of codes will
get executed after the matching block.
• The default case is optional, but it is wise to include it as it handles any
unexpected cases.Usually default case is executed when none of the
mentioned cases matches the switch expression.
If Switch
Which statement will be executed Which statement will be executed is
depend upon the output of the decided by user.
expression inside if statement.
if-else statement test for equality as switch statement test only for
well as for logical expression. equality.
if statements can evaluate float switch statements cannot evaluate
conditions. float conditions.
If the condition inside if statements is If the condition inside switch
false, then by default the else statements does not match with any
statement is executed if created. of cases, for that instance the default
statements is executed if created
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
The following illustration shows a loop structure that runs a set of statements until
a condition becomes true.
In entry controlled loop, where test condition is checked before entering the loop
body, known as Entry Controlled Loop.
Example# for and while loops are known as entry controlled loop
While in exit controlled loop the body of loop will be executed first and at the end
the test condition is checked, if condition is satisfied than body of loop will be
executed again.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
It is the most commonly and most popular loop used in any programming
language.
For loop is an entry controlled loop i.e. the condition is checked before
entering into the loop. So, if the condition is false for the
first time, the statements inside for loop may not be
executed at all.
Syntax#
Step 1: Initialization
Example#
Step 2: Condition
The condition expression is evaluated. If the test expression is false, for loop is
terminated at that time. But if the test expression is true , logic inside the body of
for loop is executed and the update expression is updated.
Example#
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
for(intialization ; i <= X; increment/decrement)
for(intialization ; i > X; increment/decrement)
Step 3: Increment/Decrement
Then it evaluates the increment/decrement condition and again follows from step
2. When the condition expression becomes false, it exits the loop.
The variable increment/decrement section is the easiest way for a for loop to handle
changing of the variable. It is possible to do things like x++, x = x + 10, x--
Example#
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("\n Enter a number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n%d",i);
}
getch();
}
Output:
Enter a number=4
1
2
3
4
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
clrscr();
printf("\nEnter a number=");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("\n %d*%d=%d",n,i,n*i);
}
getch();
}
Output:
Enter a number=4
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("\n Enter a number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
printf("\n % is even",i);
}
else
{
printf("\n %d is odd=%d",i);
}
}
getch();
}
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Output:
Enter a number=7
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
for(i=2;i<=n/2;i++) for(i=1;i<=20;i=i+2)
{ {
logic... logic...
} }
for(i=20;i>=1;i--) for(i=no1;i<=no2;i++)
{ {
logic... logic...
} }
for(i=1;i<=n;i++) for(i=1;i<=n;i++)
{ {
for(j=1;j<=n;j++) for(j=1;j<=n;j++)
{ {
Logic..
} for(k=1;k<=n;k++)
} {
Logic…
}
}
}
There may be a condition in a for loop which is always true. In such case, the loop
will run infinite times.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
We can use break to come out of an Infinite loop.
While loop keeps executing till the condition against which it tests remain
true.
The While statement always checks the condition before it begins the loop.
Syntax#
while(condition)
{
logic….
}
Step 1: condition
The while loop evaluates the conditional expression first, If the test expression is
false, while loop is terminated at a time. But if the test expression is true , logic
inside the body of while loop is executed and the update expression is updated.
When the test expression is false, the while loop is terminated. The loop iterates
while the condition is true.
Example# To print 1 to N
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter a number=");
scanf("%d",&n);
i=1;
while(i<=n)
{
printf("\n%d",i);
i++;
}
getch();
}
Output:
Enter a number=5
1
2
3
4
5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,f=1,num;
clrscr();
printf("Enter a number=");
scanf("%d",&num);
i=1;
while(i<=num)
{
f=f*i;
i++;
}
printf("\nFactorial of %d is %d",num,f);
getch();
}
Output:
Enter a number=5
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Factorial of 5 is 120
#include<stdio.h>
#include<conio.h>
main()
{
int no;
int ans=0,x;
clrscr();
printf("\nEnter no =>");
scanf("%d",&no);
while(no>0)
{
x=no%10;
ans=ans*10+x;
no=no/10;
}
printf("\nReverse No = %d",ans);
getch();
}
Output
Enter no =>123
Reverse No = 321
There may be a condition in a while loop which is always true. In such case, the
loop will run infinite times.
while (1)
{
printf("This is an infinite loop");
}
while (1)
{
if(Some Condition)
{
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
break;
}
}
The body of do...while loop is executed once, before checking the test
expression.
Syntax#
do
{
logic…;
}while(upto condition);
do do
{ {
Logic.. Logic..
}while(op!=4); }while(no>4);
do
{
Loops…
}while(op!='s');
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
printf("Enter a number=");
scanf("%d",&n);
i=1;
do
{
printf("\n%d",i);
i++;
}while(i<=n);
getch();
}
Output:
Enter a number=5
1
2
3
4
5
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char op;
do
{ clrscr();
printf("'a' for addiotn \n's' for square \n'c' for cube \n'e' for exit");
printf("\nEnter your choice: ");
scanf("%c",&op);
switch(op)
{
case 'a':
{
printf("Enter number 1: ");
scanf("%d",&a);
printf("Enter number 2: ");
scanf("%d",&b);
printf("Addition:%d",a+b);
getch();
break;
}
case 's':
{
printf("Enter a number: ");
scanf("%d",&a);
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
printf("Square:%d",a*a);
getch();
break;
}
case 'c':
{
printf("Enter a number: ");
scanf("%d",&a);
printf("Cube:%d",a*a*a);
getch();
break;
}
case 'e':
{
break;
}
default:
{
printf("Enter a valid option!!");
}
}
}while(op!='e');
}
Output:
'a' for addiotn
's' for square
'c' for cube
'e' for exit
Enter your choice: s
Enter a number: 5
Square:25
While Do..while
In While loop the condition is tested In do while the statements are executed
first and then the statements are for the first time and then the conditions
executed if the condition turns out to are tested, if the condition turns out to
be true. be true then the statements are
executed again.
These situations tend to be relatively A do while is used for a block of code
rare, thus the simple while is more that must be executed at least once.
commonly used.
while loop do not run in case the A do while loop runs at least once even
condition given is false though the the condition given is false
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
In a while loop the condition is first In a do-while loop the condition is tested
tested and if it returns true then it at the last.
goes in the loop
While loop is entry control loop do while is exit control loop.
Syntax: Syntax:
while (condition) do
{ {
Statements; Statements;
} }while(condition);
There may be a condition in a do while loop which is always true. In such case,
the loop will run infinite times.
do
{
printf("This is an infinite loop");
}while (1);
do
{
if(Some Condition)
{
break;
}
}while (1);
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,no;
clrscr();
printf("\nEnter no =>");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
for(j=1;j<=i;j++)
{
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
printf("*");
}
printf("\n");
}
getch();
}
Output
Enter no =>5
*
**
***
****
*****
The type of loops, where the number of the execution is known in advance are
termed by the counter controlled loop.
Example#
for(i=1;i<=20;i++)
{
printf("\n%d ",i);
}
The type of loop where the number of execution of the loop is unknown, is termed
by sentinel controlled loop.
Example#
do{
printf(“Enter value =>\n”);
scanf("%d", &x);
}while(x > 0);
In the above example, the loop will be executed till the entered value of the variable
x is not 0 or less then 0. This is a sentinel controlled loop and here the variable x
is a sentinel variable.
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
2.5.3.1 break
The break statement is used to stop the current execution and comes out
of loop.
In many situations, we need to get out of the loop before the loop execution
is complete normally.
Syntax#
break;
#include<conio.h>
#include<stdio.h>
void main()
{
int n,i,flag=1;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=0;
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
break;
}
}
if(flag==0)
{
printf("%d is not a prime number.",n);
}
else
{
printf("%d is a prime number.",n);
}
getch();
}
Output:
Enter a number: 13
13 is a prime number.
2.5.3.2 Continue
Continue is similar to break but when break statement is executed it break
the loop where as continue skips the rest of the statement and proceeds for
next iteration.
We can use Continue statment with For Loop, While Loop or do-While Loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter limit: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%3==0)
{
continue;
}
printf("%d\n",i);
}
getch();
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
Output:
Enter limit: 20
1
2
4
5
7
8
10
11
13
14
16
17
19
20
2.5.3.3 goto
Note that a goto breaks the normal sequential execution of the program.
Syntax#
goto label;
.
.
label: statement;
Backward jump
The label: is before statement goto label, a loop will be formed and some
statements will be executed repeatedly. Such a jump is called backward jump.
Forward jump
The label: is placed after the goto label; some statements will be skipped and the
jump is known as a forward jump.
Example#
#include<stdio.h>
#include<conio.h>
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
main()
{
int a;
clrscr();
pos:
printf("\nEnter Positive value =>");
scanf("%d",&a);
if(a<0)
{
printf("\nPlz enter positive value");
goto pos;
}
else
{
printf("\nNo = %d and Square = %d",a,a*a);
}
getch();
}
Output:
Enter Positive value =>-2
No = 4 and Square = 16
2.5.3.4 return
return statement is used for returning from a function. Whenever return statement
is encountered then the control goes out from the function. With the help of return
statement, we can also return some value
Example#
#include<conio.h>
#include<stdio.h>
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
void main()
{
int a,b,sum;
clrscr();
printf("Enter number 1: ");
scanf("%d",&a);
printf("Enter number 2: ");
scanf("%d",&b);
sum=add(a,b);
printf("Sum=%d",sum);
getch();
}
Output:
Enter number 1: 4
Enter number 2: 5
Sum=9
Syntax#
void exit(int status)
Example#
exit(0)
The difference between break and exit() function is ,that break is a keyword, which
causes an immediate exit from the switch or loop (for, while or do), while exit() is
a standard library function, which terminates program execution when it is called.
break is a reserved word in C; therefore it can't be used as a variable name while
exit() can be used as a variable name.
Example#
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,j,no;
clrscr();
printf("\nEnter no =>");
www.shyamsir.com 78 74 39 11 91
Dr. Shyam N. Chawda, C Language Tutorial , 78 74 39 11 91
scanf("%d",&no);
if(no<0)
{
printf("\nBye");
exit(0);
}
else
{
printf("\nEntered no is positive");
}
getch();
}
Output
Enter no =>-9
Bye
www.shyamsir.com 78 74 39 11 91