SlideShare a Scribd company logo
CONTROL STRUCTURES IN
‘C’
2
 Conditional operators in details
 Decision making structures like IF, IF-ELSE
AND SWITCH
 Loop control structures like FOR, WHILE, DO-
WHILE
 Implementation of break and continue.
 Nested structures.
 Unconditional branching (GOTO statement).
3
INTRODUCTION:-
 Normally the statement of a program are
executed from first to last.
 Sometimes it is necessary to alter the sequence of
execution of statements.
 Based on certain conditions or we may require
some statements to be executed repeatedly until
some condition is satisfied.
 This involves decision making and looping.
4
THREE MECHANISM ARE:-
 if statement:Many program we require testing
of some condition at some point in the program
and take action.
Syntax: if(condition)
{
Statements;
}
Expressio
n
statements
F
T
5
 if-else statement:Many program require testing of
some condition at some point in the program and
selecting one of the alternative at some point and
selecting one of the alternative path depending upon
the result is known as branching.
 General Form is:
 if(expression)
 statement1 ;
 else
 statement 2;
Expressi
on
statement1 statement2
T F
6
 Ex. Check given number is even or odd. Example of if-else
statement
 Solution:
 #include<stdio.h>
 main( )
 {
 int no;
 printf(“n Enter any number”);
 scanf(“%d”,&no);
 if (no %2==0)
 {
 printf(“ number=%d is even number”,no);
 }
 else
 {
 printf(“n number =%d is odd number”,no);
 }
 }//main
7
NESTED IF–ELSE STATEMENT
 NESTED “IF-ELSE” STATEMENTS:-
 Nested “if-else” statements, the if or the else can
contain another if or if-else statement. This provides a
programmer with a lot of flexibility in programming.
Nesting could take one of several form as illustrated as
below:

 1] if statement inside else:-
 Syntax: if(expression)
 Statement1;
 else
 if(expression2)
 Statement2;
8
 if- else statement inside if:-
Syntax:-if(expression 1)
 {
if(expression 2)
statement 1;
else
statement 2;
}
else
statements 3;

9
 4] if-else statement inside if-else:-
 Syntax:-if(expression1)
 {if(expression2)
 statement1;
 else
 statement 2;}
 else
 if(expression3)
 statement3;
 else
 statement 4;
 Eg Write a c program to accept 3 no from user and check greater no.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int no1, no2, no3;
 clrscr();
 printf(“n Ennter any 3 nos:”);
 scanf(“%d%d%d”,&no1,&no2,&no3);
 if(no1>no2)
 if(no1>no3)
 printf(“n no1 is greater number”);

10
 5]else-if ladder:-
 If there is if-else statement nested in each else
of an if-else construct, that is called as else-if ladder.
 Syntax: if(expression1)
 statement 1;
 else
 if(expression 2)
 statement2;
 else
 if(expression3)
 statement 3;
 else
 statement 4;
11
 Conditional Branching switch Statement:
Whenever we want to make decision among
multiple alternatives nested else-if statement can
be used, however structure become very
complicated and code become difficult to read and
trace. For this reason C has a build-in multi
branch decision statement called switch.
case 1 value 1
switch(expr) case 2 value 2
case n value n
 default

12
RULES:-
 Syntax:
 switch(expression)
 {
 case value1: Statements;
 break;
 case value 2: Statements;
 break;
 ….
 case value n: Statements;
 break;
 default : Statements;
 }
 The expression enclosed within parenthesis is successively compare
against the constant expression(value)in each case. They called case
labeled and must be end with colon( : ).
 The statement in each case may contain zero or more statements. If there
are multiple statement for a case they need not be enclosed in bracket.
 All case expression must be different.
 The case default is executed if non of the other case match.
13
NESTED SWITCH STATEMENT:-
 It is possible to have a switch statement
as a part of statement is another switch
statement then it is called as Nested
switch statement.
 Even if the case constant of the inner and
outer switch contain common values there
is not conflict.(mix with each other)
14
 Ex.
 #include<stdio.h>
 main()
 {
 int x,y;
 printf(“n enter 2 binary values”);
 scanf(“%d%d”,&x,&y);
 switch(x)
 {
 case 0:printf(“n Invalid Values”);
 break;
 case 1: switch(y)
 {
 case 0: printf(“n Values are 1 & 0”);
 break;
 case 1:printf(“n Values are 1 & 1”);
 break;
 }// inner switch
 break;
 } //outer switch
 }//main
