Chapter 2
Control Structures
 Introduction
The statements which ‘control’ the flow of the execution,
are known as control statements.
There are 2 types of control statements
1.Decision making / Selection statements
2.Loop / Iterative statements
•Decision making / Selection statements
The decision making statements are
1.if statement
2.if –else statement
3.Nested if statement
4.if –else ladder
5.switch statements
2. The if….else Statement :
The if…else statement is an extension of the if
statement.
 It is generally used whenever you want to execute the
condition’s true and false part.
2. The if….else Statement :
Syntax :
if(expression)
{
true- block statements;
}
else
{
false – block statements;
}
false
expression
true
false block
statement
true block
statement
false
a>b
true
b is larger
a is larger
e.g. if(a>b)
printf(“a is larger”);
else
printf(“b is larger”);
3. Nested if…else statements:
 The if clause and the else part may contain a
compound statement.
Either or both may contain another if or if….else
statement. This is called the nesting of if…else statements.
Syntax: if(condition 1)
{ if(condition 2)
{
statement – 1;
}
else
{
statement – 2;
}
}
else
{
statement – 3;
}
E.g.
if(a>b)
{ if(a>c)
printf(“a is largest”);
else
printf(“c is largest”);
}
else
{
if(b>c)
printf(“b is largest”);
else
printf(“c is largest”);
}
4. The else – if ladder:
If there is an if else statement nested in each else of an if-
else construct, it is known as the else..if ladder.
Syntax:
if (condition 1)
statement – 1;
else if (condition 2)
statement – 2;
else if (condition 3)
statement – 3;
else
statement-4;
4. The else – if ladder:
The conditions are evaluated from the top to
downwards.
If none of the conditions are true, the final else will be
executed.
5. The Switch Statement :
When one of the many alternative is to be selected, we
can use an if statement to control the selection.
For this reason ‘C’ has a built in multiway decision
statement known as switch.
The switch statement tests the value of a given variable
against a list of case value and when a match is found, a
block of statement associated with that case is executed.
5. The Switch Statement :
Syntax:
switch(expression)
{
case value-1: block-1
break;
case value-2: block-2
break;
………..
default: default-block
}
The expression is an integer or character expression.
Value1, value2 are constants and are known as case label.
There is no need to put braces around these blocks.
break statement exit the control from the switch case
and transfer the control after the switch case statement.
The default is an optional case, when present, it will be
executed if the value of the expression does not match
with any of the case values.
Rules for switch statement:
•Case label must be integer or character.
•Case label must be unique. No two case labels can have
the same value.
•Case label must end with colon.
•The break statement is optional.
•The default label is optional.
•There can be at most one default label.
•The default may be placed any where but usually placed
at the end
•It is permitted to nest switch statement.
Write a program to enter a no between 0 to 9 and display
it in words using switch
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“n Enter no”);
scanf(“%d”,&n);
switch(n)
{
case 0 : printf(“ZERO”);
break;
case 1 : printf(“ONE”);
break;
case 2 : printf(“TWO”);
break;
.
Output
Enter no 5
FIVE
Write a menu driven program for add, sub, multi, div of
2 nos. using switch
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,opt;
printf(“n Enter 2 nos”);
scanf(“%d%d”,&a,&b);
printf(“n1.Addn2.Sub n3.Multin4.Div”);
printf(“n Enter your option”);
scanf(“%d”,&opt)
switch(opt)
{
case 1 :c=a+b;
printf(“nAdd =%d”,c);
break;
case 2 : c=a-b;
printf(“nSub=%d”,c);
break;
case 3 : c=a*b;
printf(“nMult=%d”,c);
break;
case 4 : c=a/b;
printf(“nDiv=%d”,c);
break;
default : printf(”nInvalid i/p”);
}
getch();} //main
Output
Enter 2 nos 5 10
1.Add
2.Sub
3.Mult
4.Div
Enter your option 3
Mult = 50
Example – 1 : Accept a year from the user. Find
out if the year entered is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{ int y;
printf("Enter the year : ");
scanf("%d",&y);
if(y%4= =0)
printf("%d is a leap year n“,y);
else
printf("%d is not a leap year n“,y);
getch();
}
Output:
Enter the year : 1992
1992 is a leap year
Example – 2 : Accept the selling price and cost price from
the user. Find out if the seller has made profit or loss.
Also calculate the loss incurred or profit made.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the cost price and selling price ");
scanf("%d%d",&a,&b);
if(b>a)
printf("Seller has made profit which is : %d“,b-a);
else if(a > b)
printf("Seller has incurred loss which is %d“,a-b);
else
printf("Seller has neither made profit nor lossn");
getch();
Output :
Enter the cost and
selling price : 54 75
Seller has made
profit which is : 21
Example – 3 : Write a program to accept 3 numbers and
find the largest of the 3 numbers
#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
printf("Enter 3 numbers : ");
scanf("%d%d%d",&a,&b,&c);
if(a>=b && a>=c)
printf("%d is largest n",a);
else if(b>=c && b>=a)
printf("%d is largest n",b);
else
printf("%d is largest n",c);
getch(); }//main
Output:
Enter 3 numbers : 12 28 57
57 is largest
Example – 4 : Write a program to check if character
entered is an alphabet, a number or a special character.
#include<stdio.h>
#include<conio.h>
void main()
{ char n;
clrscr();
printf("Enter a character : ");
scanf("%c",&n);
if(n>=65 && n<=90 || n>=97 && n<=122)
printf("%c is an alphabetn",n);
else if(n>=48 && n<=57)
printf("%c is a number n",n);
else
printf("%c is a special symbol“,n);
getch();}
Output:
Enter a character : H
H is an alphabet
Example – 5 : A company insures its drivers in the following cases If the driver is
married, If the driver is unmarried, male & above 30 years of age, If the driver is
unmarried, female & above 25 years of age. In all other cases the driver is not
insured. If the marital status, gender & age of driver is input write a program to
determine if the driver is insured or not.
void main()
{
int age;
char ms, gender;
clrscr();
printf("Marital status : (M/U)nGender : (M/F)nn");
printf("Enter the marital status , age & Gender of driver: ");
scanf("%c %d %c",&ms,&age,&gender);
if(ms == 'M' || (ms == 'U' && age>30 && gender =='M') ||
(ms == 'U' && age>25 && gender ==‘F'))
printf("The driver is insuredn");
else
printf("The driver is not insured");
getch();
}
Enter the marital status , age & gender of
driver: M 35 F
The driver is insured
Loop
• If we want to perform the same series of actions in the
same way ,more than once it will be done by using
“Loop”.
Loop
It is ability to perform a set of instructions repeatedly.
Two types of Loops
1)Top –Tested (Entry controlled loop )
2)Bottom-Tested (Exit controlled loop)
Entry controlled loop : In this looping the condition is
evaluated before the loop body is executed.
Exit controlled loop: In this looping the condition is
evaluated after the loop body is executed.
The c language provides three statements for performing
loop operation.
•The while statement
•The do statement
•The for statement.
While Loop
It is often used when the number of times the loop is to
be executed is not known in advance but depends on
the test condition.
The while is an entry controlled loop statement.
Syntax :
while(test condition)
{
body of the loop;
}
While Loop
•The condition is evaluated and the statement (loop
body )is executed as long as the expression is TRUE
•Since it is an entry controlled loop, if the expression
evaluate to false the first time itself, the loop body will
not be executed even once.
While Loop
e.g.
int count = 1;
while (count <= 5)
{
printf(“n%d”,count);
count++;
}
Points to remember
1.The loop control variable must be initialized.
2.The loop body must contain a statement to alter
the value of the control variable.
Output
1 ,2,3,4,5
Nested While Loop
•Similar to nested if statements, loops can be nested as
well.
•That is, the body of a loop can contain another loop.
•For each iteration of the outer loop, the inner loop
iterates completely.
Nested While Loop
Syntax :
while(exp1) --------->Outer Loop
{
while(exp2) --------->Inner Loop
{
loop body of while (exp2);
}
}
Nested While Loop
•How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
printf("Here");
count2++;
}
count1++;
}
OUTPUT
“Here” will be printed 10 *
20 = 200 times
The do-while Statement
•It is a bottom tested loop i.e. it evaluates the condition
after the execution of loop body.
•This means that the statements within the loop are
executed at least once.
•Syntax:
do
{
loop body;
}while(condition);
Comparing while and do while loop
While Loop
•It is a top tested loop
•Condition is evaluated
before the executing loop
body.
•If the condition is false
very first time, the loop
body will never executed.
Do while Loop
•It is a bottom tested
loop
•Condition is evaluated
after the execution of
loop body.
•The loop body is
executed at least once.
The for loop
•It is very flexible, powerful and most commonly used
loop in C. It is useful when the number of iterations
are known in advance.
•A for statement has the following syntax:
Syntax :
for( initialization; test-condition ; increment or
decrement)
{
Body of the loop;
}
The for Statement
•A for loop is functionally equivalent to the following
while loop structure:
initialization;
while(condition)
{
statement;
increment/decrement;
}
The for Statement
•The increment section can perform any calculation
int num;
for (num=30; num > 0; num -= 5)
{
printf(“%d”, num);
}
Output : 30 25 20 15 10 5
Nested For Loop
•Nested loops consist of an outer loop with one or more
inner loops.
•e.g.,
for (i=1;i<=100;i++)
{
for(j=1;j<=50;j++)
{
…
}
}
•The above loops will be executed for 100*50 times.
Inner loop
Outer loop
loop
/* Program to display the output on the screen */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the value of n :");
scanf("%d",&n);
for(i=0;i<n;i++) --------> OUTER LOOP
{
for(j=0;j<=i;j++) --------> INNER LOOP
printf("*");
printf("n");
}
getch();
}
OUTPUT
OUPUT
Enter the value of n : 4
*
**
***
****
/* Program to Display 0 to 9 */
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
for(x=0; x<=9 ; x++)
{
printf(“%d”,x);
}
printf(“n”);
getch();
}
OUTPUT
0 1 2 3 4 5
6 7 8 9
Jumps in Loops :
There are four statements which are used to perform
jumps in loops – break, continue, goto, exit().
break statement :
When the break statement is encountered inside a loop,
the loop is immediately exited and the program continues
with the statement immediately following the loop.
Syntax :
break;
•Break in do while loop
while (……..)
{
………………..
………………..
if (condition)
break;
………………
}
Exit
loop
do
{
………………..
………………..
if (condition)
break;
………………
} while(….) ;
Exit
loop
• Break in while loop
•Break in nested for loop
for (……..)
{
………………..
if (error)
break;
………………
}
for (……..)
{ for (……….)
{
if (condition)
break;
}
----------
}
Exit
loop
Exit
loop
•Break in for loop
e.g.
count=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf(“nEnter number”);
scanf(“%d”,&n);
if(n<0)
break;
}//inner for
count++;
}//outer for
Output
Enter number
-3
No output
Control goes to
outside.
continue statement :
The use of the continue statement is to skip the
statements which are followed by ‘continue’ and continue
with the next iteration of the loop.
Syntax : continue;
continue in while loop continue in do while loop
while (……..)
{
………………..
………………..
if (condition)
continue;
………………
}
do
{
………………..
………………..
if (condition)
continue;
………………
} while(….) ;
Continue in for loop Continue in nested for loop
for (……..)
{
………………..
………………..
if (error)
continue;
………………
}
for (……..)
{
for (……….)
{
if (condition)
continue;
---------------
}
----------
}
e.g.
do
{
printf(“Enter a number”);
scanf(“%d”,&n);
if(n<0)
continue;
sum=sum+n;
}while(n!=999);
This code accepts integers and calculates sum of only
positive numbers. The loop terminates when user enters
999.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int count,negative;
float no,sqroot;
printf("Enter 9999 To Stopn");
count=0;
negative=0;
while(count<=20)
{ printf("nEnter a number :");
scanf("%f",&no);
if(no==9999)
break; //break statement
if( no < 0 )
{
printf("nNumber is negative");
negative++;
continue; // continue statement
}
sqroot = sqrt(no);
printf("Numer = %fn Square root = %fnn",no,sqroot);
count++;
}//while
printf("nPositive Numbers = %dn",count);
printf("nNegative Numbers = %dn",negative);
}
goto Statement :
C support go to statement to jump unconditionally from
one point to another in the program.
Syntax: goto label; label:
…….. Statement;
…….. ……….
…….. ……….
label: ……….
Statement; goto
label;
[Forword Jump] [Backward Jump]
goto Statement :
Backward Jump.
If a label is before the statement goto, a loop will be
formed and some statements will be executed repeatedly,
such a jump is known as Backward Jump.
Forward Jump:
If the label is placed after the goto label, some statements
will be skipped and the jump is known as a forward jump.
e.g. x=1;
loop:
x++;
if(x<100)
goto loop;
Using exit() function
The exit() function causes immediate termination of the
entire program.
e.g.
void main()
{
int code;
printf(“n enter the security code”);
scanf(“%d”,&code);
if(code<0)
exit(0);
----------
}
Example – 1 : Program to find factorial of given no::
void main()
{
int i,n;
long fact;
clrscr();
printf(“n Enter no”);
scanf(“%d”,&n);
fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
printf("n%d!=%dn",n,fact);
getch();
}
Let n=5
OUTPUT
5!=120
Example – 2 : Two numbers are entered through the keyboard.
Write a program to find the value of one number raised to the power
of other.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ans,i;
clrscr();
printf("Enter the 2 numbers : ");
scanf("%d%d",&a,&b);
ans=1;
for(i=0;i<b;i++)
ans=ans*a;
printf("n%d^%d is %dn",a,b,ans);
getch(); }//main
Output:
Enter the 2
numbers :2 3
2^3 is 8
Example – 3 : Write a program to read a no. and check
if it is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,r;
int rev=0;
printf("Enter a number : ");
scanf("%d",&n);
temp=n;
while(n != 0)
{ r= n%10;
rev = rev*10 + r;
n/=10;
}
if(rev = = temp)
printf("%d is Palindrome n",temp);
else
printf(“%d is not Palindromen”,temp);
getch();
}
Output:
Enter a number : 121
121 is Palindrome
Example – 5 : Accept a number through the keyboard. Display whether
the given number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{ int i,num,r;
clrscr();
printf(" Enter the number : ");
scanf("%d",&num);
for(i=2;i<=num /2;i++)
{ r = num % i;
if(r = = 0)
{
printf(" %d is not a prime no",num);
getch();
exit(0);
}
}
printf(" %d is a prime number ",num);
getch();
}
Output:
Enter the number :
121
121 is not a prime no

