SlideShare a Scribd company logo
1 of 26
Objectives of these slides:
 to introduce the main kinds of C
control flow
Control
Statements
C Programming
Control Structures
 There may be situations where the
programmer requires to alter normal flow of
execution of program or to perform the same
operation a no. of times.
 Various control statements supported by c
are-
 Decision control statements
 Loop control statements
Looping Structures
 When we want to repeat a group of statements a no.
of times, loops are used.
 These loops are executed until the condition is true.
 When condition becomes false, control terminates
the loop and moves on to next instruction
immediately after the loop.
 Various looping structures are-
 while
 do – while
 for
LOOPING STATEMENTS
 Loop is divided into two parts:
 Body of the loop
 Control of loop
 Mainly control of loop is divided into two
parts:
 Entry Control loop (while, for)
 Exit Control loop (do-while)
while statement
 While loop is used to execute set of statements as long as
condition evaluates to true.
 It is mostly used in those cases where the programmer
doesn’t know in advance how many times the loop will be
executed.
 Syntax: while (condition)
{
Statement 1 ;
Statement 2 ;
}
condition statement
Statement after while loop
true
do- while
 do-while is similar to while except that its test
condition is evaluated at the end of the loop instead at
the beginning as in case of while loop.
 So, in do-while the body of the loop always executes
at least once even if the test condition evaluates to
false during the first iteration.
 Syntax: do
{
statement 1;
statement 2;
}while (condition);
statement;
Body of loop
Test condition
Next statement
true
false
for loop
 Most versatile and popular of three loop structures.
 Is used in those situations when a programmer
knows in advance the number of times a statement
or block will be executed.
 It contains loop control elements all at one place
while in other loops they are scattered over the
program and are difficult to understand.
 Syntax:-
for (initialization; condition; increment/decrement)
{
Statement( s);
}
The for is a sort of while
for (expr1; expr2; expr3)
statement;
is equivalent to:
expr1;
while (expr2) {
statement;
expr3;
}
Various other ways of writing same for loops
i = 1
for (; i<=15;i ++)
{
……..
}
for (i=1; ;i++)
{
………
if (i>15)
break;
……
}
for (i=1;i<=15;)
{
………….
i++;
}
Some Examples
for(i = 7; i <=77; i += 7)
statement;
for(i = 20; i >= 2; i -= 2)
statement;
for(j = 10; j > 20; j++)
statement;
for(j = 10; j > 0; j--)
statement;
Incrementing
 Add 1 to c by writing:
c = c + 1;
Also: c += 1;
Also: c++;
Also: ++c;
Incrementing and Decrementing
/* Preincrementing and postincrementing */
#include <stdio.h>
int main()
{
int c;
c = 5;
printf("%dn", c);
printf("%dn",c++); /*post increment*/
printf("%dnn", c);
:
continued
c = 5;
printf("%dn", c);
printf("%dn",++c); /*pre-
increment*/
printf("%dn", c);
return 0;
}
Output:
5
5
6
5
6
6
Decrementing
 Take 1 from c by writing:
c = c - 1;
Also: c -= 1;
Also: c--;
Also: --c;
Decision Control Statements
 Decision control statements alter the normal
sequential execution of the statements of the
program depending upon the test condition to be
carried out at a particular point in program.
 Decision control statements supported by c are:-
 if statement
 if-else statement
 Else if Ladder
 Nested If
 switch statement
Decision Control Statements
 if statement
 Most simple and powerful decision control statement.
 It executes a statement or block of statements only if the
condition is true.
 Syntax: if (condition) if (condition)
{ OR {
statement (s); statement 1;
} statement 2;
Next statement; }
statement 3;
 In above syntax : if condition is true only then the
statements within the block are executed otherwise next
statement in sequence is executed.
 Flowchart
Condition
STOP
False
True
Block of if
Next statement
/* Program to check whether a no. is even */
# include<stdio.h>
# include<conio.h>
void main()
{
int num;
clrscr();
printf(“enter the number”);
scanf(“%d”,&num)
if(num%2==0)
{
printf(“n Number is even”);
}
printf(“ End of program”);
getch();
}
if – else statement
 In case of if statement, the block of statements is executed only when the
condition is true otherwise the control is transferred to the next statement
following if block.
 But if specific statements are to be executed in both cases (either
condition is true or false) then if – else statement is used.
 In if – else statement a block of statements are executed if the condition
is true but a different block of statements is executed when the condition
is false.
 Syntax: if (condition)
{
statement 1;
statement 2;
}
else
{
statement 3;
}
Test
Condition
Block of if Block of else
Next statement
STOP
False
True
Exercise: WAP to check whether a given no. is even or
odd?
Nested if – else statement
 When an entire if-else is enclosed within the body of if
statement or/and in the body of else statement, it is known
as nested if-else statement.
 The ways of representing nested if –else are-
if (condition1)
{
if (condition2)
statement 1;
else
statement 2;
}
else
statement 3;
if (condition1)
{
if (condition2)
statement 1;
else
statement 2;
}
else
{
if (condition 3)
statement 3;
else
statement 4;
}
if (condition1)
statement 1;
else
{
if (condition2)
statement 2;
else
statement 3;
}
If- else- if ladder
 In a program involving multiple conditions, the nested if else
statements makes the program very difficult to write and
understand if nested more deeply.
 For this ,we use if-else-if ladder.
 Syntax: if (condition1)
statement1;
else if(condition2)
statement2;
else if(condition3)
statement 3;
else
default statement;
condition 1
condition 2
condition 3
Statement 1
Statement 2
Statement 3
Default statement
Next statement
false
true
false
true
false
true
Switch statement
 Switch is a multi-way decision making statement which selects
one of the several alternatives based on the value of single
variable or expression.
 It is mainly used to replace multiple if-else-if statement.
 The if-else-if statement causes performance degradation as
several conditions need to be evaluated before a particular
condition is satisfied.
 Syntax: switch (expression)
{
case constant1 : statement (s); [break;]
case constant2 : statement (s); [break;]
…………………………………….
default: statement (s)
}
Break statement
 Break statement terminates the execution of
the loop in which it is defined.
 The control is transferred immediately to the
next executable statement after the loop.
 It is mostly used to exit early from the loop
by skipping the remaining statements of
loop or switch control structures.
 Syntax: break;
Continue statement
 Like break ,continue statement also skips the remaining
statements of the body of the loop where it is defined but
instead of terminating the loop, the control is transferred to the
beginning of the loop for next iteration.
 The loop continues until the test condition of the loop become
false.
 Syntax: continue;
 E.g. for (m=1;m<=3;m++)
{
for (n=1;n<=2;n++)
{
if (m==n)
continue;
printf(“ m=%d n=%d”);
}
}
Output:
1 2
2 1
3 1
3 2
goto Statement
 An unconditional control statement that causes the control to
jump to a different location in the program without checking
any condition.
 It is normally used to alter the normal sequence of program
execution by transferring control to some other part of the
program.
 So it is also called jump statement.
 Syntax: goto label;
 Label represents an identifier which is used to label the
destination statement to which the control should be
transferred.
label : statement;
 The goto statement causes the control to be shifted either in
forward direction or in a backward direction .
exit() function
 C provides a run time library function exit() which
when encountered in a program causes the program to
terminating without executing any statement
following it.
 Syntax: exit(status);
Status is an integer variable or constant.
 If the status is 0,then program normally terminates
without any errors.
 A non-zero status indicates abnormal termination of
the program.
 The exit() function is defined in the process.h header
file.
Difference b/w exit() & break
 Exit() is used to transfer the control
completely out of the program whereas
break is used to transfer the control out
of the loop or switch statement.

More Related Content

Similar to 2. Control structures with for while and do while.ppt

Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statementsRai University
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c languagesneha2494
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsRai University
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsRai University
 
Jumping statements
Jumping statementsJumping statements
Jumping statementsSuneel Dogra
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statementsRai University
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)TejaswiB4
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statementsRai University
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 

Similar to 2. Control structures with for while and do while.ppt (20)

Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Ch3 repetition
Ch3 repetitionCh3 repetition
Ch3 repetition
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
 

Recently uploaded

microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
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
 
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
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
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
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
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
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
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
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
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
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 

2. Control structures with for while and do while.ppt

  • 1. Objectives of these slides:  to introduce the main kinds of C control flow Control Statements C Programming
  • 2. Control Structures  There may be situations where the programmer requires to alter normal flow of execution of program or to perform the same operation a no. of times.  Various control statements supported by c are-  Decision control statements  Loop control statements
  • 3. Looping Structures  When we want to repeat a group of statements a no. of times, loops are used.  These loops are executed until the condition is true.  When condition becomes false, control terminates the loop and moves on to next instruction immediately after the loop.  Various looping structures are-  while  do – while  for
  • 4. LOOPING STATEMENTS  Loop is divided into two parts:  Body of the loop  Control of loop  Mainly control of loop is divided into two parts:  Entry Control loop (while, for)  Exit Control loop (do-while)
  • 5. while statement  While loop is used to execute set of statements as long as condition evaluates to true.  It is mostly used in those cases where the programmer doesn’t know in advance how many times the loop will be executed.  Syntax: while (condition) { Statement 1 ; Statement 2 ; } condition statement Statement after while loop true
  • 6. do- while  do-while is similar to while except that its test condition is evaluated at the end of the loop instead at the beginning as in case of while loop.  So, in do-while the body of the loop always executes at least once even if the test condition evaluates to false during the first iteration.  Syntax: do { statement 1; statement 2; }while (condition); statement; Body of loop Test condition Next statement true false
  • 7. for loop  Most versatile and popular of three loop structures.  Is used in those situations when a programmer knows in advance the number of times a statement or block will be executed.  It contains loop control elements all at one place while in other loops they are scattered over the program and are difficult to understand.  Syntax:- for (initialization; condition; increment/decrement) { Statement( s); }
  • 8. The for is a sort of while for (expr1; expr2; expr3) statement; is equivalent to: expr1; while (expr2) { statement; expr3; }
  • 9. Various other ways of writing same for loops i = 1 for (; i<=15;i ++) { …….. } for (i=1; ;i++) { ……… if (i>15) break; …… } for (i=1;i<=15;) { …………. i++; }
  • 10. Some Examples for(i = 7; i <=77; i += 7) statement; for(i = 20; i >= 2; i -= 2) statement; for(j = 10; j > 20; j++) statement; for(j = 10; j > 0; j--) statement;
  • 11. Incrementing  Add 1 to c by writing: c = c + 1; Also: c += 1; Also: c++; Also: ++c; Incrementing and Decrementing
  • 12. /* Preincrementing and postincrementing */ #include <stdio.h> int main() { int c; c = 5; printf("%dn", c); printf("%dn",c++); /*post increment*/ printf("%dnn", c); : continued
  • 13. c = 5; printf("%dn", c); printf("%dn",++c); /*pre- increment*/ printf("%dn", c); return 0; } Output: 5 5 6 5 6 6
  • 14. Decrementing  Take 1 from c by writing: c = c - 1; Also: c -= 1; Also: c--; Also: --c;
  • 15. Decision Control Statements  Decision control statements alter the normal sequential execution of the statements of the program depending upon the test condition to be carried out at a particular point in program.  Decision control statements supported by c are:-  if statement  if-else statement  Else if Ladder  Nested If  switch statement
  • 16. Decision Control Statements  if statement  Most simple and powerful decision control statement.  It executes a statement or block of statements only if the condition is true.  Syntax: if (condition) if (condition) { OR { statement (s); statement 1; } statement 2; Next statement; } statement 3;  In above syntax : if condition is true only then the statements within the block are executed otherwise next statement in sequence is executed.
  • 17.  Flowchart Condition STOP False True Block of if Next statement /* Program to check whether a no. is even */ # include<stdio.h> # include<conio.h> void main() { int num; clrscr(); printf(“enter the number”); scanf(“%d”,&num) if(num%2==0) { printf(“n Number is even”); } printf(“ End of program”); getch(); }
  • 18. if – else statement  In case of if statement, the block of statements is executed only when the condition is true otherwise the control is transferred to the next statement following if block.  But if specific statements are to be executed in both cases (either condition is true or false) then if – else statement is used.  In if – else statement a block of statements are executed if the condition is true but a different block of statements is executed when the condition is false.  Syntax: if (condition) { statement 1; statement 2; } else { statement 3; } Test Condition Block of if Block of else Next statement STOP False True
  • 19. Exercise: WAP to check whether a given no. is even or odd? Nested if – else statement  When an entire if-else is enclosed within the body of if statement or/and in the body of else statement, it is known as nested if-else statement.  The ways of representing nested if –else are- if (condition1) { if (condition2) statement 1; else statement 2; } else statement 3; if (condition1) { if (condition2) statement 1; else statement 2; } else { if (condition 3) statement 3; else statement 4; } if (condition1) statement 1; else { if (condition2) statement 2; else statement 3; }
  • 20. If- else- if ladder  In a program involving multiple conditions, the nested if else statements makes the program very difficult to write and understand if nested more deeply.  For this ,we use if-else-if ladder.  Syntax: if (condition1) statement1; else if(condition2) statement2; else if(condition3) statement 3; else default statement; condition 1 condition 2 condition 3 Statement 1 Statement 2 Statement 3 Default statement Next statement false true false true false true
  • 21. Switch statement  Switch is a multi-way decision making statement which selects one of the several alternatives based on the value of single variable or expression.  It is mainly used to replace multiple if-else-if statement.  The if-else-if statement causes performance degradation as several conditions need to be evaluated before a particular condition is satisfied.  Syntax: switch (expression) { case constant1 : statement (s); [break;] case constant2 : statement (s); [break;] ……………………………………. default: statement (s) }
  • 22. Break statement  Break statement terminates the execution of the loop in which it is defined.  The control is transferred immediately to the next executable statement after the loop.  It is mostly used to exit early from the loop by skipping the remaining statements of loop or switch control structures.  Syntax: break;
  • 23. Continue statement  Like break ,continue statement also skips the remaining statements of the body of the loop where it is defined but instead of terminating the loop, the control is transferred to the beginning of the loop for next iteration.  The loop continues until the test condition of the loop become false.  Syntax: continue;  E.g. for (m=1;m<=3;m++) { for (n=1;n<=2;n++) { if (m==n) continue; printf(“ m=%d n=%d”); } } Output: 1 2 2 1 3 1 3 2
  • 24. goto Statement  An unconditional control statement that causes the control to jump to a different location in the program without checking any condition.  It is normally used to alter the normal sequence of program execution by transferring control to some other part of the program.  So it is also called jump statement.  Syntax: goto label;  Label represents an identifier which is used to label the destination statement to which the control should be transferred. label : statement;  The goto statement causes the control to be shifted either in forward direction or in a backward direction .
  • 25. exit() function  C provides a run time library function exit() which when encountered in a program causes the program to terminating without executing any statement following it.  Syntax: exit(status); Status is an integer variable or constant.  If the status is 0,then program normally terminates without any errors.  A non-zero status indicates abnormal termination of the program.  The exit() function is defined in the process.h header file.
  • 26. Difference b/w exit() & break  Exit() is used to transfer the control completely out of the program whereas break is used to transfer the control out of the loop or switch statement.