SlideShare a Scribd company logo
1 of 34
Decision Making Statements
Condition Statements
• Conditional statements help us to make a decision based on certain
conditions.
• These conditions are specified by a set of conditional statements
having Boolean expressions which are evaluated to a Boolean value of
true or false.
Types of Conditional statements in C
• If statement
• If-Else statement
• Nested If-else statement
• If-Else ladder
• Switch statement
If statements
• The single if statement in C language is used to execute the code if a
condition is true.
• It is also called a one-way selection statement.
• Syntax
if(expression)
{
//code to be executed
}
• Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
if(n%2==0)
{
printf("%d number in even",num);
}
getch();
}
If-else statement
• The if-else statement in C language is used to execute the code if the
condition is true or false.
• It is also called a two-way selection statement.
• Syntax
if(expression)
{
//Statements
}
else
{
//Statements
}
• Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
if(n%2==0)
{
printf("%d number in even", num);
}
else
{
printf("%d number in odd",num);
}
getch();
}
Nested If-else statement
• The nested if...else statement is used when a program requires more
than one test expression.
• It is also called a multi-way selection statement.
• Nested if-else statements can be useful when we can have multiple
sources of expression and the values and based on the specific value,
we need to check nested conditions.
• Syntax
if( expression )
{
if( expression1 )
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
• Example
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr();
printf("Please Enter 3 number");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greatest");
}
else
{
printf("c is greatest");
}
}
else
{
if(b>c)
{
printf("b is greatest");
}
else
{
printf("c is greatest");
}
}
getch();
}
If..else If ladder
• The if-else-if statement is used to execute one code from multiple
conditions.
• It is also called a multipath decision statement.
• Syntax
if(condition1)
{
//statements
}
else if(condition2)
{
//statements
}
else if(condition3)
{
//statements
}
else
{
//statements
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf("enter a number");
scanf("%d",&a);
if( a%5==0 && a%8==0)
{
printf("divisible by both 5 and 8");
}
else if( a%8==0 )
{
printf("divisible by 8");
}
else if(a%5==0)
{
printf("divisible by 5");
}
else
{
printf("divisible by none");
}
getch();
}
Switch Statement
• switch statement acts as a substitute for a long if-else-if ladder that is
used to test a list of cases.
• A switch statement contains one or more case labels that are tested
against the switch expression.
• When the expression match to a case then the associated statements
with that case would be executed.
• Synatx
Switch (expression)
{
case value1:
//Statements
break;
case value 2:
//Statements
break;
case value 3:
//Statements
case value n:
//Statements
break;
Default:
//Statements
}
• Example
#include<stdio.h>
#include<conio.h>
void main( )
{
char grade = 'B';
if (grade == 'A')
{
printf("Excellent!");
}
else if (grade == 'B')
{
printf("Well done");
}
else if (grade == 'D')
{
printf("You passed");
}
else if (grade == 'F')
{
printf("Better try again");
}
else
{
printf("You Failed!");
}
}
getch();
}
Note
• The switch statement must be an integral type.
• Case labels must be constants.
• Case labels must be unique.
• Case labels must end with a colon.
• The break statement transfers the control out of the switch statement.
• The break statement is optional.
Looping Statments
• Loops in C programming language are a conditional concept used to
execute a line or block of code consecutively.
• In C programming, there are three loops: For Loop, While Loop, and
Do While Loop.
• Loops in C can also be combined with other control statements such
as the Break statement, Goto statement, and Control statement.
These loops can be used anywhere in the program, in either entry
control or exit control units.
Different Types of Loops
• There are 3 different types of Loops in C:
1) While Loop
2) Do While Loop
3) For Loop
While Loop
• The condition is evaluated before processing the loop’s body.
• Only the loop’s body is executed if the condition is true. Then the
control goes back to the beginning after completing the loop once.
• The control will go out of the loop if the condition is false.
• After completion of the loop, the control will go to the statement
immediately after the loop.
• If the condition is not true in the while loop, then loop statements
won’t get executed.
• Syntax
while (condition) {
statements;
}
Example
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
while(num<=5)
{
printf("%dn",num);
num++;
}
return 0;
}
Do While Loop
• In this loop, the statements the loop need to be executed at least
once.
• If the condition is true, it will again have executed the loop;
otherwise, it will exit it. It is known as an exit-controlled loop.
• The while loop is performed only when the condition is true, but
sometimes the statement must be conducted at least once, so the do-
while loop has to be used.
• Syntax
do {
statements
} while (expression);
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
do
{
printf ("%dn",2*num);
num++;
}
while(num<=5);
return 0;
}
For Loop
• It executes the set of statements until the time a particular condition
is accomplished. It is known as the Open-ended loop.
• In For loop, we can have more than one initialization or
increment/decrement, separated using a comma operator and one
condition.
• Syntax
for (initial value; condition; incrementation or decrementation )
{
statements;
}
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
for(number=1;number<=5;number++)
{
printf("%dn",number);
}
return 0;
}
Control Statements
• Some loop control statements need to be used in loops for different
purposes and to achieve the end result.
• Below are the different statements that are used:
1) Break statement
2) Continue statement
3) Goto statment
Break statement
• The break statement is used to exit the loop immediately after
executing a particular statement for a specific condition.
• Syntax
While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ break;}
Statement 3; }
Continue Statement
• It generally skips the statements according to the condition.
• For a particular condition, it skips the current loop or statements and
enters into a new loop or condition.
• Syntax
While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ continue;}
Statement 3; }
Goto statement
• It is used to transfer the protocol to a labeled statement.
• Syntax:
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
number=0;
repeat:
printf ("%dn",number);
number++;
if(number<=5)
goto repeat;
return 0;
}
Thank You

