SlideShare a Scribd company logo
CONTROL STATEMENTS
Anil Dutt
Flow of Control
• Unless specified otherwise, the order of statement
execution through a function is linear: one statement after
another in sequence
• Some programming statements allow us to:
– decide whether or not to execute a particular statement
– execute a statement over and over, repetitively
• These decisions are based on boolean expressions (or
conditions) that evaluate to true or false
• The order of statement execution is called the flow of
control
Conditional Statements
• A conditional statement lets us choose which statement
will be executed next
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic
decisions
• The C conditional statements are the:
– if statement
– if-else statement
– switch statement
Logic of an if statement
condition
evaluated
statement
true
false
The if Statement
• The if statement has the following syntax:
if ( condition )
statement;
if is a C
reserved word
The condition must be a
boolean expression. It must
evaluate to either true or false.
If the condition is true, the statement is executed.
If it is false, the statement is skipped.
For example:
if (hours > 70)
hours = hours + 100;
printf("Less hours, no bonus!n");
 If hours is less than or equal to 70, its value will remain unchanged and the printf() will
be executed.
 If it exceeds 70, its value will be increased by 100.
if(jobCode == '1')
{
carAllowance = 100.00;
housingAllowance = 500.00;
entertainmentAllowance = 300.00;
}
printf("Not qualified for car, housing and entertainment allowances!");
The three statements enclosed in the curly braces { } will only be executed if jobCode is equal
to '1', else the printf() will be executed.
Logic of an if-else statement
condition
evaluated
statement1
true false
statement2
The if-else Statement
• An else clause can be added to an if
statement to make an if-else statement
if ( condition )
statement1;
else
statement2;
• If the condition is true, statement1 is executed;
if the condition is false, statement2 is executed
• One or the other will be executed, but not both
For example:
if(myCode == '1')
rate = 7.20;
else
rate = 12.50;
If myCode is equal to '1', the rate is 7.20 else, if myCode
is not equal to '1' the rate is 12.50.
Anil Dutt
Equal/not equal (=) is not a value comparison, but a character comparison!!!
Boolean Expressions
• A condition often uses one of C's equality operators or relational
operators, which all return Boolean results:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
• Note the difference between the equality operator (==) and the
assignment operator (=)
Boolean Expressions in C
• C does not have a boolean data type.
• Therefore, C compares the values of variables and expressions
against 0 (zero) to determine if they are true or false.
• If the value is 0 then the result is implicitly assumed to be
false.
• If the value is different from 0 then the result is implicitly
assumed to be true.
• C++ and Java have boolean data types.
Block Statements
• Several statements can be grouped together
into a block statement delimited by braces
• A block statement can be used wherever a
statement is called for in the C syntax rules
if (total > MAX)
{
printf ("Error!!n");
errorCount++;
}
Block Statements
• In an if-else statement, the if portion, or the
else portion, or both, could be block statements
if (total > MAX)
{
printf("Error!!");
errorCount++;
}
else
{
printf ("Total: %d“, total);
current = total*2;
}
Nested if Statements
• The statement executed as a result of an if statement or
else clause could be another if statement
• These are called nested if statements
• An else clause is matched to the last unmatched if (no
matter what the indentation implies)
• Braces can be used to specify the if statement to which an
else clause belongs
 The if-else constructs can be nested (placed one within another) to any depth.
 General forms: if-if-else and if-else-if.
 The if-if-else constructs has the following form (3 level of depth example),
if(condition_1)
if(condition_2)
if(condition_3)
statement_4;
else
statement_3;
else
statement_2;
else
statement_1;
next_statement;
Anil Dutt
The switch Statement
• The switch statement provides another way to decide which
statement to execute next
• The switch statement evaluates an expression, then attempts
to match the result to one of several possible cases
• Each case contains a value and a list of statements
• The flow of control transfers to statement associated with the
first case value that matches
The switch Statement
• Often a break statement is used as the last statement in each
case's statement list
• A break statement causes control to transfer to the end of the
switch statement
• If a break statement is not used, the flow of control will
continue into the next case
• Sometimes this may be appropriate, but often we want to
execute only the statements associated with one case
The switch Statement
switch (option)
{
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
otherCount++;
break;
}
• An example of a switch statement:
The switch Statement
• A switch statement can have an optional default case
• The default case has no associated value and simply uses the
reserved word default
• If the default case is present, control will transfer to it if no
other case value matches
• If there is no default case, and no other value matches,
control falls through to the statement after the switch
The switch Statement
• The expression of a switch statement must result in an
integral type, meaning an integer (byte, short, int,) or a
char
• It cannot be a floating point value (float or double)
• The implicit test condition in a switch statement is equality
• You cannot perform relational checks with a switch
statement
The switch Statement
• The general syntax of a switch statement
is: switch ( expression )
{
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
switch
and
case
are
reserved
words
If expression
matches value2,
control jumps
to here
Repetition in Programs
• In most software, the statements in the program may
need to repeat for many times.
– e.g., calculate the value of n!.
– If n = 10000, it’s not elegant to write the code as
1*2*3*…*10000.
• Loop is a control structure that repeats a group of steps
in a program.
– Loop body stands for the repeated statements.
• There are three C loop control statements:
– while, for, and do-while.
5-23
Flow Diagram of Loop Choice Process
5-24
e.g., calculate the value of n!
e.g., read the content in a file
Comparison of Loop Choices (1/2)
Kind When to Use C Structure
Counting loop We know how many loop repetitions
will be needed in advance.
while, for
Sentinel-controlled
loop
Input of a list of data ended by a special
value
while, for
Endfile-controlled
loop
Input of a list of data from a data file while, for
5-25
Comparison of Loop Choices (2/2)
Kind When to Use C Structure
Input validation
loop
Repeated interactive input of a value
until a desired value is entered.
do-while
General
conditional
loop
Repeated processing of data until a
desired condition is met.
while, for
5-26
The while Statement in C
• The syntax of while statement in C:
while (loop repetition condition)
statement
• Loop repetition condition is the condition which controls the
loop.
• The statement is repeated as long as the loop repetition
condition is true.
• A loop is called an infinite loop if the loop repetition condition is
always true.
5-27
An Example of a while Loop
5-28
Statement
Loop repetition condition
Loop control variable is the variable whose value controls loop
repetition.
In this example, count_emp is the loop control variable.
Flowchart for a while Loop
5-29
Loop repetition condition
Statement
Compound Assignment Operators (1/2)
• The loop body usually consists of statements of the
form: variable = variable op expression.
– e.g., count_emp = count_emp + 1;
• C provides compound assignment operators which
enable a more concise notation for this kind of
statements.
– “variable op = expression” is the same to
“variable = variable op expression.”
Compound Assignment Operators (2/2)
Simple Assignment Operators Compound Assignment
Operators
count_emp = count_emp +
1;
count_emp += 1;
time = time -1; time -= 1;
product = product *
item;
product *= item;
total = total / number; total /= number;
n = n % (x+1); n %= x+1;
5-31
The for Statement in C
• The syntax of for statement in C:
for (initialization expression;
loop repetition condition;
update expression)
statement
• The initialization expression set the initial value of the loop
control variable.
• The loop repetition condition test the value of the loop control
variable.
• The update expression update the loop control variable.
5-32
An Example of the for Loop
5-33
Loop repetition condition
Initialization Expression
Update Expression
count_emp is set to 0 initially.
count_emp should not exceed the value of number_emp.
count_emp is increased by one after each iteration.
Increment and Decrement Operators
• The statements of increment and decrement are
commonly used in the for loop.
• The increment (i.e., ++) or decrement (i.e., --) operators
are the frequently used operators which take only one
operand.
• The increment/decrement operators increase or
decrease the value of the single operand.
– e.g., for (int i = 0; i < 100; i++){ … }
– The variable i increase one after each iteration.
5-34
Comparison of Prefix and Postfix Increments
Copyright ©2004 Pearson Addison-Wesley. All
rights reserved.
5-35
The value of the expression (that uses the ++/-- operators) depends on
the position of the operator.
The value of
j is increased
The value of
j is not
increased
Nested Loops
• Nested loops consist of an outer loop with one or more
inner loops.
• e.g.,
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){
…
}
}
• The above loop will run for 100*50 iterations.
5-36
Inner loop
Outer loop
The do-while Statement in C
• The syntax of do-while statement in C:
do
statement
while (loop repetition condition);
• The statement is first executed.
• If the loop repetition condition is true, the statement is
repeated.
• Otherwise, the loop is exited.
5-37
An Example of the do-while Loop
/* Find even number input */
do{
printf(“Enter a value: ”);
scanf(“%d”, &num);
}while (num % 2 !=0)
5-38
This loop will repeat if the user
inputs odd number.
Control statements anil
Control statements anil