Chapter2. control structure in programming with c

  • 1.
    Chapter 2 Control Structures Introduction The statements which ‘control’ the flow of the execution, are known as control statements. There are 2 types of control statements 1.Decision making / Selection statements 2.Loop / Iterative statements
  • 2.
    •Decision making /Selection statements The decision making statements are 1.if statement 2.if –else statement 3.Nested if statement 4.if –else ladder 5.switch statements
  • 3.
    2. The if….elseStatement : The if…else statement is an extension of the if statement.  It is generally used whenever you want to execute the condition’s true and false part.
  • 4.
    2. The if….elseStatement : Syntax : if(expression) { true- block statements; } else { false – block statements; }
  • 5.
  • 6.
    false a>b true b is larger ais larger e.g. if(a>b) printf(“a is larger”); else printf(“b is larger”);
  • 7.
    3. Nested if…elsestatements:  The if clause and the else part may contain a compound statement. Either or both may contain another if or if….else statement. This is called the nesting of if…else statements.
  • 8.
    Syntax: if(condition 1) {if(condition 2) { statement – 1; } else { statement – 2; } } else { statement – 3; }
  • 9.
    E.g. if(a>b) { if(a>c) printf(“a islargest”); else printf(“c is largest”); } else { if(b>c) printf(“b is largest”); else printf(“c is largest”); }
  • 10.
    4. The else– if ladder: If there is an if else statement nested in each else of an if- else construct, it is known as the else..if ladder. Syntax: if (condition 1) statement – 1; else if (condition 2) statement – 2; else if (condition 3) statement – 3; else statement-4;
  • 11.
    4. The else– if ladder: The conditions are evaluated from the top to downwards. If none of the conditions are true, the final else will be executed.
  • 12.
    5. The SwitchStatement : When one of the many alternative is to be selected, we can use an if statement to control the selection. For this reason ‘C’ has a built in multiway decision statement known as switch. The switch statement tests the value of a given variable against a list of case value and when a match is found, a block of statement associated with that case is executed.
  • 13.
    5. The SwitchStatement : Syntax: switch(expression) { case value-1: block-1 break; case value-2: block-2 break; ……….. default: default-block }
  • 14.
    The expression isan integer or character expression. Value1, value2 are constants and are known as case label. There is no need to put braces around these blocks. break statement exit the control from the switch case and transfer the control after the switch case statement. The default is an optional case, when present, it will be executed if the value of the expression does not match with any of the case values.
  • 15.
    Rules for switchstatement: •Case label must be integer or character. •Case label must be unique. No two case labels can have the same value. •Case label must end with colon. •The break statement is optional. •The default label is optional. •There can be at most one default label. •The default may be placed any where but usually placed at the end •It is permitted to nest switch statement.
  • 16.
    Write a programto enter a no between 0 to 9 and display it in words using switch #include<stdio.h> #include<conio.h> void main() { int n; printf(“n Enter no”); scanf(“%d”,&n); switch(n) { case 0 : printf(“ZERO”); break; case 1 : printf(“ONE”); break; case 2 : printf(“TWO”); break; . Output Enter no 5 FIVE
  • 17.
    Write a menudriven program for add, sub, multi, div of 2 nos. using switch #include<stdio.h> #include<conio.h> void main() { int a,b,c,opt; printf(“n Enter 2 nos”); scanf(“%d%d”,&a,&b); printf(“n1.Addn2.Sub n3.Multin4.Div”); printf(“n Enter your option”); scanf(“%d”,&opt)
  • 18.
    switch(opt) { case 1 :c=a+b; printf(“nAdd=%d”,c); break; case 2 : c=a-b; printf(“nSub=%d”,c); break; case 3 : c=a*b; printf(“nMult=%d”,c); break; case 4 : c=a/b; printf(“nDiv=%d”,c); break; default : printf(”nInvalid i/p”); } getch();} //main Output Enter 2 nos 5 10 1.Add 2.Sub 3.Mult 4.Div Enter your option 3 Mult = 50
  • 19.
    Example – 1: Accept a year from the user. Find out if the year entered is leap year or not. #include<stdio.h> #include<conio.h> void main() { int y; printf("Enter the year : "); scanf("%d",&y); if(y%4= =0) printf("%d is a leap year n“,y); else printf("%d is not a leap year n“,y); getch(); } Output: Enter the year : 1992 1992 is a leap year
  • 20.
    Example – 2: Accept the selling price and cost price from the user. Find out if the seller has made profit or loss. Also calculate the loss incurred or profit made. #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the cost price and selling price "); scanf("%d%d",&a,&b); if(b>a) printf("Seller has made profit which is : %d“,b-a); else if(a > b) printf("Seller has incurred loss which is %d“,a-b); else printf("Seller has neither made profit nor lossn"); getch(); Output : Enter the cost and selling price : 54 75 Seller has made profit which is : 21
  • 21.
    Example – 3: Write a program to accept 3 numbers and find the largest of the 3 numbers #include<stdio.h> #include<conio.h> void main() { int a,b,c; printf("Enter 3 numbers : "); scanf("%d%d%d",&a,&b,&c); if(a>=b && a>=c) printf("%d is largest n",a); else if(b>=c && b>=a) printf("%d is largest n",b); else printf("%d is largest n",c); getch(); }//main Output: Enter 3 numbers : 12 28 57 57 is largest
  • 22.
    Example – 4: Write a program to check if character entered is an alphabet, a number or a special character. #include<stdio.h> #include<conio.h> void main() { char n; clrscr(); printf("Enter a character : "); scanf("%c",&n); if(n>=65 && n<=90 || n>=97 && n<=122) printf("%c is an alphabetn",n); else if(n>=48 && n<=57) printf("%c is a number n",n); else printf("%c is a special symbol“,n); getch();} Output: Enter a character : H H is an alphabet
  • 23.
    Example – 5: A company insures its drivers in the following cases If the driver is married, If the driver is unmarried, male & above 30 years of age, If the driver is unmarried, female & above 25 years of age. In all other cases the driver is not insured. If the marital status, gender & age of driver is input write a program to determine if the driver is insured or not. void main() { int age; char ms, gender; clrscr(); printf("Marital status : (M/U)nGender : (M/F)nn"); printf("Enter the marital status , age & Gender of driver: "); scanf("%c %d %c",&ms,&age,&gender); if(ms == 'M' || (ms == 'U' && age>30 && gender =='M') || (ms == 'U' && age>25 && gender ==‘F')) printf("The driver is insuredn"); else printf("The driver is not insured"); getch(); } Enter the marital status , age & gender of driver: M 35 F The driver is insured
  • 24.
    Loop • If wewant to perform the same series of actions in the same way ,more than once it will be done by using “Loop”. Loop It is ability to perform a set of instructions repeatedly.
  • 25.
    Two types ofLoops 1)Top –Tested (Entry controlled loop ) 2)Bottom-Tested (Exit controlled loop) Entry controlled loop : In this looping the condition is evaluated before the loop body is executed. Exit controlled loop: In this looping the condition is evaluated after the loop body is executed. The c language provides three statements for performing loop operation. •The while statement •The do statement •The for statement.
  • 26.
    While Loop It isoften used when the number of times the loop is to be executed is not known in advance but depends on the test condition. The while is an entry controlled loop statement. Syntax : while(test condition) { body of the loop; }
  • 27.
    While Loop •The conditionis evaluated and the statement (loop body )is executed as long as the expression is TRUE •Since it is an entry controlled loop, if the expression evaluate to false the first time itself, the loop body will not be executed even once.
  • 28.
    While Loop e.g. int count= 1; while (count <= 5) { printf(“n%d”,count); count++; } Points to remember 1.The loop control variable must be initialized. 2.The loop body must contain a statement to alter the value of the control variable. Output 1 ,2,3,4,5
  • 29.
    Nested While Loop •Similarto nested if statements, loops can be nested as well. •That is, the body of a loop can contain another loop. •For each iteration of the outer loop, the inner loop iterates completely.
  • 30.
    Nested While Loop Syntax: while(exp1) --------->Outer Loop { while(exp2) --------->Inner Loop { loop body of while (exp2); } }
  • 31.
    Nested While Loop •Howmany times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { printf("Here"); count2++; } count1++; } OUTPUT “Here” will be printed 10 * 20 = 200 times
  • 32.
    The do-while Statement •Itis a bottom tested loop i.e. it evaluates the condition after the execution of loop body. •This means that the statements within the loop are executed at least once. •Syntax: do { loop body; }while(condition);
  • 33.
    Comparing while anddo while loop While Loop •It is a top tested loop •Condition is evaluated before the executing loop body. •If the condition is false very first time, the loop body will never executed. Do while Loop •It is a bottom tested loop •Condition is evaluated after the execution of loop body. •The loop body is executed at least once.
  • 34.
    The for loop •Itis very flexible, powerful and most commonly used loop in C. It is useful when the number of iterations are known in advance. •A for statement has the following syntax: Syntax : for( initialization; test-condition ; increment or decrement) { Body of the loop; }
  • 35.
    The for Statement •Afor loop is functionally equivalent to the following while loop structure: initialization; while(condition) { statement; increment/decrement; }
  • 36.
    The for Statement •Theincrement section can perform any calculation int num; for (num=30; num > 0; num -= 5) { printf(“%d”, num); } Output : 30 25 20 15 10 5
  • 37.
    Nested For Loop •Nestedloops consist of an outer loop with one or more inner loops. •e.g., for (i=1;i<=100;i++) { for(j=1;j<=50;j++) { … } } •The above loops will be executed for 100*50 times. Inner loop Outer loop loop
  • 38.
    /* Program todisplay the output on the screen */ #include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("Enter the value of n :"); scanf("%d",&n); for(i=0;i<n;i++) --------> OUTER LOOP { for(j=0;j<=i;j++) --------> INNER LOOP printf("*"); printf("n"); } getch(); } OUTPUT OUPUT Enter the value of n : 4 * ** *** ****
  • 39.
    /* Program toDisplay 0 to 9 */ #include<stdio.h> #include<conio.h> void main() { int x; for(x=0; x<=9 ; x++) { printf(“%d”,x); } printf(“n”); getch(); } OUTPUT 0 1 2 3 4 5 6 7 8 9
  • 40.
    Jumps in Loops: There are four statements which are used to perform jumps in loops – break, continue, goto, exit(). break statement : When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. Syntax : break;
  • 41.
    •Break in dowhile loop while (……..) { ……………….. ……………….. if (condition) break; ……………… } Exit loop do { ……………….. ……………….. if (condition) break; ……………… } while(….) ; Exit loop • Break in while loop
  • 42.
    •Break in nestedfor loop for (……..) { ……………….. if (error) break; ……………… } for (……..) { for (……….) { if (condition) break; } ---------- } Exit loop Exit loop •Break in for loop
  • 43.
  • 44.
    continue statement : Theuse of the continue statement is to skip the statements which are followed by ‘continue’ and continue with the next iteration of the loop. Syntax : continue;
  • 45.
    continue in whileloop continue in do while loop while (……..) { ……………….. ……………….. if (condition) continue; ……………… } do { ……………….. ……………….. if (condition) continue; ……………… } while(….) ;
  • 46.
    Continue in forloop Continue in nested for loop for (……..) { ……………….. ……………….. if (error) continue; ……………… } for (……..) { for (……….) { if (condition) continue; --------------- } ---------- }
  • 47.
    e.g. do { printf(“Enter a number”); scanf(“%d”,&n); if(n<0) continue; sum=sum+n; }while(n!=999); Thiscode accepts integers and calculates sum of only positive numbers. The loop terminates when user enters 999.
  • 48.
    #include<stdio.h> #include<conio.h> #include<math.h> void main() { int count,negative; floatno,sqroot; printf("Enter 9999 To Stopn"); count=0; negative=0; while(count<=20) { printf("nEnter a number :"); scanf("%f",&no); if(no==9999) break; //break statement
  • 49.
    if( no <0 ) { printf("nNumber is negative"); negative++; continue; // continue statement } sqroot = sqrt(no); printf("Numer = %fn Square root = %fnn",no,sqroot); count++; }//while printf("nPositive Numbers = %dn",count); printf("nNegative Numbers = %dn",negative); }
  • 50.
    goto Statement : Csupport go to statement to jump unconditionally from one point to another in the program. Syntax: goto label; label: …….. Statement; …….. ………. …….. ………. label: ………. Statement; goto label; [Forword Jump] [Backward Jump]
  • 51.
    goto Statement : BackwardJump. If a label is before the statement goto, a loop will be formed and some statements will be executed repeatedly, such a jump is known as Backward Jump. Forward Jump: If the label is placed after the goto label, some statements will be skipped and the jump is known as a forward jump. e.g. x=1; loop: x++; if(x<100) goto loop;
  • 52.
    Using exit() function Theexit() function causes immediate termination of the entire program. e.g. void main() { int code; printf(“n enter the security code”); scanf(“%d”,&code); if(code<0) exit(0); ---------- }
  • 53.
    Example – 1: Program to find factorial of given no:: void main() { int i,n; long fact; clrscr(); printf(“n Enter no”); scanf(“%d”,&n); fact=1; for(i=1;i<=n;i++) fact=fact*i; printf("n%d!=%dn",n,fact); getch(); } Let n=5 OUTPUT 5!=120
  • 54.
    Example – 2: Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of other. #include<stdio.h> #include<conio.h> void main() { int a,b,ans,i; clrscr(); printf("Enter the 2 numbers : "); scanf("%d%d",&a,&b); ans=1; for(i=0;i<b;i++) ans=ans*a; printf("n%d^%d is %dn",a,b,ans); getch(); }//main Output: Enter the 2 numbers :2 3 2^3 is 8
  • 55.
    Example – 3: Write a program to read a no. and check if it is palindrome or not. #include<stdio.h> #include<conio.h> void main() { int n,temp,r; int rev=0; printf("Enter a number : "); scanf("%d",&n); temp=n; while(n != 0) { r= n%10; rev = rev*10 + r; n/=10; }
  • 56.
    if(rev = =temp) printf("%d is Palindrome n",temp); else printf(“%d is not Palindromen”,temp); getch(); } Output: Enter a number : 121 121 is Palindrome
  • 57.
    Example – 5: Accept a number through the keyboard. Display whether the given number is prime or not. #include<stdio.h> #include<conio.h> void main() { int i,num,r; clrscr(); printf(" Enter the number : "); scanf("%d",&num); for(i=2;i<=num /2;i++) { r = num % i; if(r = = 0) { printf(" %d is not a prime no",num); getch(); exit(0); } }
  • 58.
    printf(" %d isa prime number ",num); getch(); } Output: Enter the number : 121 121 is not a prime no