SlideShare a Scribd company logo
1 of 35
UNIT-II
INPUT OUTPUT & CONTROL
STATEMENTS
CONTROL STATEMENTS
⚫C language supports the following statements known
as control or decision making statements.
1. if statement
2. switchstatement
3. conditional operatorstatement
4. Go tostatement
IF STATEMENT
⚫The if statement is used to control the flow of
execution of statementsand is of the form
⚫If(testexpression)
⚫Eg:
if(bank balance is zero)
Borrow money
Cont..
⚫The if statement may be implemented in different
forms depending on the complexity of conditions
to be tested.
1. Simple if statement
2. if…..elsestatement
3. Nested if…..elsestatement
4. elseif ladder
The general form of a simple
if statement is The
‘statement-block’ may be a
single statement or a group
of statement
If(testexprn)
{
statement-block;
}
statement-x;
Cont..
THE IF…ELSE STATEMENT
⚫The if….else statement is an extension
statement. The general form is
of simple if
If(testexpression)
{
True-blockstatement(s)
}
else
{
False-block statement(s)
}
statement-x
Cont..
⚫If the test expression is true, then the true block statements
are executed; otherwise the false block statement will be
executed.
Cont..
⚫Eg:
………
………
if(code ==1)
boy = boy + 1;
if(code == 2)
girl =girl + 1;
………
………
NESTING OF IF…..ELSE STATEMENTS
⚫When a series of decisionsare involved, we may have
to use more than one if….else statements, in nested
form as follows
Cont..
Here, if the condition 1 is false then it
skipped tostatement 3.
But if the condition 1 is true, then it
testscondition 2.

If condition 2 is true then it executes
statement 1 and if false then it executes
statement 2.
 Then the control is transferred to the
statement x.
This can also be shown by the following
flowchart,
Program
/*Selecting the largestof threevalues*/
main()
{
floatA, B, C;
printf(“Enter three values n”);
scanf(“|%f %f %f”,&A, &B, &C);
printf(“nLargest value is:”);
if(A > B)
{ if(A > C)
printf(“%f n”,A);
else
printf(“%f n”,C);
}
else
{
if(C > B)
printf(“%f n”,C);
else
printf(“%f n”,B);
}
}
⚫OUTPUT
⚫Enter threevalues:
⚫5 8 24
⚫Largestvalue is 24
The else if ladder
When a multipathdecision is involved then we use else if ladder.
A multipath decision is a chain of ifs in which the statement associated
witheach else is an if.
Ittakes the following general form,
Cont..
 This construct is known as the else if ladder. The conditions are
evaluated from the top, downwards. This can be shown by the
following flowchart
THE SWITCH STATEMENT
⚫Switch statement is used for complex programs when
the numberof alternatives increases.
⚫The switch statement tests the value of the given
variable against the list of case values and when a
match is found, a block of statements associated with
thatcase is executed.
SWITCH STATEMENT
⚫ Thegeneral form of switch statement is
switch(expression)
{
casevalue-1:
block-1
break;
casevalue-2:
block-2
break;
…….
…….
default:
default-block
break;
}
statement-x;
Example:
index = marks / 10;
switch(index)
{
case 10:
case 9:
case 8:
grade = “Honours”;
break;
case 7:
case 6:
grade = “firstdivision”;
break;
case 5:
grade = “second
division”;
break;
case 4:
grade = “third
division”;
break;
default:
grade = “first
division”; break
}
printf(“%s n”,grade);
………….
THE ?: OPERATOR
⚫ The C language has an unusual operator, useful for making two-
waydecisions.
⚫ This operator is a combination of ? and : and takes three
operands.
⚫ It isof the form exp1?exp2:exp 3
⚫ Here exp1 is evaluated first. If it is true then the expression exp2 is
evaluated and becomes thevalue of theexpression.
⚫ If exp1 is false then exp3 is evaluated and its value becomes the
valueof theexpression.
Eg:
if(x < 0)
flag = 0;
else
flag = 1;
can be written as
flag = (x < 0)? 0 : 1;
UNCONDITIONAL STATEMENTS - THE GOTO STATEMENT
C supports the goto statement to
branchunconditionally from one point of the
program toanother.
The goto requires a label in order to identify
the place where the branch is to be made.
A label is any valid variable name and
must be followed byacolon.
DECISION MAKING AND LOOPING
 In looping, a sequence of statements are executed until some
conditions for terminationof the loopare satisfied.
 In looping, a sequence of statements are executed until some
conditions for terminationof the loopare satisfied.
 A program loop therefore consists of two segments, one known
as the body of the loop and the other known as the control
statements.
Cont..
Depending on the position of the control statements in
the loop, a control structure may be classified either as an
entry-controlled loop or as the exit-controlled loop.
Loops In C
⚫The C language provides for three loop constructs for
performing loopoperations.
⚫Theyare:
The whilestatement
The dostatement
The forstatement
THE WHILE STATEMENT
⚫The basic formatof the while statement is
while(testcondition)
{
bodyof the loop
}
⚫ The while isan entry–controlled loop statement.
⚫ The test-condition is evaluated and if the condition is true, then the
bodyof the loop is executed.
⚫ After execution of the body, the test-condition is once again evaluated and if
it is true, the body is executed onceagain.
⚫ This process of repeated execution of the body continues until the test-
condition finally becomes false and the control is transferred out of the loop.
Example of WHILE Loop
Bodyof the loop
test
condn?
while(testcondition)
{
bodyof the loop
}
sum = 0;
n = 1;
while(n <= 10)
{
sum = sum + n* n;
n = n + 1;
}
printf(“sum = %d n”,sum);
THE DO STATEMENT
⚫In while loop the body of the loop may not be executed at
all if the condition is not satisfied at the very first attempt.
⚫Such situations can be handled with the help of the do
statement.
do
{
body of the loop
}
while(test condition);
Cont..
⚫Since the test-condition is evaluated at the bottom of the loop,
the do…..while construct provides an exit-controlled loop and
therefore the body of the loop is always executed at least once.
Eg:
do
{
printf(“Inputa numbern”);
number= getnum();
}
while(number> 0);
THE FOR STATEMENT
⚫The for loop is another entry-controlled loop
provides a more concise loop control structure
⚫Thegeneral formof the for loop is
that
for(initialization ; test-condition ; increment
{
body of the loop
}
Cont..
⚫ The execution of the forstatement isas follows:
 Initialization of the control variables is done first.
 Thevalue of thecontrol variable is tested using the test-condition.
 If the condition is true, the body of the loop is executed; otherwise
the loop is terminated and the execution continues with the
statementthat immediately follows the loop.
 When the body of the loop is executed, the control is transferred
back to the for statement after evaluating the last statement in
the loop.
 Now, the control variable is either incremented or decremented as
perthecondition.
For Statement
⚫Eg 1)
for(x = 0; x <= 9; x = x + 1)
{
printf)”%d”
,x);
}
printf(“n”);
⚫ The multiplearguments in the incrementsectionare possible
and separated by commas.
Cont..
⚫Eg 2)
sum = 0;
for(i = 1; i < 20 && sum <100; ++i)
{
sum =sum + i;
printf(“%d %d n”,sum);
}
for(initialization ; test-condition ; increment
{
bodyof the loop
}
Nesting of For Loops
⚫C allowsone forstatementwithin anotherfor
statement.
Cont..
⚫ Eg:
⚫
⚫
⚫ for(row = 1; row <= ROWMAX; ++row)
⚫ {
⚫ for(column = 1; column < = COLMAX; ++column)
⚫ {
⚫ y = row * column;
⚫ printf(“%4d”
, y);
⚫ }
⚫ printf(“n”);
⚫ }
⚫
JUMPS IN LOOPS
 C permits a jump from one statement to another within a
loop as well as the jumpoutof a loop.
Jumping outof a Loop
 An early exit from a loop can be accomplished by using the
break statementor thegoto statement.
 When the break statement is encountered inside a loop, the
loop is immediately exited and the program continues with
thestatement immediately following the loop.
 When the loops are nested, the break would only exit from
the loop containing it. That is, the break will exit only a
single loop.
Skipping a part of a Loop
⚫Like the break statement, C supports another similar
statementcalled thecontinue statement.
⚫However, unlike the break which causes the loop to be
terminated, the continue, as the name implies, causes the
loop to be continued with the next iteration after skipping
any statements in between.
⚫The continue statement tells the compiler,
FOLLOWING STATEMENTS AND CONTINUE WITH
ITERATION”.
⚫The formatof thecontinue statement is simply
“SKIP THE
THE NEXT
continue;
Bypassing and continuing I Loops
References
1. http://www.computer-books.us/c_0008.php
2. http://www.computer-books.us/c_0009
3. http://www.computer-books.us/c_2.php
4. www.tutorialspoint.com/cprogramming/cprogramming_pdf.
5. Programming in C by yashwant kanitkar
6. ANSI C by E.balagurusamy- TMG publication
7. Computer programming and Utilization by sanjay shah Mahajan Publication
8. www.cprogramming.com/books.html

More Related Content

What's hot

Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentationManeesha Caldera
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statementsjyoti_lakhani
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Control Structure in C
Control Structure in CControl Structure in C
Control Structure in CNeel Shah
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision ControlJayfee Ramos
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 

What's hot (20)

Control statement
Control statementControl statement
Control statement
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Control statements
Control statementsControl statements
Control statements
 
Control Structure in C
Control Structure in CControl Structure in C
Control Structure in C
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
M C6java6
M C6java6M C6java6
M C6java6
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Control statements
Control statementsControl statements
Control statements
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Control Structures
Control StructuresControl Structures
Control Structures
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 

Similar to CONTROL STMTS.pptx

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
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsLovelitJose
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Control structures
Control structuresControl structures
Control structuresElakkiyaS11
 
C statements
C statementsC statements
C statementsAhsann111
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
C++ decision making
C++ decision makingC++ decision making
C++ decision makingZohaib Ahmed
 

Similar to CONTROL STMTS.pptx (20)

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
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Control structures
Control structuresControl structures
Control structures
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
C statements
C statementsC statements
C statements
 
UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Flow of control
Flow of controlFlow of control
Flow of control
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 

More from JavvajiVenkat

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 Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
Nested Loops in C unit2.docx
Nested Loops in C unit2.docxNested Loops in C unit2.docx
Nested Loops in C unit2.docxJavvajiVenkat
 

More from JavvajiVenkat (6)

itretion.docx
itretion.docxitretion.docx
itretion.docx
 
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
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Nested Loops in C unit2.docx
Nested Loops in C unit2.docxNested Loops in C unit2.docx
Nested Loops in C unit2.docx
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 

Recently uploaded

Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 

Recently uploaded (20)

Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 

CONTROL STMTS.pptx

  • 1. UNIT-II INPUT OUTPUT & CONTROL STATEMENTS
  • 2. CONTROL STATEMENTS ⚫C language supports the following statements known as control or decision making statements. 1. if statement 2. switchstatement 3. conditional operatorstatement 4. Go tostatement
  • 3. IF STATEMENT ⚫The if statement is used to control the flow of execution of statementsand is of the form ⚫If(testexpression) ⚫Eg: if(bank balance is zero) Borrow money
  • 4. Cont.. ⚫The if statement may be implemented in different forms depending on the complexity of conditions to be tested. 1. Simple if statement 2. if…..elsestatement 3. Nested if…..elsestatement 4. elseif ladder
  • 5. The general form of a simple if statement is The ‘statement-block’ may be a single statement or a group of statement If(testexprn) { statement-block; } statement-x; Cont..
  • 6. THE IF…ELSE STATEMENT ⚫The if….else statement is an extension statement. The general form is of simple if If(testexpression) { True-blockstatement(s) } else { False-block statement(s) } statement-x
  • 7. Cont.. ⚫If the test expression is true, then the true block statements are executed; otherwise the false block statement will be executed.
  • 8. Cont.. ⚫Eg: ……… ……… if(code ==1) boy = boy + 1; if(code == 2) girl =girl + 1; ……… ………
  • 9. NESTING OF IF…..ELSE STATEMENTS ⚫When a series of decisionsare involved, we may have to use more than one if….else statements, in nested form as follows
  • 10. Cont.. Here, if the condition 1 is false then it skipped tostatement 3. But if the condition 1 is true, then it testscondition 2.  If condition 2 is true then it executes statement 1 and if false then it executes statement 2.  Then the control is transferred to the statement x. This can also be shown by the following flowchart,
  • 11. Program /*Selecting the largestof threevalues*/ main() { floatA, B, C; printf(“Enter three values n”); scanf(“|%f %f %f”,&A, &B, &C); printf(“nLargest value is:”); if(A > B) { if(A > C) printf(“%f n”,A); else printf(“%f n”,C); } else { if(C > B) printf(“%f n”,C); else printf(“%f n”,B); } } ⚫OUTPUT ⚫Enter threevalues: ⚫5 8 24 ⚫Largestvalue is 24
  • 12. The else if ladder When a multipathdecision is involved then we use else if ladder. A multipath decision is a chain of ifs in which the statement associated witheach else is an if. Ittakes the following general form,
  • 13. Cont..  This construct is known as the else if ladder. The conditions are evaluated from the top, downwards. This can be shown by the following flowchart
  • 14. THE SWITCH STATEMENT ⚫Switch statement is used for complex programs when the numberof alternatives increases. ⚫The switch statement tests the value of the given variable against the list of case values and when a match is found, a block of statements associated with thatcase is executed.
  • 15. SWITCH STATEMENT ⚫ Thegeneral form of switch statement is switch(expression) { casevalue-1: block-1 break; casevalue-2: block-2 break; ……. ……. default: default-block break; } statement-x;
  • 16. Example: index = marks / 10; switch(index) { case 10: case 9: case 8: grade = “Honours”; break; case 7: case 6: grade = “firstdivision”; break; case 5: grade = “second division”; break; case 4: grade = “third division”; break; default: grade = “first division”; break } printf(“%s n”,grade); ………….
  • 17. THE ?: OPERATOR ⚫ The C language has an unusual operator, useful for making two- waydecisions. ⚫ This operator is a combination of ? and : and takes three operands. ⚫ It isof the form exp1?exp2:exp 3 ⚫ Here exp1 is evaluated first. If it is true then the expression exp2 is evaluated and becomes thevalue of theexpression. ⚫ If exp1 is false then exp3 is evaluated and its value becomes the valueof theexpression. Eg: if(x < 0) flag = 0; else flag = 1; can be written as flag = (x < 0)? 0 : 1;
  • 18. UNCONDITIONAL STATEMENTS - THE GOTO STATEMENT C supports the goto statement to branchunconditionally from one point of the program toanother. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name and must be followed byacolon.
  • 19. DECISION MAKING AND LOOPING  In looping, a sequence of statements are executed until some conditions for terminationof the loopare satisfied.  In looping, a sequence of statements are executed until some conditions for terminationof the loopare satisfied.  A program loop therefore consists of two segments, one known as the body of the loop and the other known as the control statements.
  • 20. Cont.. Depending on the position of the control statements in the loop, a control structure may be classified either as an entry-controlled loop or as the exit-controlled loop.
  • 21. Loops In C ⚫The C language provides for three loop constructs for performing loopoperations. ⚫Theyare: The whilestatement The dostatement The forstatement
  • 22. THE WHILE STATEMENT ⚫The basic formatof the while statement is while(testcondition) { bodyof the loop } ⚫ The while isan entry–controlled loop statement. ⚫ The test-condition is evaluated and if the condition is true, then the bodyof the loop is executed. ⚫ After execution of the body, the test-condition is once again evaluated and if it is true, the body is executed onceagain. ⚫ This process of repeated execution of the body continues until the test- condition finally becomes false and the control is transferred out of the loop.
  • 23. Example of WHILE Loop Bodyof the loop test condn? while(testcondition) { bodyof the loop } sum = 0; n = 1; while(n <= 10) { sum = sum + n* n; n = n + 1; } printf(“sum = %d n”,sum);
  • 24. THE DO STATEMENT ⚫In while loop the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. ⚫Such situations can be handled with the help of the do statement. do { body of the loop } while(test condition);
  • 25. Cont.. ⚫Since the test-condition is evaluated at the bottom of the loop, the do…..while construct provides an exit-controlled loop and therefore the body of the loop is always executed at least once. Eg: do { printf(“Inputa numbern”); number= getnum(); } while(number> 0);
  • 26. THE FOR STATEMENT ⚫The for loop is another entry-controlled loop provides a more concise loop control structure ⚫Thegeneral formof the for loop is that for(initialization ; test-condition ; increment { body of the loop }
  • 27. Cont.. ⚫ The execution of the forstatement isas follows:  Initialization of the control variables is done first.  Thevalue of thecontrol variable is tested using the test-condition.  If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statementthat immediately follows the loop.  When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop.  Now, the control variable is either incremented or decremented as perthecondition.
  • 28. For Statement ⚫Eg 1) for(x = 0; x <= 9; x = x + 1) { printf)”%d” ,x); } printf(“n”); ⚫ The multiplearguments in the incrementsectionare possible and separated by commas.
  • 29. Cont.. ⚫Eg 2) sum = 0; for(i = 1; i < 20 && sum <100; ++i) { sum =sum + i; printf(“%d %d n”,sum); } for(initialization ; test-condition ; increment { bodyof the loop }
  • 30. Nesting of For Loops ⚫C allowsone forstatementwithin anotherfor statement.
  • 31. Cont.. ⚫ Eg: ⚫ ⚫ ⚫ for(row = 1; row <= ROWMAX; ++row) ⚫ { ⚫ for(column = 1; column < = COLMAX; ++column) ⚫ { ⚫ y = row * column; ⚫ printf(“%4d” , y); ⚫ } ⚫ printf(“n”); ⚫ } ⚫
  • 32. JUMPS IN LOOPS  C permits a jump from one statement to another within a loop as well as the jumpoutof a loop. Jumping outof a Loop  An early exit from a loop can be accomplished by using the break statementor thegoto statement.  When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with thestatement immediately following the loop.  When the loops are nested, the break would only exit from the loop containing it. That is, the break will exit only a single loop.
  • 33. Skipping a part of a Loop ⚫Like the break statement, C supports another similar statementcalled thecontinue statement. ⚫However, unlike the break which causes the loop to be terminated, the continue, as the name implies, causes the loop to be continued with the next iteration after skipping any statements in between. ⚫The continue statement tells the compiler, FOLLOWING STATEMENTS AND CONTINUE WITH ITERATION”. ⚫The formatof thecontinue statement is simply “SKIP THE THE NEXT continue;
  • 35. References 1. http://www.computer-books.us/c_0008.php 2. http://www.computer-books.us/c_0009 3. http://www.computer-books.us/c_2.php 4. www.tutorialspoint.com/cprogramming/cprogramming_pdf. 5. Programming in C by yashwant kanitkar 6. ANSI C by E.balagurusamy- TMG publication 7. Computer programming and Utilization by sanjay shah Mahajan Publication 8. www.cprogramming.com/books.html