More Related Content

What's hot

Flow of Control
Flow of ControlFlow of Control
Flow of Control
Praveen M Jigajinni
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
Hossain Md Shakhawat
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
Nitin Jawla
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Hossain Md Shakhawat
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 
Iteration
IterationIteration
Iteration
Liam Dunphy
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
Tech
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
University of Potsdam
 
Looping
LoopingLooping
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
Sriman Sawarthia
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
Future Programming
 
C++ loop
C++ loop C++ loop
C++ loop
Khelan Ameen
 

What's hot (20)

Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
Iteration
IterationIteration
Iteration
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Loops c++
Loops c++Loops c++
Loops c++
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Looping
LoopingLooping
Looping
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
 
C++ loop
C++ loop C++ loop
C++ loop
 

Viewers also liked

Annabell on the Maarefah Platform
Annabell on the Maarefah PlatformAnnabell on the Maarefah Platform
Annabell on the Maarefah Platform
Annabell Van den Berghe
 
Al zawm on Promoting Accountability
Al zawm on Promoting AccountabilityAl zawm on Promoting Accountability
Al zawm on Promoting Accountability
Annabell Van den Berghe
 
Proyecto honda barcelona 2013 by think tank
Proyecto honda barcelona 2013 by think tankProyecto honda barcelona 2013 by think tank
Proyecto honda barcelona 2013 by think tank2013 Think Tank
 
