SlideShare a Scribd company logo
Lectures on Numerical Methods 1
Statements
 Expressions, when terminated by a semicolon, become statements.
 Examples
X = 5; I++; IsPrime(c); c = 5 * ( f – 32 ) / 9
 One can construct compound statements by putting statements and
declarations in Braces { and }. Also called blocks.
 Example
if ( rad > 0.0 )
{
float area = pi * rad * rad;
float peri = 2 * pi * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
Lectures on Numerical Methods 2
Labeled statements
 One can give a label to any statement. The form is
 Identifier : statement
 There are other labeled statements like case and default, to be used with
switch statement.
 Example
Lectures on Numerical Methods 3
If – else
 The if-else statement expresses simplest decision making. The
syntax is
if (expression)
statement1
elseopt
Statement2
 The expression is evaluated and if it is nonzero (true) then the
statement1 is executed. Otherwise, if else is present statement2 is
executed.
 Expression can just be numeric and need not be conditional
expression only.
 Statement1 and statement2 may be compound statements or blocks.
Lectures on Numerical Methods 4
If – else
if ( n > 0 )
if ( isPrime(n) ) {
printf(“%d is primen”,n);
return;
}
else
printf(“Input must be positiven”,n);
printf(“%d is non-primen”,n);
 Each else is associated with closest previous else-less if. Using this
principle multi-way decisions can be constructed.
Lectures on Numerical Methods 5
Switch
 The syntax of switch statement is
switch (expression) {
case const-expression1 : statement1
case const-expression2 : statement2
:
default : statementn
}
 All case expressions must be different. Expression must evaluate to an
integer.
 First the expression is evaluated. Then the value of expression is
compared with the case expressions. The execution begins at the case
statement, whose case expression matches. All the statements below are
executed.
 If default is present and if no other case matches then default statement
is executed.
Lectures on Numerical Methods 6
Switch
switch ( marks / 10 ) {
case 3 : grade = “DD”; break;
case 4 : grade = “CD”; break;
case 5 : grade = “CC”; break;
case 6 : grade = “BC”; break;
case 7 : grade = “BB”; break;
case 8 : grade = “AB”; break;
case 9 :
case 10 : grade = “AA”; break;
default : grade = “FF”; break;
}
Lectures on Numerical Methods 7
Iterations
 The three types of loops are given below.
while (expression) statement
for (expression1opt; expression2opt; expression3opt)
statement
do statement while(expression);
 In while loop the expression (which must be arithmetic) is evaluated and
if the value is nonzero, then the statement is executed. The process is
continued till the expression becomes zero. The expression is evaluated
before the iteration.
 For statement is equivalent to
expression1;
while ( expression2 ) {
statement
expression3;
}
Lectures on Numerical Methods 8
Iterations
 In the do loop, the statement is executed and then the expression is
evaluated. If the expression is nonzero the process is repeated. Thus
the condition is evaluated at the end of each iteration.
 Example
x1 = 1;
do {
x0 = x1;
x1 = x0 – f(x0) / derf(x0);
} while ( fabs(x1 – x0) > FLT_EPSILON ) ;
 We needed to evaluate x1 before we could apply the convergence
criterion.
Lectures on Numerical Methods 9
Break and Continue
 The loops have one expression that decides whether the iterative
process should be terminated. It is sometimes convenient to be able to
exit from the loop.
 break statement provides an early exit from the for, while and do
loops.
 It also provides an exit from switch statement.
 continue statement causes the jump to the end of the loop body,
skipping the statements only once. The loop may continue.
while (...) {
...
continue;
...
cont: ;
}
do {
...
continue;
...
cont: ;
}
for (...) {
...
continue;
...
cont: ;
}
Lectures on Numerical Methods 10
Break and continue
 Example
for ( i = 0; i < n; i++ ) {
if ( a[i] < 0 ) continue;
/* Process only non-negative elements of the array */
...
}
 Example
for ( i = 2; i <= (int)sqrt(n); i++ ) {
if ( n % i == 0 ) break;
if ( n % i ) printf(“Primen”);
else printf(“Not primen”);
Ad

More Related Content

Similar to Control structure and Looping statements (20)

Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 
Unit 2=Decision Control & Looping Statements.pdf
Unit 2=Decision Control & Looping Statements.pdfUnit 2=Decision Control & Looping Statements.pdf
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Decision making in C(2020-2021) statements
Decision making in C(2020-2021) statementsDecision making in C(2020-2021) statements
Decision making in C(2020-2021) statements
BalaKrishnan466
 
Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201
vpenubot
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf
ArkSingh7
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
SzeChingChen
 
control-statements in C Language MH.pptx
control-statements in C Language MH.pptxcontrol-statements in C Language MH.pptx
control-statements in C Language MH.pptx
mehedi_hasan
 
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
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
TejaswiB4
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
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
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 
Decision making in C(2020-2021) statements
Decision making in C(2020-2021) statementsDecision making in C(2020-2021) statements
Decision making in C(2020-2021) statements
BalaKrishnan466
 
Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201
vpenubot
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf
ArkSingh7
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
SzeChingChen
 
control-statements in C Language MH.pptx
control-statements in C Language MH.pptxcontrol-statements in C Language MH.pptx
control-statements in C Language MH.pptx
mehedi_hasan
 
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
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
TejaswiB4
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
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
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 

More from BalaKrishnan466 (14)

Switch and control statement for c language
Switch and control statement for c languageSwitch and control statement for c language
Switch and control statement for c language
BalaKrishnan466
 
c-Looping-Statements-P-Suman statement for c
c-Looping-Statements-P-Suman statement for cc-Looping-Statements-P-Suman statement for c
c-Looping-Statements-P-Suman statement for c
BalaKrishnan466
 
Switch and control looping statement for C
Switch and control looping statement for CSwitch and control looping statement for C
Switch and control looping statement for C
BalaKrishnan466
 
Switch and looping statement for c language
Switch and looping statement for c languageSwitch and looping statement for c language
Switch and looping statement for c language
BalaKrishnan466
 
C language Looping and conditional statement
C language Looping and conditional statementC language Looping and conditional statement
C language Looping and conditional statement
BalaKrishnan466
 
Control structure and Looping statements
Control structure and Looping statementsControl structure and Looping statements
Control structure and Looping statements
BalaKrishnan466
 
Control structure and Looping statements
Control structure and Looping statementsControl structure and Looping statements
Control structure and Looping statements
BalaKrishnan466
 
C array and Initialisation and declaration
C array and Initialisation and declarationC array and Initialisation and declaration
C array and Initialisation and declaration
BalaKrishnan466
 
Basic operators and it's types in c languages
Basic operators and it's types in c languagesBasic operators and it's types in c languages
Basic operators and it's types in c languages
BalaKrishnan466
 
Queues operation using data structure in c
Queues operation using data structure in cQueues operation using data structure in c
Queues operation using data structure in c
BalaKrishnan466
 
Stack operation in data structure in c language
Stack operation in data structure in c languageStack operation in data structure in c language
Stack operation in data structure in c language
BalaKrishnan466
 
Flow chart and algorithm working progress
Flow chart and algorithm working progressFlow chart and algorithm working progress
Flow chart and algorithm working progress
BalaKrishnan466
 
Algorithms and Flowchart usages in C laguage
Algorithms and Flowchart usages in C laguageAlgorithms and Flowchart usages in C laguage
Algorithms and Flowchart usages in C laguage
BalaKrishnan466
 
Data types and it's usage in c languages
Data types and it's usage in c languagesData types and it's usage in c languages
Data types and it's usage in c languages
BalaKrishnan466
 
Switch and control statement for c language
Switch and control statement for c languageSwitch and control statement for c language
Switch and control statement for c language
BalaKrishnan466
 
c-Looping-Statements-P-Suman statement for c
c-Looping-Statements-P-Suman statement for cc-Looping-Statements-P-Suman statement for c
c-Looping-Statements-P-Suman statement for c
BalaKrishnan466
 
Switch and control looping statement for C
Switch and control looping statement for CSwitch and control looping statement for C
Switch and control looping statement for C
BalaKrishnan466
 
Switch and looping statement for c language
Switch and looping statement for c languageSwitch and looping statement for c language
Switch and looping statement for c language
BalaKrishnan466
 
C language Looping and conditional statement
C language Looping and conditional statementC language Looping and conditional statement
C language Looping and conditional statement
BalaKrishnan466
 
Control structure and Looping statements
Control structure and Looping statementsControl structure and Looping statements
Control structure and Looping statements
BalaKrishnan466
 
Control structure and Looping statements
Control structure and Looping statementsControl structure and Looping statements
Control structure and Looping statements
BalaKrishnan466
 
C array and Initialisation and declaration
C array and Initialisation and declarationC array and Initialisation and declaration
C array and Initialisation and declaration
BalaKrishnan466
 
Basic operators and it's types in c languages
Basic operators and it's types in c languagesBasic operators and it's types in c languages
Basic operators and it's types in c languages
BalaKrishnan466
 
Queues operation using data structure in c
Queues operation using data structure in cQueues operation using data structure in c
Queues operation using data structure in c
BalaKrishnan466
 
Stack operation in data structure in c language
Stack operation in data structure in c languageStack operation in data structure in c language
Stack operation in data structure in c language
BalaKrishnan466
 
Flow chart and algorithm working progress
Flow chart and algorithm working progressFlow chart and algorithm working progress
Flow chart and algorithm working progress
BalaKrishnan466
 
Algorithms and Flowchart usages in C laguage
Algorithms and Flowchart usages in C laguageAlgorithms and Flowchart usages in C laguage
Algorithms and Flowchart usages in C laguage
BalaKrishnan466
 
Data types and it's usage in c languages
Data types and it's usage in c languagesData types and it's usage in c languages
Data types and it's usage in c languages
BalaKrishnan466
 
Ad

Recently uploaded (20)

All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Ad

Control structure and Looping statements

  • 1. Lectures on Numerical Methods 1 Statements  Expressions, when terminated by a semicolon, become statements.  Examples X = 5; I++; IsPrime(c); c = 5 * ( f – 32 ) / 9  One can construct compound statements by putting statements and declarations in Braces { and }. Also called blocks.  Example if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”);
  • 2. Lectures on Numerical Methods 2 Labeled statements  One can give a label to any statement. The form is  Identifier : statement  There are other labeled statements like case and default, to be used with switch statement.  Example
  • 3. Lectures on Numerical Methods 3 If – else  The if-else statement expresses simplest decision making. The syntax is if (expression) statement1 elseopt Statement2  The expression is evaluated and if it is nonzero (true) then the statement1 is executed. Otherwise, if else is present statement2 is executed.  Expression can just be numeric and need not be conditional expression only.  Statement1 and statement2 may be compound statements or blocks.
  • 4. Lectures on Numerical Methods 4 If – else if ( n > 0 ) if ( isPrime(n) ) { printf(“%d is primen”,n); return; } else printf(“Input must be positiven”,n); printf(“%d is non-primen”,n);  Each else is associated with closest previous else-less if. Using this principle multi-way decisions can be constructed.
  • 5. Lectures on Numerical Methods 5 Switch  The syntax of switch statement is switch (expression) { case const-expression1 : statement1 case const-expression2 : statement2 : default : statementn }  All case expressions must be different. Expression must evaluate to an integer.  First the expression is evaluated. Then the value of expression is compared with the case expressions. The execution begins at the case statement, whose case expression matches. All the statements below are executed.  If default is present and if no other case matches then default statement is executed.
  • 6. Lectures on Numerical Methods 6 Switch switch ( marks / 10 ) { case 3 : grade = “DD”; break; case 4 : grade = “CD”; break; case 5 : grade = “CC”; break; case 6 : grade = “BC”; break; case 7 : grade = “BB”; break; case 8 : grade = “AB”; break; case 9 : case 10 : grade = “AA”; break; default : grade = “FF”; break; }
  • 7. Lectures on Numerical Methods 7 Iterations  The three types of loops are given below. while (expression) statement for (expression1opt; expression2opt; expression3opt) statement do statement while(expression);  In while loop the expression (which must be arithmetic) is evaluated and if the value is nonzero, then the statement is executed. The process is continued till the expression becomes zero. The expression is evaluated before the iteration.  For statement is equivalent to expression1; while ( expression2 ) { statement expression3; }
  • 8. Lectures on Numerical Methods 8 Iterations  In the do loop, the statement is executed and then the expression is evaluated. If the expression is nonzero the process is repeated. Thus the condition is evaluated at the end of each iteration.  Example x1 = 1; do { x0 = x1; x1 = x0 – f(x0) / derf(x0); } while ( fabs(x1 – x0) > FLT_EPSILON ) ;  We needed to evaluate x1 before we could apply the convergence criterion.
  • 9. Lectures on Numerical Methods 9 Break and Continue  The loops have one expression that decides whether the iterative process should be terminated. It is sometimes convenient to be able to exit from the loop.  break statement provides an early exit from the for, while and do loops.  It also provides an exit from switch statement.  continue statement causes the jump to the end of the loop body, skipping the statements only once. The loop may continue. while (...) { ... continue; ... cont: ; } do { ... continue; ... cont: ; } for (...) { ... continue; ... cont: ; }
  • 10. Lectures on Numerical Methods 10 Break and continue  Example for ( i = 0; i < n; i++ ) { if ( a[i] < 0 ) continue; /* Process only non-negative elements of the array */ ... }  Example for ( i = 2; i <= (int)sqrt(n); i++ ) { if ( n % i == 0 ) break; if ( n % i ) printf(“Primen”); else printf(“Not primen”);
  翻译: