SlideShare a Scribd company logo
1. Control Structure in C.pdf
Introduction
 In C, statements are executed sequentially.
 C provides two styles of flow control to change the sequential
execution of program:
 Branching (Decision Making)
 Looping
 Branching is deciding what actions to take and looping is
deciding how many times to take a certain action.
3
 C language provides such decision making capabilities
by supporting “IF” statement.
 Types of “IF” statements.
 Simple if statement
 if...else statement
 Nested if...else statement
 else if ladder
Decision Making Sataement
4
 Used to execute or skip one statement or group of
statements for a particular condition.
 Syntax:
if (condition)
{
Block of statements;
}
Next Statement X;
 To execute only one statement, opening and closing
braces are not required.
If the condition is true then the statement or block of statements
Gets executed otherwise these statements are skipped and
Statement X will be executed
Simple If
If
Test
conditio
n
Statement-block
True
Statement-x
False
5
Example:
#include<stdio.h>
void main()
{
int i = 5;
if (i >1)
{
printf(" i is greater than 1n");
}
printf(" End of programn");
}
Output:-
i is greater than 1
End of Program
6
7
 The if....else statement is an extension of the simple if
statement.
 Syntax:
if (condition)
{
Block of statements-1;
}
else
{
Block of statements-2;
}
Next Statement;
Simple If else Statement
If test
conditio
n
True False
8
9
Example:
#include<stdio.h>
void main()
{
int i = 5;
if (i >0)
{
printf (" i is greater than 0n");
}
else
{
printf (" i is not grater than 0n");
}
}
Output:-
i is greater than 0
10
 When a series of decisions are involved, we may have to use more than one
if.....else statement in nested form.
 Syntax:
if(condition-p)
{
if(condition-q)
{
statement-1;
}
else
{
statement-2;
}
statement-3;
}
else
{
statement-4;
}
NESTED IF-ELSE… STATEMENT
Conditi
on-q
True
True
False
False
Conditi
on-p
Flow chart
11
12
Example:-
#include <stdio.h>
void main()
{
int a,b,c,big;
a=4;
B=6;
C=3;
If (a>b)
{
if (a>c)
big=a;
else
big=c;
}
else
{
if (b>c)
big=b;
else
big=c;
}
printf (“Largest no=%d”,big);
}
Output:-
Largest no=6
13
 When a series of many conditions have to
be checked we may use the ladder else if
statement
 The structure of else-if ladder looks like a
ladder
 Syntax:
If (condition 1)
statement-1;
else if (condition 2)
statement-2;
else if (condition n)
statement-n;
else
default statement;
next statement;
ELSE-IF…LADDER
Conditio
n-1
Conditio
n-2
Conditio
n-n
Statement-
1
Statement-
2
Statement-
3
Default
statement
T
T
T F
F
F
Next Statement
 The conditions are evaluated from
the top of the ladder to downwards.
 As soon on the true condition is
found, the statement associated
with it is executed and the control is
transferred to the next statement – x
(skipping the rest of the ladder.)
 When all the condition becomes