Ben soltane on Access to Information
Ben soltane on Access to InformationBen soltane on Access to Information
Ben soltane on Access to Information
Annabell Van den Berghe
 
Mendel on Access to Information
Mendel on Access to InformationMendel on Access to Information
Mendel on Access to Information
Annabell Van den Berghe
 
Camargo baez on Participatory Approaches
Camargo baez on Participatory ApproachesCamargo baez on Participatory Approaches
Camargo baez on Participatory Approaches
Annabell Van den Berghe
 
AbdelLatif on Promoting Accountability
AbdelLatif on Promoting AccountabilityAbdelLatif on Promoting Accountability
AbdelLatif on Promoting Accountability
Annabell Van den Berghe
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
pratikborsadiya
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
C language control statements
C language  control statementsC language  control statements
C language control statements
suman Aggarwal
 

Viewers also liked (13)

Annabell on the Maarefah Platform
Annabell on the Maarefah PlatformAnnabell on the Maarefah Platform
Annabell on the Maarefah Platform
 
Al zawm on Promoting Accountability
Al zawm on Promoting AccountabilityAl zawm on Promoting Accountability
Al zawm on Promoting Accountability
 
Proyecto honda barcelona 2013 by think tank
Proyecto honda barcelona 2013 by think tankProyecto honda barcelona 2013 by think tank
Proyecto honda barcelona 2013 by think tank
 
Ben soltane on Access to Information
Ben soltane on Access to InformationBen soltane on Access to Information
Ben soltane on Access to Information
 
Mendel on Access to Information
Mendel on Access to InformationMendel on Access to Information
Mendel on Access to Information
 
Camargo baez on Participatory Approaches
Camargo baez on Participatory ApproachesCamargo baez on Participatory Approaches
Camargo baez on Participatory Approaches
 
AbdelLatif on Promoting Accountability
AbdelLatif on Promoting AccountabilityAbdelLatif on Promoting Accountability
AbdelLatif on Promoting Accountability
 
Ma arefah ar
Ma arefah arMa arefah ar
Ma arefah ar
 
AbdelHamid, المشاركة
AbdelHamid, المشاركة  AbdelHamid, المشاركة
AbdelHamid, المشاركة
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 

Similar to Control statements anil

Control structure
Control structureControl structure
Control structure
Samsil Arefin
 
Control structures in C
Control structures in CControl structures 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
TANUJ ⠀
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
ssuserfb3c3e
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
JavvajiVenkat
 
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
Sowmya Jyothi
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer science
RaianaTabitha
 
While loop
While loopWhile loop
While loop
Feras_83
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
BasirKhan21
 
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
 
Programming loop
Programming loopProgramming loop
Programming loop
University of Potsdam
 
cpu.pdf
cpu.pdfcpu.pdf

Similar to Control statements anil (20)

Control structure
Control structureControl structure
Control structure
 
Control structures in C
Control structures in CControl structures in C
Control structures 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
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
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
 
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
 
IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer science
 
While loop
While loopWhile loop
While loop
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
 
Programming loop
Programming loopProgramming loop
Programming loop
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 

More from Anil Dutt

Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
Anil Dutt
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
Anil Dutt
 
Ponters
PontersPonters
Ponters
Anil Dutt
 
Array
ArrayArray
Array
Anil Dutt
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 

More from Anil Dutt (6)

Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
Ponters
PontersPonters
Ponters
 
Array
ArrayArray
Array
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 

Recently uploaded

Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 

Recently uploaded (20)

Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 

Control statements anil

  • 2. Flow of Control • Unless specified otherwise, the order of statement execution through a function is linear: one statement after another in sequence • Some programming statements allow us to: – decide whether or not to execute a particular statement – execute a statement over and over, repetitively • These decisions are based on boolean expressions (or conditions) that evaluate to true or false • The order of statement execution is called the flow of control
  • 3. Conditional Statements • A conditional statement lets us choose which statement will be executed next • Therefore they are sometimes called selection statements • Conditional statements give us the power to make basic decisions • The C conditional statements are the: – if statement – if-else statement – switch statement
  • 4. Logic of an if statement condition evaluated statement true false
  • 5. The if Statement • The if statement has the following syntax: if ( condition ) statement; if is a C reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.
  • 6. For example: if (hours > 70) hours = hours + 100; printf("Less hours, no bonus!n");  If hours is less than or equal to 70, its value will remain unchanged and the printf() will be executed.  If it exceeds 70, its value will be increased by 100. if(jobCode == '1') { carAllowance = 100.00; housingAllowance = 500.00; entertainmentAllowance = 300.00; } printf("Not qualified for car, housing and entertainment allowances!"); The three statements enclosed in the curly braces { } will only be executed if jobCode is equal to '1', else the printf() will be executed.
  • 7. Logic of an if-else statement condition evaluated statement1 true false statement2
  • 8. The if-else Statement • An else clause can be added to an if statement to make an if-else statement if ( condition ) statement1; else statement2; • If the condition is true, statement1 is executed; if the condition is false, statement2 is executed • One or the other will be executed, but not both
  • 9. For example: if(myCode == '1') rate = 7.20; else rate = 12.50; If myCode is equal to '1', the rate is 7.20 else, if myCode is not equal to '1' the rate is 12.50. Anil Dutt Equal/not equal (=) is not a value comparison, but a character comparison!!!
  • 10. Boolean Expressions • A condition often uses one of C's equality operators or relational operators, which all return Boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to • Note the difference between the equality operator (==) and the assignment operator (=)
  • 11. Boolean Expressions in C • C does not have a boolean data type. • Therefore, C compares the values of variables and expressions against 0 (zero) to determine if they are true or false. • If the value is 0 then the result is implicitly assumed to be false. • If the value is different from 0 then the result is implicitly assumed to be true. • C++ and Java have boolean data types.
  • 12. Block Statements • Several statements can be grouped together into a block statement delimited by braces • A block statement can be used wherever a statement is called for in the C syntax rules if (total > MAX) { printf ("Error!!n"); errorCount++; }
  • 13. Block Statements • In an if-else statement, the if portion, or the else portion, or both, could be block statements if (total > MAX) { printf("Error!!"); errorCount++; } else { printf ("Total: %d“, total); current = total*2; }
  • 14. Nested if Statements • The statement executed as a result of an if statement or else clause could be another if statement • These are called nested if statements • An else clause is matched to the last unmatched if (no matter what the indentation implies) • Braces can be used to specify the if statement to which an else clause belongs
  • 15.  The if-else constructs can be nested (placed one within another) to any depth.  General forms: if-if-else and if-else-if.  The if-if-else constructs has the following form (3 level of depth example), if(condition_1) if(condition_2) if(condition_3) statement_4; else statement_3; else statement_2; else statement_1; next_statement; Anil Dutt
  • 16. The switch Statement • The switch statement provides another way to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first case value that matches
  • 17. The switch Statement • Often a break statement is used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case • Sometimes this may be appropriate, but often we want to execute only the statements associated with one case
  • 18. The switch Statement switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++; break; } • An example of a switch statement:
  • 19. The switch Statement • A switch statement can have an optional default case • The default case has no associated value and simply uses the reserved word default • If the default case is present, control will transfer to it if no other case value matches • If there is no default case, and no other value matches, control falls through to the statement after the switch
  • 20. The switch Statement • The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int,) or a char • It cannot be a floating point value (float or double) • The implicit test condition in a switch statement is equality • You cannot perform relational checks with a switch statement
  • 21. The switch Statement • The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } switch and case are reserved words If expression matches value2, control jumps to here
  • 22.
  • 23. Repetition in Programs • In most software, the statements in the program may need to repeat for many times. – e.g., calculate the value of n!. – If n = 10000, it’s not elegant to write the code as 1*2*3*…*10000. • Loop is a control structure that repeats a group of steps in a program. – Loop body stands for the repeated statements. • There are three C loop control statements: – while, for, and do-while. 5-23
  • 24. Flow Diagram of Loop Choice Process 5-24 e.g., calculate the value of n! e.g., read the content in a file
  • 25. Comparison of Loop Choices (1/2) Kind When to Use C Structure Counting loop We know how many loop repetitions will be needed in advance. while, for Sentinel-controlled loop Input of a list of data ended by a special value while, for Endfile-controlled loop Input of a list of data from a data file while, for 5-25
  • 26. Comparison of Loop Choices (2/2) Kind When to Use C Structure Input validation loop Repeated interactive input of a value until a desired value is entered. do-while General conditional loop Repeated processing of data until a desired condition is met. while, for 5-26
  • 27. The while Statement in C • The syntax of while statement in C: while (loop repetition condition) statement • Loop repetition condition is the condition which controls the loop. • The statement is repeated as long as the loop repetition condition is true. • A loop is called an infinite loop if the loop repetition condition is always true. 5-27
  • 28. An Example of a while Loop 5-28 Statement Loop repetition condition Loop control variable is the variable whose value controls loop repetition. In this example, count_emp is the loop control variable.
  • 29. Flowchart for a while Loop 5-29 Loop repetition condition Statement
  • 30. Compound Assignment Operators (1/2) • The loop body usually consists of statements of the form: variable = variable op expression. – e.g., count_emp = count_emp + 1; • C provides compound assignment operators which enable a more concise notation for this kind of statements. – “variable op = expression” is the same to “variable = variable op expression.”
  • 31. Compound Assignment Operators (2/2) Simple Assignment Operators Compound Assignment Operators count_emp = count_emp + 1; count_emp += 1; time = time -1; time -= 1; product = product * item; product *= item; total = total / number; total /= number; n = n % (x+1); n %= x+1; 5-31
  • 32. The for Statement in C • The syntax of for statement in C: for (initialization expression; loop repetition condition; update expression) statement • The initialization expression set the initial value of the loop control variable. • The loop repetition condition test the value of the loop control variable. • The update expression update the loop control variable. 5-32
  • 33. An Example of the for Loop 5-33 Loop repetition condition Initialization Expression Update Expression count_emp is set to 0 initially. count_emp should not exceed the value of number_emp. count_emp is increased by one after each iteration.
  • 34. Increment and Decrement Operators • The statements of increment and decrement are commonly used in the for loop. • The increment (i.e., ++) or decrement (i.e., --) operators are the frequently used operators which take only one operand. • The increment/decrement operators increase or decrease the value of the single operand. – e.g., for (int i = 0; i < 100; i++){ … } – The variable i increase one after each iteration. 5-34
  • 35. Comparison of Prefix and Postfix Increments Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-35 The value of the expression (that uses the ++/-- operators) depends on the position of the operator. The value of j is increased The value of j is not increased
  • 36. Nested Loops • Nested loops consist of an outer loop with one or more inner loops. • e.g., for (i=1;i<=100;i++){ for(j=1;j<=50;j++){ … } } • The above loop will run for 100*50 iterations. 5-36 Inner loop Outer loop
  • 37. The do-while Statement in C • The syntax of do-while statement in C: do statement while (loop repetition condition); • The statement is first executed. • If the loop repetition condition is true, the statement is repeated. • Otherwise, the loop is exited. 5-37
  • 38. An Example of the do-while Loop /* Find even number input */ do{ printf(“Enter a value: ”); scanf(“%d”, &num); }while (num % 2 !=0) 5-38 This loop will repeat if the user inputs odd number.