SlideShare a Scribd company logo
CONTROL STATEMENTS
LEARNING OBJECTIVES
 Understanding meaning of a statement and
statement block.
 Learn about decision type control constructs in C
and the way these are used.
 Learn about looping type control constructs in C
and the technique of putting them to use.
 Learn the use of special control constructs such as
goto, break, continue, and return.
 Learn about nested loops and their utility.
CONTROL STATEMENTS INCLUDE
Selection
Statements
• if
• if-else
• switch
Iteration
Statements
• for
• while
• do-while
Jump
Statements
• goto
• break
• continue
• return
PROGRAM CONTROL
STATEMENTS/CONSTRUCTS
Program Control
Statements/Constructs
Selection/Branching
Conditional
Type
if if-else
if-else-
if
switch
Unconditional
Type
break continue goto
Iteration/Looping
for while
do-
while
CONDITIONAL EXECUTION AND
SELECTION
• Selection Statements
• The Conditional Operator
• The switch Statement
SELECTION STATEMENTS
• One-way decisions using if statement
• Two-way decisions using if-else statement
• Multi-way decisions
• Dangling else Problem
ONE-WAY DECISIONS USING IF
STATEMENT
Flowchart for if construct
TestExpr
stmtT
WRITE A PROGRAM THAT PRINTS THE
LARGEST AMONG THREE NUMBERS.
Algorithm C Program
1. START #include <stdio.h>
int main()
{
int a, b, c, max;
printf(“nEnter 3 numbers”);
scanf(“%d %d %d”, &a, &b, &c);
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
printf(“Largest No is %d”, max);
return 0;
}
2. PRINT “ENTER THREE
NUMBERS”
3. INPUT A, B, C
4. MAX=A
5. IF B>MAX THEN MAX=B
6. IF C>MAX THEN MAX=C
7. PRINT “LARGEST
NUMBER IS”, MAX
8. STOP
TWO-WAY DECISIONS USING
IF-ELSE STATEMENT
Flowchart of if-else construct
The form of a two-way
decision is as follows:
if(TestExpr)
stmtT;
else
stmtF;
TestExpr
stmtT stmtF
WRITE A PROGRAM THAT PRINTS THE
LARGEST AMONG THREE NUMBERS.
Algorithm C Program
1. START #include <stdio.h>
int main()
{
int a, b, c, max;
printf(“nEnter 3 numbers”);
scanf(“%d %d %d”, &a, &b, &c);
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
printf(“Largest No is %d”, max);
return 0;
}
2. PRINT “ENTER THREE
NUMBERS”
3. INPUT A, B, C
4. MAX=A
5. IF B>MAX THEN MAX=B
6. IF C>MAX THEN MAX=C
7. PRINT “LARGEST
NUMBER IS”, MAX
8. STOP
MULTI-WAY DECISIONS
if-else-if ladder
if(TestExpr1)
stmtT1;
else if(TestExpr2)
stmtT2;
else if(TestExpr3)
stmtT3;
.. .
else if(TestExprN)
stmtTN;
else
stmtF;
General format of switch
statements
switch(expr)
{
case constant1: stmtList1;
break;
case constant2: stmtList2;
break;
case constant3: stmtList3;
break;
………………………….
………………………….
default: stmtListn;
}
FLOWCHART OF AN IF-
ELSE-IF CONSTRUCT
TestExpr
TestExpr2
TestExprN
TestExpr3
stmtT2
stmtTN
stmtT3
stmtTF
stmtT1
THE FOLLOWING PROGRAM CHECKS WHETHER A
NUMBER GIVEN BY THE USER IS ZERO, POSITIVE, OR
NEGATIVE
int main()
{
int x;
printf(“n ENTER THE NUMBER:”);
scanf(“%d”, &x);
if(x > 0)
printf(“x is positive n”);
else if(x == 0)
printf(“x is zero n”);
else
printf(“x is negative n”);
return 0;
NESTED IF
• When any if statement is
written under another if
statement, this cluster is
called a nested if.
• The syntax for the
nested is given here:
Construct 1 Construct 2
if(TestExprA)
if(TestExprB)
stmtBT;
else
stmtBF;
else
stmtAF;
if(TestExprA)
if(TestExprB)
stmtBT;
else
stmtBF;
else
if(TestExprC)
stmtCT;
else
stmtCF;
A PROGRAM TO FIND THE LARGEST AMONG
THREE NUMBERS USING THE NESTED LOOP
int main()
{
int a, b, c;
printf(“nEnter the three numbers”);
scanf(“%d %d %d”, &a, &b, &c);
if(a > b)
if(a > c)
printf(“%d”, a);
else
printf(“%d”, c);
else
if(b > c)
printf(“%d”, b);
else
printf(“%d”, c);
return 0;
}
DANGLING ELSE PROBLEM
• This classic problem occurs
when there is no matching
else for each if. To avoid this
problem, the simple C rule is
that always pair an else to the
most recent unpaired if in the
current block.
• The else is automatically
paired with the closest if.
But, it may be needed to
associate an else with the
outer if also.
SOLUTIONS TO DANGLING
ELSE PROBLEM
• Use of null else
• Use of braces to
enclose the true
action of the second if
With null else With braces
if(TestExprA)
if(TestExprB
)
stmtBT;
else
;
else
stmtAF;
if(TestExprA)
{
if(TestExprB
)
stmtBT;
}
else
stmtAF;
THE SWITCH STATEMENT
The C switch construct
switch(expr)
{
case constant1: stmtList1;
break;
case constant2: stmtList2;
break;
case constant3: stmtList3;
break;
………………………….
………………………….
default: stmtListn;
}
SWITCH VS NESTED IF
 The switch differs from the else-if in that switch can
test only for equality, whereas the if conditional
expression can be of a test expression involving any
type of relational operators and/or logical
operators.
 A switch statement is usually more efficient than
nested ifs.
 The switch statement can always be replaced with a
series of else-if statements.
ITERATION AND REPETITIVE
EXECUTION
• A loop allows one to execute
a statement or block of
statements repeatedly. There
are mainly two types of
iterations or loops –
unbounded iteration or
unbounded loop and bounded
iteration or bounded loop.
• A loop can either be a pre-test
loop or be a post-test loop as
illustrated in the diagram.
“WHILE” CONSTRUCT
Expanded Syntax of “while” and its
Flowchart Representation
• while statement is a
pretest loop. The basic
syntax of the while
statement is shown below:
AN EXAMPLE
#include <stdio.h>
int main()
{
int c;
c=5; // Initialization
while(c>0)
{ // Test Expression
printf(“ n %d”,c);
c=c-1; // Updating
}
return 0;
}
This loop contains all the
parts of a while loop. When
executed in a program, this
loop will output
5
4
3
2
1
TESTING FOR FLOATING-POINT
‘EQUALITY’
float x;
x = 0.0;
while(x != 1.1)
{
x = x + 0.1;
printf(“1.1 minus %f equals %.20gn”, x, 1.1 -x);
}
• The above loop never terminates on many computers,
because 0.1 cannot be accurately represented using
binary numbers.
• The correct way to make the test is to see if the two
numbers are ‘approximately equal’.
“FOR” CONSTRUCT
• The general form of the for statement is as follows:
for(initialization; TestExpr; updating)
statement;
for construct
flow chart
EXAMPLE
int main()
{
int n, s=0, r;
printf(“n Enter the Number”);
scanf(“%d”, &n);
for(;n>0;n/=10)
{
r=n%10;
s=s+r;
}
printf(“n Sum of digits %d”, s);
return 0;
}
“DO-WHILE” CONSTRUCT
• The form of this loop construct is as follows:
do
{
statement; /* body of statements would be
placed here*/
} while(TestExpression);
• With a do-while statement, the body of the loop is
executed first and the test expression is checked
after the loop body is executed.
• Thus, the do-while statement always executes the
loop body at least once.
#include <stdio.h>
int main()
{
int x = 1;
int count = 0;
do {
scanf(“%d”, &x);
if(x >= 0) count += 1;
} while(x >= 0);
return 0;
}
Some methods of controlling repetition in a
program are:
 Using Sentinel Values
 Using Prime Read
 Using Counter
GOTO STATEMENT
• The control is unconditionally transferred to
the statement associated with the label
specified in the goto statement.
• The form of a goto statement is
goto label_name;
The following program is used to find the factorial of a
number.
int main()
{
int n, c;
long int f=1;
printf(“n Enter the number:”);
scanf(“%d”,&n);
if(n<0)
goto end;
for(c=1; c<=n; c++)
f*=c;
printf(“n FACTORIAL IS %ld”, f);
end:
}
“BREAK” AND “CONTINUE”
break continue
1. It helps to make an early
exit from the block where it
appears.
1. It helps in avoiding the
remaining statements in a
current iteration of the loop
and continuing with the next
Iteration
2. It can be used in all control
statements including switch
construct.
2. It can be used only in loop
constructs.
NESTED LOOPS
 A nested loop refers to a
loop that is contained
within another loop.
 If the following output
has to be obtained on the
screen
1
2 2
3 3 3
4 4 4 4
then the corresponding
program will be
#include <stdio.h>
int main()
{
int row, col;
for(row=1;row<=4;++row)
{
for(col=1;col<=row;++col)
printf(“%d t”, row);
printf(“n”);
}
return 0;
}

More Related Content

What's hot

Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
Ashim Lamichhane
 
Introduction to Tiny OS
Introduction to Tiny OSIntroduction to Tiny OS
Introduction to Tiny OS
Sudharsan S
 
C language unit-1
C language unit-1C language unit-1
Embedded system Design
Embedded system DesignEmbedded system Design
Embedded system Design
AJAL A J
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
Thooyavan Venkatachalam
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr Microcontrollers
Mohamed Tarek
 
Introduction to embedded systems
Introduction to embedded systemsIntroduction to embedded systems
Introduction to embedded systems
EslamSaid16
 
System programming note
System programming noteSystem programming note
System programming note
SANTOSH RATH
 
CAN Bus
CAN BusCAN Bus
8086 modes
8086 modes8086 modes
8086 modes
PDFSHARE
 
Data structures using c
Data structures using cData structures using c
Data structures using c
Prof. Dr. K. Adisesha
 
Learn C# Programming - Variables & Constants
Learn C# Programming - Variables & ConstantsLearn C# Programming - Variables & Constants
Learn C# Programming - Variables & Constants
Eng Teong Cheah
 
Computer instruction
Computer instructionComputer instruction
Computer instruction
Sanjeev Patel
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
Corrado Santoro
 
Cache memoy designed by Mohd Tariq
Cache memoy designed by Mohd TariqCache memoy designed by Mohd Tariq
Cache memoy designed by Mohd Tariq
Mohd Tariq
 
The evolution of TMS, family of DSP\'s
The evolution of TMS, family of DSP\'sThe evolution of TMS, family of DSP\'s
The evolution of TMS, family of DSP\'s
Ritul Sonania
 
CNS UNIT-II.pptx
CNS UNIT-II.pptxCNS UNIT-II.pptx
CNS UNIT-II.pptx
nandan543979
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
narmadhakin
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programming
Kartik Sharma
 

What's hot (20)

Ascii
AsciiAscii
Ascii
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Introduction to Tiny OS
Introduction to Tiny OSIntroduction to Tiny OS
Introduction to Tiny OS
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
Embedded system Design
Embedded system DesignEmbedded system Design
Embedded system Design
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr Microcontrollers
 
Introduction to embedded systems
Introduction to embedded systemsIntroduction to embedded systems
Introduction to embedded systems
 
System programming note
System programming noteSystem programming note
System programming note
 
CAN Bus
CAN BusCAN Bus
CAN Bus
 
8086 modes
8086 modes8086 modes
8086 modes
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Learn C# Programming - Variables & Constants
Learn C# Programming - Variables & ConstantsLearn C# Programming - Variables & Constants
Learn C# Programming - Variables & Constants
 
Computer instruction
Computer instructionComputer instruction
Computer instruction
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
 
Cache memoy designed by Mohd Tariq
Cache memoy designed by Mohd TariqCache memoy designed by Mohd Tariq
Cache memoy designed by Mohd Tariq
 
The evolution of TMS, family of DSP\'s
The evolution of TMS, family of DSP\'sThe evolution of TMS, family of DSP\'s
The evolution of TMS, family of DSP\'s
 
CNS UNIT-II.pptx
CNS UNIT-II.pptxCNS UNIT-II.pptx
CNS UNIT-II.pptx
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programming
 

Similar to C-Programming Control statements.pptx

Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
JavvajiVenkat
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
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
Rai University
 
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
Rai University
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
Rai University
 
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
Rai University
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
Manzoor ALam
 
Session 3
Session 3Session 3
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
Rai University
 
Control statements
Control statementsControl statements
Control statements
CutyChhaya
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
M C6java6
M C6java6M C6java6
M C6java6
mbruggen
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
ssuserfb3c3e
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structures
nikshaikh786
 

Similar to C-Programming Control statements.pptx (20)

Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow 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
 
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
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
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
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
Session 3
Session 3Session 3
Session 3
 
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
 
Control statements
Control statementsControl statements
Control statements
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptx
 
M C6java6
M C6java6M C6java6
M C6java6
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structures
 

More from LECO9

C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
LECO9
 
Basic Electronics.pptx
Basic Electronics.pptxBasic Electronics.pptx
Basic Electronics.pptx
LECO9
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptxIntro to Microcontroller.pptx
Intro to Microcontroller.pptx
LECO9
 
PIC_Intro.pptx
PIC_Intro.pptxPIC_Intro.pptx
PIC_Intro.pptx
LECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
LECO9
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
LECO9
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
LECO9
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
LECO9
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
LECO9
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
LECO9
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
LECO9
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
LECO9
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
LECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
LECO9
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
LECO9
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
LECO9
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
LECO9
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
LECO9
 
COMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptxCOMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptx
LECO9
 

More from LECO9 (20)

C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
 
Basic Electronics.pptx
Basic Electronics.pptxBasic Electronics.pptx
Basic Electronics.pptx
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptxIntro to Microcontroller.pptx
Intro to Microcontroller.pptx
 
PIC_Intro.pptx
PIC_Intro.pptxPIC_Intro.pptx
PIC_Intro.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
 
COMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptxCOMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptx
 

Recently uploaded

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
TaghreedAltamimi
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
riddhimaagrawal986
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
GauravCar
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 

Recently uploaded (20)

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 

C-Programming Control statements.pptx

  • 2. LEARNING OBJECTIVES  Understanding meaning of a statement and statement block.  Learn about decision type control constructs in C and the way these are used.  Learn about looping type control constructs in C and the technique of putting them to use.  Learn the use of special control constructs such as goto, break, continue, and return.  Learn about nested loops and their utility.
  • 3. CONTROL STATEMENTS INCLUDE Selection Statements • if • if-else • switch Iteration Statements • for • while • do-while Jump Statements • goto • break • continue • return
  • 4. PROGRAM CONTROL STATEMENTS/CONSTRUCTS Program Control Statements/Constructs Selection/Branching Conditional Type if if-else if-else- if switch Unconditional Type break continue goto Iteration/Looping for while do- while
  • 5. CONDITIONAL EXECUTION AND SELECTION • Selection Statements • The Conditional Operator • The switch Statement
  • 6. SELECTION STATEMENTS • One-way decisions using if statement • Two-way decisions using if-else statement • Multi-way decisions • Dangling else Problem
  • 7. ONE-WAY DECISIONS USING IF STATEMENT Flowchart for if construct TestExpr stmtT
  • 8. WRITE A PROGRAM THAT PRINTS THE LARGEST AMONG THREE NUMBERS. Algorithm C Program 1. START #include <stdio.h> int main() { int a, b, c, max; printf(“nEnter 3 numbers”); scanf(“%d %d %d”, &a, &b, &c); max=a; if(b>max) max=b; if(c>max) max=c; printf(“Largest No is %d”, max); return 0; } 2. PRINT “ENTER THREE NUMBERS” 3. INPUT A, B, C 4. MAX=A 5. IF B>MAX THEN MAX=B 6. IF C>MAX THEN MAX=C 7. PRINT “LARGEST NUMBER IS”, MAX 8. STOP
  • 9. TWO-WAY DECISIONS USING IF-ELSE STATEMENT Flowchart of if-else construct The form of a two-way decision is as follows: if(TestExpr) stmtT; else stmtF; TestExpr stmtT stmtF
  • 10. WRITE A PROGRAM THAT PRINTS THE LARGEST AMONG THREE NUMBERS. Algorithm C Program 1. START #include <stdio.h> int main() { int a, b, c, max; printf(“nEnter 3 numbers”); scanf(“%d %d %d”, &a, &b, &c); max=a; if(b>max) max=b; if(c>max) max=c; printf(“Largest No is %d”, max); return 0; } 2. PRINT “ENTER THREE NUMBERS” 3. INPUT A, B, C 4. MAX=A 5. IF B>MAX THEN MAX=B 6. IF C>MAX THEN MAX=C 7. PRINT “LARGEST NUMBER IS”, MAX 8. STOP
  • 11. MULTI-WAY DECISIONS if-else-if ladder if(TestExpr1) stmtT1; else if(TestExpr2) stmtT2; else if(TestExpr3) stmtT3; .. . else if(TestExprN) stmtTN; else stmtF; General format of switch statements switch(expr) { case constant1: stmtList1; break; case constant2: stmtList2; break; case constant3: stmtList3; break; …………………………. …………………………. default: stmtListn; }
  • 12. FLOWCHART OF AN IF- ELSE-IF CONSTRUCT TestExpr TestExpr2 TestExprN TestExpr3 stmtT2 stmtTN stmtT3 stmtTF stmtT1
  • 13. THE FOLLOWING PROGRAM CHECKS WHETHER A NUMBER GIVEN BY THE USER IS ZERO, POSITIVE, OR NEGATIVE int main() { int x; printf(“n ENTER THE NUMBER:”); scanf(“%d”, &x); if(x > 0) printf(“x is positive n”); else if(x == 0) printf(“x is zero n”); else printf(“x is negative n”); return 0;
  • 14. NESTED IF • When any if statement is written under another if statement, this cluster is called a nested if. • The syntax for the nested is given here: Construct 1 Construct 2 if(TestExprA) if(TestExprB) stmtBT; else stmtBF; else stmtAF; if(TestExprA) if(TestExprB) stmtBT; else stmtBF; else if(TestExprC) stmtCT; else stmtCF;
  • 15. A PROGRAM TO FIND THE LARGEST AMONG THREE NUMBERS USING THE NESTED LOOP int main() { int a, b, c; printf(“nEnter the three numbers”); scanf(“%d %d %d”, &a, &b, &c); if(a > b) if(a > c) printf(“%d”, a); else printf(“%d”, c); else if(b > c) printf(“%d”, b); else printf(“%d”, c); return 0; }
  • 16. DANGLING ELSE PROBLEM • This classic problem occurs when there is no matching else for each if. To avoid this problem, the simple C rule is that always pair an else to the most recent unpaired if in the current block. • The else is automatically paired with the closest if. But, it may be needed to associate an else with the outer if also.
  • 17. SOLUTIONS TO DANGLING ELSE PROBLEM • Use of null else • Use of braces to enclose the true action of the second if With null else With braces if(TestExprA) if(TestExprB ) stmtBT; else ; else stmtAF; if(TestExprA) { if(TestExprB ) stmtBT; } else stmtAF;
  • 18. THE SWITCH STATEMENT The C switch construct switch(expr) { case constant1: stmtList1; break; case constant2: stmtList2; break; case constant3: stmtList3; break; …………………………. …………………………. default: stmtListn; }
  • 19. SWITCH VS NESTED IF  The switch differs from the else-if in that switch can test only for equality, whereas the if conditional expression can be of a test expression involving any type of relational operators and/or logical operators.  A switch statement is usually more efficient than nested ifs.  The switch statement can always be replaced with a series of else-if statements.
  • 20. ITERATION AND REPETITIVE EXECUTION • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded iteration or unbounded loop and bounded iteration or bounded loop. • A loop can either be a pre-test loop or be a post-test loop as illustrated in the diagram.
  • 21. “WHILE” CONSTRUCT Expanded Syntax of “while” and its Flowchart Representation • while statement is a pretest loop. The basic syntax of the while statement is shown below:
  • 22. AN EXAMPLE #include <stdio.h> int main() { int c; c=5; // Initialization while(c>0) { // Test Expression printf(“ n %d”,c); c=c-1; // Updating } return 0; } This loop contains all the parts of a while loop. When executed in a program, this loop will output 5 4 3 2 1
  • 23. TESTING FOR FLOATING-POINT ‘EQUALITY’ float x; x = 0.0; while(x != 1.1) { x = x + 0.1; printf(“1.1 minus %f equals %.20gn”, x, 1.1 -x); } • The above loop never terminates on many computers, because 0.1 cannot be accurately represented using binary numbers. • The correct way to make the test is to see if the two numbers are ‘approximately equal’.
  • 24. “FOR” CONSTRUCT • The general form of the for statement is as follows: for(initialization; TestExpr; updating) statement; for construct flow chart
  • 25. EXAMPLE int main() { int n, s=0, r; printf(“n Enter the Number”); scanf(“%d”, &n); for(;n>0;n/=10) { r=n%10; s=s+r; } printf(“n Sum of digits %d”, s); return 0; }
  • 26. “DO-WHILE” CONSTRUCT • The form of this loop construct is as follows: do { statement; /* body of statements would be placed here*/ } while(TestExpression);
  • 27. • With a do-while statement, the body of the loop is executed first and the test expression is checked after the loop body is executed. • Thus, the do-while statement always executes the loop body at least once.
  • 28. #include <stdio.h> int main() { int x = 1; int count = 0; do { scanf(“%d”, &x); if(x >= 0) count += 1; } while(x >= 0); return 0; }
  • 29. Some methods of controlling repetition in a program are:  Using Sentinel Values  Using Prime Read  Using Counter
  • 30. GOTO STATEMENT • The control is unconditionally transferred to the statement associated with the label specified in the goto statement. • The form of a goto statement is goto label_name;
  • 31. The following program is used to find the factorial of a number. int main() { int n, c; long int f=1; printf(“n Enter the number:”); scanf(“%d”,&n); if(n<0) goto end; for(c=1; c<=n; c++) f*=c; printf(“n FACTORIAL IS %ld”, f); end: }
  • 32. “BREAK” AND “CONTINUE” break continue 1. It helps to make an early exit from the block where it appears. 1. It helps in avoiding the remaining statements in a current iteration of the loop and continuing with the next Iteration 2. It can be used in all control statements including switch construct. 2. It can be used only in loop constructs.
  • 33. NESTED LOOPS  A nested loop refers to a loop that is contained within another loop.  If the following output has to be obtained on the screen 1 2 2 3 3 3 4 4 4 4 then the corresponding program will be #include <stdio.h> int main() { int row, col; for(row=1;row<=4;++row) { for(col=1;col<=row;++col) printf(“%d t”, row); printf(“n”); } return 0; }