SlideShare a Scribd company logo
1 of 33
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

USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
UVM Update: Register Package
UVM Update: Register PackageUVM Update: Register Package
UVM Update: Register PackageDVClub
 
System verilog verification building blocks
System verilog verification building blocksSystem verilog verification building blocks
System verilog verification building blocksNirav Desai
 
Union in c language
Union  in c languageUnion  in c language
Union in c languagetanmaymodi4
 
Chapter1 - Signal and System
Chapter1 - Signal and SystemChapter1 - Signal and System
Chapter1 - Signal and SystemAttaporn Ninsuwan
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...jycazihi6460
 
verilog code for logic gates
verilog code for logic gatesverilog code for logic gates
verilog code for logic gatesRakesh kumar jha
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slidesrfojdar
 
Python ppt.pdf
Python ppt.pdfPython ppt.pdf
Python ppt.pdfkalai75
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming LanguageRamaBoya2
 

What's hot (20)

USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
UVM Update: Register Package
UVM Update: Register PackageUVM Update: Register Package
UVM Update: Register Package
 
Structures
StructuresStructures
Structures
 
VHDL - Part 2
VHDL - Part 2VHDL - Part 2
VHDL - Part 2
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Arduino Functions
Arduino FunctionsArduino Functions
Arduino Functions
 
System verilog verification building blocks
System verilog verification building blocksSystem verilog verification building blocks
System verilog verification building blocks
 
Loops
LoopsLoops
Loops
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Chapter1 - Signal and System
Chapter1 - Signal and SystemChapter1 - Signal and System
Chapter1 - Signal and System
 
CAN BUS.ppt
CAN BUS.pptCAN BUS.ppt
CAN BUS.ppt
 
Uvm dac2011 final_color
Uvm dac2011 final_colorUvm dac2011 final_color
Uvm dac2011 final_color
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Character set of c
Character set of cCharacter set of c
Character set of c
 
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
 
verilog code for logic gates
verilog code for logic gatesverilog code for logic gates
verilog code for logic gates
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
 
Python ppt.pdf
Python ppt.pdfPython ppt.pdf
Python ppt.pdf
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
 

Similar to C-Programming Control statements.pptx

Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
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 statementsRai 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 statementsRai University
 
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
 
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
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loopsManzoor ALam
 
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
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
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.pptxJavvajiVenkat
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structuresnikshaikh786
 

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.pptLECO9
 
Embedded Systems.pptx
Embedded Systems.pptxEmbedded Systems.pptx
Embedded Systems.pptxLECO9
 
Basic Electronics.pptx
Basic Electronics.pptxBasic Electronics.pptx
Basic Electronics.pptxLECO9
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptxIntro to Microcontroller.pptx
Intro to Microcontroller.pptxLECO9
 
PIC_Intro.pptx
PIC_Intro.pptxPIC_Intro.pptx
PIC_Intro.pptxLECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxLECO9
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxLECO9
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptxLECO9
 
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.pptxLECO9
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxLECO9
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptxLECO9
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxLECO9
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptxLECO9
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptxLECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxLECO9
 
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.pptxLECO9
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptxLECO9
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptxLECO9
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptxLECO9
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxLECO9
 

More from LECO9 (20)

C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
 
Embedded Systems.pptx
Embedded Systems.pptxEmbedded Systems.pptx
Embedded Systems.pptx
 
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
 

Recently uploaded

457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptxrouholahahmadi9876
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...vershagrag
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...jabtakhaidam7
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...gragchanchal546
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 

Recently uploaded (20)

457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 

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; }