15
LOOP CONTROL STRUCTURE:-
 A block of program statements that is executed
repeatedly is called a loop. The repetition is done
until some condition for termination of the loop is
satisfied .
 A loop structure essentially contains a test
condition.
1]A test condition determines the number of times
the loop body is executed.
2]Loop statements- If the test condition evaluate to
true then statement within the loop body get
executed otherwise control transfer outside the
loop i.e loop termination.
initially contains a test condition.
16
 1) Top –Tested Loop:
 An Entry Controlled loop , the condition is evaluated before the loop
body is executed. Ex.while,for
Test
Condition
Loop Body Statement
False
True
17
 2) Bottom tested loop(exit controlled loop).
 In exit controlled loop, the condition is tested after
the loop body executed.
 Ex.do-while
Loop Body
Test
Condition
True
False
18
ITERATION PROCEDURE:-
 The iteration procedure takes place in 4 steps
1. Initializing the loop control variable.
2. Execution of loop statement.
3. Changing the value of the control variable.
4. Testing the condition.
5. The C language provides 3 loops:-
6. 1) while
7. 2) do-while
8. 3) for
19
LOOPS:-
 C LANGUAGE PROVIDES 3 LOOP
STRUCTURES:-
1) While: It is often used when the no. of time the
loop is to be executed is not known in advanced
but depends on the test condition.ENTRY
CONTOL LOOP
2) do-while: do-while loop is a bottom tested or
exit controlled loop i.e it evaluates the condition
after the execution of statement in it construct.
This means that the statements within the loop
are executed at least once if the condition is false.
3) for: for loop is very flexible, powerful and most
commonly used loop in C. It is useful when the
member of repetition is known in advance. This is
a top-tested loop similarly to the while loop.
20
1] While:-
 It is often used when the no. of time the
loop is to be executed is not known in
advanced but depends on the test condition.
 Syntax:- while(expression)
 {
 statements;
 }
That expression is a test condition and can
be any valid c expression. The statement can
be a single or set of statement.
21
 2) The do-while loop
 Iterative statement means loop control statement.
 do-while loop, is a bottom-tested or exit control
loop i.e. It evaluates the condition after the
execution of statements in its.
 This means that the statements within the loop
are executed at-least once, when condition is false.
 Syntax:
 do
 {
 statements;
 }while(expression);

22
 3] for loop:-
 For loop is very flexible, powerful and most
commonly used loop in C . It is useful when the
member of repetition is known in advance.
 This is a top-tested loop similarly to the while loop.
 Syntax:-
 for(exp1;exp2;exp3) ex. for( i=1;i<=10;i++)
 {
 statement;
 }
 Where, expr1 is to used to initialization of expression.
 expr2 is used to test condition .
 expr3 is used to update expression.
 these 3 expression is separated by ‘ ; ’.
23
 Eg. Write a program to print first 10 no’s
using for loop
 #include<stdio.h>
 void main()
 {
 int i;
 for(i=1;i<=10;i=i+1)
 {
 printf(“%d”,i);
 }
 }
24
 Print table for the given number using C for loop
 #include<stdio.h>
 int main()
 {
 int i=1,number=0;
 printf("Enter a number: ");
 scanf("%d",&number);
 for(i=1;i<=10;i++)
 {
 printf("%d n",(number*i));
 }
 return 0;
 }
25
 #include <stdio.h>
 int main()
 {
 int i,j,k;
 for(i=0,j=0,k=0;i<4,k<8,j<10;i++)
 {
 printf("%d %d %dn",i,j,k);
 j+=2;
 k+=3;
 }
 }
0 0 0
1 2 3
2 4 6
3 6 9
4 8 12
26
 #include <stdio.h>
 int main()
 {
 int i;
 for(i=0;;i++)
 {
 printf("%d",i);
 }
 }
27
1. #include<stdio.h>
2. void main ()
3. {
4. int i=0,j=2;
5. for(i = 0;i<5;i++,j=j+2)
6. {
7. printf("%d %dn",i,j);
8. }
9. }
0 2 1
4 2 6
3 8 4
10
28
NESTED LOOP:-
 Loop can also be nested ,Nesting of loops means a loop i.e contain within another loop. Any loop can be nested
within any other loop.
 Eg:
 1) while(expr1)
 {
 while(expr2)
 {
 loop body of inner while
 }
 }
 2) while(expr1)
 {
 for( )
 {
 loop body of for
 }
 }
 3) for( )
 {
 …….
 for( )
 {
 statements;
 }
 }

29
 #include <stdio.h>
 int main()
 {
 int n;// variable declaration
 printf("Enter the value of n :");
 // Displaying the n tables.
 for(int i=1;i<=n;i++) // outer loop
 {
 for(int j=1;j<=10;j++) // inner loop
 {
 printf("%dt",(i*j)); // printing the value.
 }
 printf("n");
 }
30
 #include <stdio.h>
 int main()
 {
 int rows; // variable declaration
 int columns; // variable declaration
 int k=1; // variable initialization
 printf("Enter the number of rows :"); // input the number of rows.
 scanf("%d",&rows);
 printf("nEnter the number of columns :"); // input the number of columns.
 scanf("%d",&columns);
 int a[rows][columns]; //2d array declaration
 int i=1;
 while(i<=rows) // outer loop
 {
 int j=1;
 while(j<=columns) // inner loop
 {
 printf("%dt",k); // printing the value of k.
 k++; // increment counter
 j++;
 }
 i++;
 printf("n");
 }
 }
31
32
JUMP STATEMENT:-
 In many cases we want to stop the execution loop
before it ends or transfer the control to some
other part of a program. This can be done using
jump statement.
C supports 4 jump statements
1. break
2. continue
3. return
4. Goto
5. exit
33

1] break:-
 Sometime it is require to exit a loop as soon as a
certain condition is made.
 To skip all subsequent statement of loop body and to come
out of the loop.
 When the break statement is encounter inside a loop, the
loop is immediately terminate subsequent statement are
skipped and program control transfer at the next
statement of the following loop.
34
35
36
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
37
 #include <stdio.h>

 int main(){

 /* local variable definition */
 char ch = 'm';
 printf("Time code: %cnn", ch);

 switch(ch) {

 case 'm':
 printf("Good Morning n");
 break;

 case 'a':
 printf("Good Afternoon n");
 break;

 case 'e':
 printf("Good Evening n");
 break;

 default:
 printf("Hello");
 }
 }
38
 2]continue:-
 Continue statement is used to transfer control
to the beginning of the loop.
 It remaining statement and it forces the next
interaction of the loop to takes place as usual.
 Syntax:- continue;
39
 while (expr){
 . . .
 . . .
 if (condition)
 continue;
 . . .
 }
40
41
 In this program the loop generates 1 to 10 values of the variable "i".
 Whenever it is an even number, the next iteration starts, skipping the printf
statement. Only the odd numbers are printed.
 #include <stdio.h>
 int main(){
 int i = 0;

 while (i < 10){
 i++;
 if(i%2 == 0)
 continue;
 printf("i: %dn", i);

 }
 }
 Output
 i: 1
 i: 3
 i: 5
 i: 7
 i: 9
42
 3] goto Statement:-
 goto statement is an unconditional jump
statement.
 Goto statement is use to alter the normal sequence of
program execution by unconditionally transferring by
control some other part of program.
 Syntax: goto label;
 The statement where control has to be transfer is
identified by the label.
 A label is valid C identifier.
 The label followed by colon “:” .
 The label can attached to any statement in the same
function as the goto.
 The label does not has to be declared like other identifier.
43
44
 Ex.:
 for(….)
 {
 for(…)
 { while( ..)
 {
 …..
 if( )
 goto out;
 …..
 }
 }
 }
 out:
 ….
 ….
45
 #include <stdio.h>

 int main (){
 int n = 0;

 if (n == 0)
 goto end;
 printf("The number is: %d", n);

 end:
 printf ("End of program");

 return 0;
 }
46
 #include <stdio.h>

 int main (){
 START:
 printf("Hello World n");
 printf("How are you? n");
 goto START;
 return 0;
 }
 Output:
 Hello World
 How are you?
 .......
 .......
 The program prints the two strings continuously until
forcibly stopped.
47
 4] exit():-
 The exit(0) function causes indicate
termination of the entire program .the exit
function is called with an arguments’0’ to
indicate that termination is normal.

 Eg. Invalid password enter that time use exit(0).
48
49
THANK YOU

More Related Content

Similar to C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH) (20)

PPT
control-statements, control-statements, control statement
crrpavankumar
 
PPT
control-statements....ppt - definition
Papitha7
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PPTX
Condition Stmt n Looping stmt.pptx
Likhil181
 
PPTX
Control Structures in C
sana shaikh
 
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
PPTX
Control Flow Statements
Tarun Sharma
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PDF
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PPTX
C Programming - Decision making, Looping
MURALIDHAR R
 
PDF
3 flow
suresh rathod
 
PDF
3 flow
suresh rathod
 
PDF
Bt0067 c programming and data structures 1
Techglyphs
 
PDF
Controls & Loops in C
Thesis Scientist Private Limited
 
PPTX
computer programming and utilization
JAYDEV PATEL
 
PDF
Loop and while Loop
JayBhavsar68
 
PPTX
Decision Making and Looping
Munazza-Mah-Jabeen
 
DOC
3. control statement
Shankar Gangaju
 
control-statements, control-statements, control statement
crrpavankumar
 
control-statements....ppt - definition
Papitha7
 
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Condition Stmt n Looping stmt.pptx
Likhil181
 
Control Structures in C
sana shaikh
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Control Flow Statements
Tarun Sharma
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
C Programming: Control Structure
Sokngim Sa
 
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
C Programming - Decision making, Looping
MURALIDHAR R
 
Bt0067 c programming and data structures 1
Techglyphs
 
Controls & Loops in C
Thesis Scientist Private Limited
 
computer programming and utilization
JAYDEV PATEL
 
Loop and while Loop
JayBhavsar68
 
Decision Making and Looping
Munazza-Mah-Jabeen
 
3. control statement
Shankar Gangaju
 

Recently uploaded (20)

PDF
Plant growth promoting bacterial non symbiotic
psuvethapalani
 
PDF
A Man of the Forest: The Contributions of Gifford Pinchot
RowanSales
 
PDF
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
PDF
Carbonate formation and fluctuating habitability on Mars
Sérgio Sacani
 
DOCX
Paper - Taboo Language (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
PPTX
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
PDF
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
PDF
Annual report 2024 - Inria - English version.pdf
Inria
 
PPTX
Congestive Heart failure for pharmacology topic hehe
oceangunnu
 
PDF
The emergence of galactic thin and thick discs across cosmic history
Sérgio Sacani
 
PPTX
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
DOCX
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
PPTX
Cerebellum_ Parts_Structure_Function.pptx
muralinath2
 
PPTX
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
PDF
Integrating Lifestyle Data into Personalized Health Solutions (www.kiu.ac.ug)
publication11
 
PPTX
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
PDF
20250603 Recycling 4.pdf . Rice flour, aluminium, hydrogen, paper, cardboard.
Sharon Liu
 
PPTX
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
PPTX
Q1 - W1 - D2 - Models of matter for science.pptx
RyanCudal3
 
PPTX
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
Plant growth promoting bacterial non symbiotic
psuvethapalani
 
A Man of the Forest: The Contributions of Gifford Pinchot
RowanSales
 
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
Carbonate formation and fluctuating habitability on Mars
Sérgio Sacani
 
Paper - Taboo Language (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
Annual report 2024 - Inria - English version.pdf
Inria
 
Congestive Heart failure for pharmacology topic hehe
oceangunnu
 
The emergence of galactic thin and thick discs across cosmic history
Sérgio Sacani
 
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
Cerebellum_ Parts_Structure_Function.pptx
muralinath2
 
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
Integrating Lifestyle Data into Personalized Health Solutions (www.kiu.ac.ug)
publication11
 
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
20250603 Recycling 4.pdf . Rice flour, aluminium, hydrogen, paper, cardboard.
Sharon Liu
 
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
Q1 - W1 - D2 - Models of matter for science.pptx
RyanCudal3
 
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
Ad

C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)

  • 2. 2  Conditional operators in details  Decision making structures like IF, IF-ELSE AND SWITCH  Loop control structures like FOR, WHILE, DO- WHILE  Implementation of break and continue.  Nested structures.  Unconditional branching (GOTO statement).
  • 3. 3 INTRODUCTION:-  Normally the statement of a program are executed from first to last.  Sometimes it is necessary to alter the sequence of execution of statements.  Based on certain conditions or we may require some statements to be executed repeatedly until some condition is satisfied.  This involves decision making and looping.
  • 4. 4 THREE MECHANISM ARE:-  if statement:Many program we require testing of some condition at some point in the program and take action. Syntax: if(condition) { Statements; } Expressio n statements F T
  • 5. 5  if-else statement:Many program require testing of some condition at some point in the program and selecting one of the alternative at some point and selecting one of the alternative path depending upon the result is known as branching.  General Form is:  if(expression)  statement1 ;  else  statement 2; Expressi on statement1 statement2 T F
  • 6. 6  Ex. Check given number is even or odd. Example of if-else statement  Solution:  #include<stdio.h>  main( )  {  int no;  printf(“n Enter any number”);  scanf(“%d”,&no);  if (no %2==0)  {  printf(“ number=%d is even number”,no);  }  else  {  printf(“n number =%d is odd number”,no);  }  }//main
  • 7. 7 NESTED IF–ELSE STATEMENT  NESTED “IF-ELSE” STATEMENTS:-  Nested “if-else” statements, the if or the else can contain another if or if-else statement. This provides a programmer with a lot of flexibility in programming. Nesting could take one of several form as illustrated as below:   1] if statement inside else:-  Syntax: if(expression)  Statement1;  else  if(expression2)  Statement2;
  • 8. 8  if- else statement inside if:- Syntax:-if(expression 1)  { if(expression 2) statement 1; else statement 2; } else statements 3; 
  • 9. 9  4] if-else statement inside if-else:-  Syntax:-if(expression1)  {if(expression2)  statement1;  else  statement 2;}  else  if(expression3)  statement3;  else  statement 4;  Eg Write a c program to accept 3 no from user and check greater no.  #include<stdio.h>  #include<conio.h>  void main()  {  int no1, no2, no3;  clrscr();  printf(“n Ennter any 3 nos:”);  scanf(“%d%d%d”,&no1,&no2,&no3);  if(no1>no2)  if(no1>no3)  printf(“n no1 is greater number”); 
  • 10. 10  5]else-if ladder:-  If there is if-else statement nested in each else of an if-else construct, that is called as else-if ladder.  Syntax: if(expression1)  statement 1;  else  if(expression 2)  statement2;  else  if(expression3)  statement 3;  else  statement 4;
  • 11. 11  Conditional Branching switch Statement: Whenever we want to make decision among multiple alternatives nested else-if statement can be used, however structure become very complicated and code become difficult to read and trace. For this reason C has a build-in multi branch decision statement called switch. case 1 value 1 switch(expr) case 2 value 2 case n value n  default 
  • 12. 12 RULES:-  Syntax:  switch(expression)  {  case value1: Statements;  break;  case value 2: Statements;  break;  ….  case value n: Statements;  break;  default : Statements;  }  The expression enclosed within parenthesis is successively compare against the constant expression(value)in each case. They called case labeled and must be end with colon( : ).  The statement in each case may contain zero or more statements. If there are multiple statement for a case they need not be enclosed in bracket.  All case expression must be different.  The case default is executed if non of the other case match.
  • 13. 13 NESTED SWITCH STATEMENT:-  It is possible to have a switch statement as a part of statement is another switch statement then it is called as Nested switch statement.  Even if the case constant of the inner and outer switch contain common values there is not conflict.(mix with each other)
  • 14. 14  Ex.  #include<stdio.h>  main()  {  int x,y;  printf(“n enter 2 binary values”);  scanf(“%d%d”,&x,&y);  switch(x)  {  case 0:printf(“n Invalid Values”);  break;  case 1: switch(y)  {  case 0: printf(“n Values are 1 & 0”);  break;  case 1:printf(“n Values are 1 & 1”);  break;  }// inner switch  break;  } //outer switch  }//main
  • 15. 15 LOOP CONTROL STRUCTURE:-  A block of program statements that is executed repeatedly is called a loop. The repetition is done until some condition for termination of the loop is satisfied .  A loop structure essentially contains a test condition. 1]A test condition determines the number of times the loop body is executed. 2]Loop statements- If the test condition evaluate to true then statement within the loop body get executed otherwise control transfer outside the loop i.e loop termination. initially contains a test condition.
  • 16. 16  1) Top –Tested Loop:  An Entry Controlled loop , the condition is evaluated before the loop body is executed. Ex.while,for Test Condition Loop Body Statement False True
  • 17. 17  2) Bottom tested loop(exit controlled loop).  In exit controlled loop, the condition is tested after the loop body executed.  Ex.do-while Loop Body Test Condition True False
  • 18. 18 ITERATION PROCEDURE:-  The iteration procedure takes place in 4 steps 1. Initializing the loop control variable. 2. Execution of loop statement. 3. Changing the value of the control variable. 4. Testing the condition. 5. The C language provides 3 loops:- 6. 1) while 7. 2) do-while 8. 3) for
  • 19. 19 LOOPS:-  C LANGUAGE PROVIDES 3 LOOP STRUCTURES:- 1) While: It is often used when the no. of time the loop is to be executed is not known in advanced but depends on the test condition.ENTRY CONTOL LOOP 2) do-while: do-while loop is a bottom tested or exit controlled loop i.e it evaluates the condition after the execution of statement in it construct. This means that the statements within the loop are executed at least once if the condition is false. 3) for: for loop is very flexible, powerful and most commonly used loop in C. It is useful when the member of repetition is known in advance. This is a top-tested loop similarly to the while loop.
  • 20. 20 1] While:-  It is often used when the no. of time the loop is to be executed is not known in advanced but depends on the test condition.  Syntax:- while(expression)  {  statements;  } That expression is a test condition and can be any valid c expression. The statement can be a single or set of statement.
  • 21. 21  2) The do-while loop  Iterative statement means loop control statement.  do-while loop, is a bottom-tested or exit control loop i.e. It evaluates the condition after the execution of statements in its.  This means that the statements within the loop are executed at-least once, when condition is false.  Syntax:  do  {  statements;  }while(expression); 
  • 22. 22  3] for loop:-  For loop is very flexible, powerful and most commonly used loop in C . It is useful when the member of repetition is known in advance.  This is a top-tested loop similarly to the while loop.  Syntax:-  for(exp1;exp2;exp3) ex. for( i=1;i<=10;i++)  {  statement;  }  Where, expr1 is to used to initialization of expression.  expr2 is used to test condition .  expr3 is used to update expression.  these 3 expression is separated by ‘ ; ’.
  • 23. 23  Eg. Write a program to print first 10 no’s using for loop  #include<stdio.h>  void main()  {  int i;  for(i=1;i<=10;i=i+1)  {  printf(“%d”,i);  }  }
  • 24. 24  Print table for the given number using C for loop  #include<stdio.h>  int main()  {  int i=1,number=0;  printf("Enter a number: ");  scanf("%d",&number);  for(i=1;i<=10;i++)  {  printf("%d n",(number*i));  }  return 0;  }
  • 25. 25  #include <stdio.h>  int main()  {  int i,j,k;  for(i=0,j=0,k=0;i<4,k<8,j<10;i++)  {  printf("%d %d %dn",i,j,k);  j+=2;  k+=3;  }  } 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12
  • 26. 26  #include <stdio.h>  int main()  {  int i;  for(i=0;;i++)  {  printf("%d",i);  }  }
  • 27. 27 1. #include<stdio.h> 2. void main () 3. { 4. int i=0,j=2; 5. for(i = 0;i<5;i++,j=j+2) 6. { 7. printf("%d %dn",i,j); 8. } 9. } 0 2 1 4 2 6 3 8 4 10
  • 28. 28 NESTED LOOP:-  Loop can also be nested ,Nesting of loops means a loop i.e contain within another loop. Any loop can be nested within any other loop.  Eg:  1) while(expr1)  {  while(expr2)  {  loop body of inner while  }  }  2) while(expr1)  {  for( )  {  loop body of for  }  }  3) for( )  {  …….  for( )  {  statements;  }  } 
  • 29. 29  #include <stdio.h>  int main()  {  int n;// variable declaration  printf("Enter the value of n :");  // Displaying the n tables.  for(int i=1;i<=n;i++) // outer loop  {  for(int j=1;j<=10;j++) // inner loop  {  printf("%dt",(i*j)); // printing the value.  }  printf("n");  }
  • 30. 30  #include <stdio.h>  int main()  {  int rows; // variable declaration  int columns; // variable declaration  int k=1; // variable initialization  printf("Enter the number of rows :"); // input the number of rows.  scanf("%d",&rows);  printf("nEnter the number of columns :"); // input the number of columns.  scanf("%d",&columns);  int a[rows][columns]; //2d array declaration  int i=1;  while(i<=rows) // outer loop  {  int j=1;  while(j<=columns) // inner loop  {  printf("%dt",k); // printing the value of k.  k++; // increment counter  j++;  }  i++;  printf("n");  }  }
  • 31. 31
  • 32. 32 JUMP STATEMENT:-  In many cases we want to stop the execution loop before it ends or transfer the control to some other part of a program. This can be done using jump statement. C supports 4 jump statements 1. break 2. continue 3. return 4. Goto 5. exit
  • 33. 33  1] break:-  Sometime it is require to exit a loop as soon as a certain condition is made.  To skip all subsequent statement of loop body and to come out of the loop.  When the break statement is encounter inside a loop, the loop is immediately terminate subsequent statement are skipped and program control transfer at the next statement of the following loop.
  • 34. 34
  • 35. 35
  • 36. 36 #include <stdio.h> int main() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &n); // 0 and 1 are not prime numbers // change flag to 1 for non-prime number if (n == 0 || n == 1) flag = 1; for (i = 2; i <= n / 2; ++i) { // if n is divisible by i, then n is not prime // change flag to 1 for non-prime number if (n % i == 0) { flag = 1; break; } } // flag is 0 for prime numbers if (flag == 0) printf("%d is a prime number.", n); else printf("%d is not a prime number.", n); return 0; }
  • 37. 37  #include <stdio.h>   int main(){   /* local variable definition */  char ch = 'm';  printf("Time code: %cnn", ch);   switch(ch) {   case 'm':  printf("Good Morning n");  break;   case 'a':  printf("Good Afternoon n");  break;   case 'e':  printf("Good Evening n");  break;   default:  printf("Hello");  }  }
  • 38. 38  2]continue:-  Continue statement is used to transfer control to the beginning of the loop.  It remaining statement and it forces the next interaction of the loop to takes place as usual.  Syntax:- continue;
  • 39. 39  while (expr){  . . .  . . .  if (condition)  continue;  . . .  }
  • 40. 40
  • 41. 41  In this program the loop generates 1 to 10 values of the variable "i".  Whenever it is an even number, the next iteration starts, skipping the printf statement. Only the odd numbers are printed.  #include <stdio.h>  int main(){  int i = 0;   while (i < 10){  i++;  if(i%2 == 0)  continue;  printf("i: %dn", i);   }  }  Output  i: 1  i: 3  i: 5  i: 7  i: 9
  • 42. 42  3] goto Statement:-  goto statement is an unconditional jump statement.  Goto statement is use to alter the normal sequence of program execution by unconditionally transferring by control some other part of program.  Syntax: goto label;  The statement where control has to be transfer is identified by the label.  A label is valid C identifier.  The label followed by colon “:” .  The label can attached to any statement in the same function as the goto.  The label does not has to be declared like other identifier.
  • 43. 43
  • 44. 44  Ex.:  for(….)  {  for(…)  { while( ..)  {  …..  if( )  goto out;  …..  }  }  }  out:  ….  ….
  • 45. 45  #include <stdio.h>   int main (){  int n = 0;   if (n == 0)  goto end;  printf("The number is: %d", n);   end:  printf ("End of program");   return 0;  }
  • 46. 46  #include <stdio.h>   int main (){  START:  printf("Hello World n");  printf("How are you? n");  goto START;  return 0;  }  Output:  Hello World  How are you?  .......  .......  The program prints the two strings continuously until forcibly stopped.
  • 47. 47  4] exit():-  The exit(0) function causes indicate termination of the entire program .the exit function is called with an arguments’0’ to indicate that termination is normal.   Eg. Invalid password enter that time use exit(0).
  • 48. 48