false, the final else containing the
default statement will be executed.
14
15
Example:
void main()
{
int i=2;
if (i==0)
printf ("i==0n");
else if (i==1)
printf("i==1n");
else if (i==2)
printf("i==2n");
else
printf(“enter the value between 0 to 2n");
}
Output :
i==2
16
 Syntax:
switch(expression)
{
case value1:
block1;
break;
case value2:
block2;
break;
......
.....
default:
default block;
break;
}
next statement;
Switch Statment
Switch
(expressio
n)
Next Statement
Default
Block
Block-4 Block-3 Block-2 Block-1
Value 1
Value 2
Value 3
Value 4
Default
17
18
 C has a built-in multiway decision statement known as a switch.
 The switch statement tests the value of a given variable (or expression)
against a list of case values and when a match is found, a block of
statements associated with that case is executed.
 The expression is an integer expression or characters.
 value1, value2, .... are constants or constant expressions and are known as
case labels.
 block1, block2,...... are statement lists and may contain zero or more
statements. Note that case labels end with a colon (:).
 The break statement at the end of each block signals the end of a particular
case and causes an exit from the switch statement, transferring the control
to the statement-n following the switch.
 The default is an optional case. When present, it will be executed if the
expression does not match any of the case values. If not present, no action
takes place if all matches fail and the control goes to the statement-n.
#include <stdio.h>
int main() {
int color = 1;
printf("Please choose a color(1: red,2: green,3: blue):n");
scanf("%d", &color);
switch (color) {
case 1:
printf("you chose red colorn");
break;
case 2:
printf("you chose green colorn");
break;
case 3:
printf("you chose blue colorn");
break;
default:
printf("you did not choose any colorn");
}
return 0;
}
20
 Example:
void main()
{
int i,n = 5;
for(i = 1; i<n; i= i+1)
{
switch(i%2)
{
case 0 : printf("the number %d is even n",i);
break;
case 1 : printf("the number %d is odd n",i);
break;
}
}
Output :
the number 1 is odd
the number 2 is even
the number 3 is odd
the number 4 is even
No. The if…else The switch-case
1. Is a two-way branching. Is a multi-way branching.
2. The nested if…else is
complicated.
Is easier to write.
3. The nested else…if can take
many levels and it becomes
difficult to match the else part to
its corresponding if.
No such problem occurs in switch-
case.
4. Debugging becomes difficult. Tracing of errors and debugging is
easy.
5. The test expression can be
constant value or may involve
any type of operator.
Only constant integer expressions
and characters values are allowed.
22
 C ?: operator is sometimes called conditional operator or ternary
operator.
 Syntax:
condition ? expression 1 : expression 2
 C first evaluates the condition.
 If the condition is evaluated to true , expresion 1 is evaluated.
Then the operator return the value of the expression 1.
 If the condtion is evaluated to false, expression 2 is evaluated.
The value of the expression 2 is returned.
The ?: Operator (Ternary operator)
23
 Example:
void main()
{
int a=10,b=5,big;
big=if(a>b)?a:b;
printf(“The number %d is greater number
n",big);
}
Output:-
The number 10 is greater number
24
 The goto statement is a jump statement which jumps from one point to
another point within a function.
 Used to transfer the program control unconditionally from one statement to
another statement.
 Syntax :
goto label;
…………
…………
label:
statement;
or
label:
statement;
.......
.......
goto label;
Go to Statement:
25
Example:
void main()
{
int n = 0;
Loop:
printf ("n%d", n);
n++;
if (n<10)
{
goto Loop;
}
getch();
}
Output :
0
1
2
3
4
5
6
7
8
9

More Related Content

Similar to 1. Control Structure in C.pdf (20)

PPTX
Decision Making.pptx
AthulyaRamachandran2
 
PPTX
If statements in c programming
Archana Gopinath
 
PDF
Control statements-Computer programming
nmahi96
 
PDF
Control structure and Looping statements
BalaKrishnan466
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PPTX
chapter 3enejngewngjnjnfjndnennwnnefwf.pptx
TriggeredZulkar
 
PPT
C statements.ppt presentation in c language
chintupro9
 
PPTX
Control Structures in C
sana shaikh
 
PPT
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PPTX
C Programming Control Structures(if,if-else)
poonambhagat36
 
PPTX
computer programming Control Statements.pptx
eaglesniper008
 
PDF
csj-161127083146power point presentation
deepayaganti1
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
Control statement-Selective
Nurul Zakiah Zamri Tan
 
PPTX
Simple if else statement,nesting of if else statement &amp; else if ladder
Moni Adhikary
 
PPT
Flow of Control
Praveen M Jigajinni
 
PDF
3-Conditional-if-else-switch btech computer in c.pdf
ArkSingh7
 
PPT
Decision making in C(2020-2021) statements
BalaKrishnan466
 
Decision Making.pptx
AthulyaRamachandran2
 
If statements in c programming
Archana Gopinath
 
Control statements-Computer programming
nmahi96
 
Control structure and Looping statements
BalaKrishnan466
 
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
chapter 3enejngewngjnjnfjndnennwnnefwf.pptx
TriggeredZulkar
 
C statements.ppt presentation in c language
chintupro9
 
Control Structures in C
sana shaikh
 
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
C Programming Control Structures(if,if-else)
poonambhagat36
 
computer programming Control Statements.pptx
eaglesniper008
 
csj-161127083146power point presentation
deepayaganti1
 
C Programming: Control Structure
Sokngim Sa
 
Control statement-Selective
Nurul Zakiah Zamri Tan
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Moni Adhikary
 
Flow of Control
Praveen M Jigajinni
 
3-Conditional-if-else-switch btech computer in c.pdf
ArkSingh7
 
Decision making in C(2020-2021) statements
BalaKrishnan466
 

Recently uploaded (20)

PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PPTX
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
PDF
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Information Retrieval and Extraction - Module 7
premSankar19
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Ad

1. Control Structure in C.pdf

  • 2. Introduction  In C, statements are executed sequentially.  C provides two styles of flow control to change the sequential execution of program:  Branching (Decision Making)  Looping  Branching is deciding what actions to take and looping is deciding how many times to take a certain action.
  • 3. 3  C language provides such decision making capabilities by supporting “IF” statement.  Types of “IF” statements.  Simple if statement  if...else statement  Nested if...else statement  else if ladder Decision Making Sataement
  • 4. 4  Used to execute or skip one statement or group of statements for a particular condition.  Syntax: if (condition) { Block of statements; } Next Statement X;  To execute only one statement, opening and closing braces are not required. If the condition is true then the statement or block of statements Gets executed otherwise these statements are skipped and Statement X will be executed Simple If
  • 6. Example: #include<stdio.h> void main() { int i = 5; if (i >1) { printf(" i is greater than 1n"); } printf(" End of programn"); } Output:- i is greater than 1 End of Program 6
  • 7. 7  The if....else statement is an extension of the simple if statement.  Syntax: if (condition) { Block of statements-1; } else { Block of statements-2; } Next Statement; Simple If else Statement
  • 9. 9 Example: #include<stdio.h> void main() { int i = 5; if (i >0) { printf (" i is greater than 0n"); } else { printf (" i is not grater than 0n"); } } Output:- i is greater than 0
  • 10. 10  When a series of decisions are involved, we may have to use more than one if.....else statement in nested form.  Syntax: if(condition-p) { if(condition-q) { statement-1; } else { statement-2; } statement-3; } else { statement-4; } NESTED IF-ELSE… STATEMENT
  • 12. 12 Example:- #include <stdio.h> void main() { int a,b,c,big; a=4; B=6; C=3; If (a>b) { if (a>c) big=a; else big=c; } else { if (b>c) big=b; else big=c; } printf (“Largest no=%d”,big); } Output:- Largest no=6
  • 13. 13  When a series of many conditions have to be checked we may use the ladder else if statement  The structure of else-if ladder looks like a ladder  Syntax: If (condition 1) statement-1; else if (condition 2) statement-2; else if (condition n) statement-n; else default statement; next statement; ELSE-IF…LADDER
  • 14. Conditio n-1 Conditio n-2 Conditio n-n Statement- 1 Statement- 2 Statement- 3 Default statement T T T F F F Next Statement  The conditions are evaluated from the top of the ladder to downwards.  As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the next statement – x (skipping the rest of the ladder.)  When all the condition becomes false, the final else containing the default statement will be executed. 14
  • 15. 15 Example: void main() { int i=2; if (i==0) printf ("i==0n"); else if (i==1) printf("i==1n"); else if (i==2) printf("i==2n"); else printf(“enter the value between 0 to 2n"); } Output : i==2
  • 16. 16  Syntax: switch(expression) { case value1: block1; break; case value2: block2; break; ...... ..... default: default block; break; } next statement; Switch Statment
  • 17. Switch (expressio n) Next Statement Default Block Block-4 Block-3 Block-2 Block-1 Value 1 Value 2 Value 3 Value 4 Default 17
  • 18. 18  C has a built-in multiway decision statement known as a switch.  The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed.  The expression is an integer expression or characters.  value1, value2, .... are constants or constant expressions and are known as case labels.  block1, block2,...... are statement lists and may contain zero or more statements. Note that case labels end with a colon (:).  The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement-n following the switch.  The default is an optional case. When present, it will be executed if the expression does not match any of the case values. If not present, no action takes place if all matches fail and the control goes to the statement-n.
  • 19. #include <stdio.h> int main() { int color = 1; printf("Please choose a color(1: red,2: green,3: blue):n"); scanf("%d", &color); switch (color) { case 1: printf("you chose red colorn"); break; case 2: printf("you chose green colorn"); break; case 3: printf("you chose blue colorn"); break; default: printf("you did not choose any colorn"); } return 0; }
  • 20. 20  Example: void main() { int i,n = 5; for(i = 1; i<n; i= i+1) { switch(i%2) { case 0 : printf("the number %d is even n",i); break; case 1 : printf("the number %d is odd n",i); break; } } Output : the number 1 is odd the number 2 is even the number 3 is odd the number 4 is even
  • 21. No. The if…else The switch-case 1. Is a two-way branching. Is a multi-way branching. 2. The nested if…else is complicated. Is easier to write. 3. The nested else…if can take many levels and it becomes difficult to match the else part to its corresponding if. No such problem occurs in switch- case. 4. Debugging becomes difficult. Tracing of errors and debugging is easy. 5. The test expression can be constant value or may involve any type of operator. Only constant integer expressions and characters values are allowed.
  • 22. 22  C ?: operator is sometimes called conditional operator or ternary operator.  Syntax: condition ? expression 1 : expression 2  C first evaluates the condition.  If the condition is evaluated to true , expresion 1 is evaluated. Then the operator return the value of the expression 1.  If the condtion is evaluated to false, expression 2 is evaluated. The value of the expression 2 is returned. The ?: Operator (Ternary operator)
  • 23. 23  Example: void main() { int a=10,b=5,big; big=if(a>b)?a:b; printf(“The number %d is greater number n",big); } Output:- The number 10 is greater number
  • 24. 24  The goto statement is a jump statement which jumps from one point to another point within a function.  Used to transfer the program control unconditionally from one statement to another statement.  Syntax : goto label; ………… ………… label: statement; or label: statement; ....... ....... goto label; Go to Statement:
  • 25. 25 Example: void main() { int n = 0; Loop: printf ("n%d", n); n++; if (n<10) { goto Loop; } getch(); } Output : 0 1 2 3 4 5 6 7 8 9