More Related Content

Similar to Condition Stmt n Looping stmt.pptx

Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguageTanmay Modi
 
10control statement in c#
10control statement in c#10control statement in c#
10control statement in c#Sireesh K
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxSmitaAparadh
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 

Similar to Condition Stmt n Looping stmt.pptx (20)

Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
10control statement in c#
10control statement in c#10control statement in c#
10control statement in c#
 
Control structure
Control structureControl structure
Control structure
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
C operators
C operatorsC operators
C operators
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 

Recently uploaded

INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 

Recently uploaded (20)

INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 

Condition Stmt n Looping stmt.pptx

  • 2. Condition Statements • Conditional statements help us to make a decision based on certain conditions. • These conditions are specified by a set of conditional statements having Boolean expressions which are evaluated to a Boolean value of true or false.
  • 3. Types of Conditional statements in C • If statement • If-Else statement • Nested If-else statement • If-Else ladder • Switch statement
  • 4. If statements • The single if statement in C language is used to execute the code if a condition is true. • It is also called a one-way selection statement.
  • 6. • Example: #include<stdio.h> #include<conio.h> void main() { int num=0; printf("enter the number"); scanf("%d",&num); if(n%2==0) { printf("%d number in even",num); } getch(); }
  • 7. If-else statement • The if-else statement in C language is used to execute the code if the condition is true or false. • It is also called a two-way selection statement.
  • 9. • Example: #include<stdio.h> #include<conio.h> void main() { int num=0; printf("enter the number"); scanf("%d",&num); if(n%2==0) { printf("%d number in even", num); } else { printf("%d number in odd",num); } getch(); }
  • 10. Nested If-else statement • The nested if...else statement is used when a program requires more than one test expression. • It is also called a multi-way selection statement. • Nested if-else statements can be useful when we can have multiple sources of expression and the values and based on the specific value, we need to check nested conditions.
  • 11. • Syntax if( expression ) { if( expression1 ) { statement-block1; } else { statement-block 2; } } else { statement-block 3; }
  • 12. • Example #include<stdio.h> #include<conio.h> void main( ) { int a,b,c; clrscr(); printf("Please Enter 3 number"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) { printf("a is greatest"); } else { printf("c is greatest"); } }
  • 14. If..else If ladder • The if-else-if statement is used to execute one code from multiple conditions. • It is also called a multipath decision statement.
  • 15. • Syntax if(condition1) { //statements } else if(condition2) { //statements } else if(condition3) { //statements } else { //statements }
  • 16. #include<stdio.h> #include<conio.h> void main( ) { int a; printf("enter a number"); scanf("%d",&a); if( a%5==0 && a%8==0) { printf("divisible by both 5 and 8"); } else if( a%8==0 ) { printf("divisible by 8"); } else if(a%5==0) { printf("divisible by 5"); } else { printf("divisible by none"); } getch(); }
  • 17. Switch Statement • switch statement acts as a substitute for a long if-else-if ladder that is used to test a list of cases. • A switch statement contains one or more case labels that are tested against the switch expression. • When the expression match to a case then the associated statements with that case would be executed.
  • 18. • Synatx Switch (expression) { case value1: //Statements break; case value 2: //Statements break; case value 3: //Statements case value n: //Statements break; Default: //Statements }
  • 19. • Example #include<stdio.h> #include<conio.h> void main( ) { char grade = 'B'; if (grade == 'A') { printf("Excellent!"); } else if (grade == 'B') { printf("Well done"); } else if (grade == 'D') { printf("You passed"); }
  • 20. else if (grade == 'F') { printf("Better try again"); } else { printf("You Failed!"); } } getch(); }
  • 21. Note • The switch statement must be an integral type. • Case labels must be constants. • Case labels must be unique. • Case labels must end with a colon. • The break statement transfers the control out of the switch statement. • The break statement is optional.
  • 22. Looping Statments • Loops in C programming language are a conditional concept used to execute a line or block of code consecutively. • In C programming, there are three loops: For Loop, While Loop, and Do While Loop. • Loops in C can also be combined with other control statements such as the Break statement, Goto statement, and Control statement. These loops can be used anywhere in the program, in either entry control or exit control units.
  • 23. Different Types of Loops • There are 3 different types of Loops in C: 1) While Loop 2) Do While Loop 3) For Loop
  • 24. While Loop • The condition is evaluated before processing the loop’s body. • Only the loop’s body is executed if the condition is true. Then the control goes back to the beginning after completing the loop once. • The control will go out of the loop if the condition is false. • After completion of the loop, the control will go to the statement immediately after the loop. • If the condition is not true in the while loop, then loop statements won’t get executed.
  • 25. • Syntax while (condition) { statements; } Example #include<stdio.h> #include<conio.h> int main() { int num=1; while(num<=5) { printf("%dn",num); num++; } return 0; }
  • 26. Do While Loop • In this loop, the statements the loop need to be executed at least once. • If the condition is true, it will again have executed the loop; otherwise, it will exit it. It is known as an exit-controlled loop. • The while loop is performed only when the condition is true, but sometimes the statement must be conducted at least once, so the do- while loop has to be used.
  • 27. • Syntax do { statements } while (expression); Example: #include<stdio.h> #include<conio.h> int main() { int num=1; do { printf ("%dn",2*num); num++; } while(num<=5); return 0; }
  • 28. For Loop • It executes the set of statements until the time a particular condition is accomplished. It is known as the Open-ended loop. • In For loop, we can have more than one initialization or increment/decrement, separated using a comma operator and one condition.
  • 29. • Syntax for (initial value; condition; incrementation or decrementation ) { statements; } Example: #include<stdio.h> #include<conio.h> int main() { int number; for(number=1;number<=5;number++) { printf("%dn",number); } return 0; }
  • 30. Control Statements • Some loop control statements need to be used in loops for different purposes and to achieve the end result. • Below are the different statements that are used: 1) Break statement 2) Continue statement 3) Goto statment
  • 31. Break statement • The break statement is used to exit the loop immediately after executing a particular statement for a specific condition. • Syntax While (Condition) { Statement 1; statement 2; If (Condition) { break;} Statement 3; }
  • 32. Continue Statement • It generally skips the statements according to the condition. • For a particular condition, it skips the current loop or statements and enters into a new loop or condition. • Syntax While (Condition) { Statement 1; statement 2; If (Condition) { continue;} Statement 3; }
  • 33. Goto statement • It is used to transfer the protocol to a labeled statement. • Syntax: #include<stdio.h> #include<conio.h> int main() { int number; number=0; repeat: printf ("%dn",number); number++; if(number<=5) goto repeat; return 0; }