SlideShare a Scribd company logo
1 of 30
Conditionals and
Recursion
Conditional Execution
 A control statement is a statement that determines the
control fl ow of a set of instructions.
A control structure is a set of instructions and the control
statements controlling their
execution. Three fundamental forms of control in programming
are sequential, selection,and iterative control.
Contd.
Boolean Expressions
A boolean expression is an expression that is either true or false.
Boolean expressions are used to denote the conditions for selection
and iterative control statements.
Ex:
True and False are special values that belong to the type bool; they
are not Strings
Contd…
Contd..
+This program asks a user for a name and a password it then
check them to make sure that the user is allowed in.
Contd..
+The other relational operators are
+A common error is to use a single equal sign (=) instead of
a double equal sign (==).
Logical operators
+There are three logical operators: and, or, and not. The
semantics (meaning) of these operators is similar to their
meaning in English.
+For example x>0 and x<10, is true only if x is greater than 0 and less
than 10.
+ n%2==0 or n%3==0, is true if either of the conditions is true, that is,
if the number is divisible by 2 or 3.
+Finally, the not operator negates a Boolean expression, so not (x
> y) is true if x > y is false, that is, if x is less than or equal to y.
Contd..
+Python is not very strict. Any nonzero number is interpreted as
“true.”
+ Ex:>>> 17 and True
True
Operator precedence and Boolean
Expressions
Conditional Execution
In order to write useful programs, we almost always need the
ability to check conditions and change the behavior of the
program accordingly.
 Conditional statements give us this ability.
The simplest form is the if statement
An if statement consists of a boolean expression followed by
one or more statements.
Contd..
Ex:
if x > 0:
print 'x is positive’
The Boolean expression after the if statement is called the
condition.
 We end the if statement with a colon character (:) and the
line(s) after the if statement are indented.
Statements like this are called compound statements
Contd..
There is no limit on the number of statements that can appear
in the body, but there has to be at least one.
We use pass statement when body is with no statements.
Ex: if x < 0:
pass
Alternative execution
A second form of the if statement is alternative execution, in which there
are two possibilities and the condition determines which one gets
executed. The syntax looks like this:
Ex:
The alternatives are called branches, because they are branches in the
flow of execution.
Chained conditionals
+Sometimes there are more than two possibilities and we need
more than two branches.
+One way to express a computation like that is a chained
conditional:
+Ex:
Contd…
elif is an abbreviation of “else if.”
Again, exactly one branch will be executed.
There is no limit on the number of elif statements.
If there is an else clause, it has to be at the end, but there doesn’t have to
be one.
Each condition is checked in order. If the first is false, the next is checked,
and so on.
If one of them is true, the corresponding branch executes, and the
statement ends.
Even if more than one condition is true, only the first true branch executes.
Nested conditionals
We can have a if...elif...else statement inside another
if...elif...else statement.
This is called nesting in computer programming.
nested conditionals become difficult to read very quickly.
 In general, it is a good idea to avoid them when you can.
Logical operators often provide a way to simplify nested
conditional statements.
Contd..
+Ex:
if 0 < x:
if x < 10:
print 'x is a positive single-digit number.’
+The above can be simplified as
if 0 < x and x < 10:
print 'x is a positive single-digit number.'
Recursion
+It is legal for one function to call another; it is also legal for a
function to call itself.
+Ex:
Stack diagrams for recursive functions
+The following is the stack diagram for the above example with
n=3.
Contd..
+Every time a function gets called, Python creates a new function
frame, which contains the function’s local variables and
parameters.
+For a recursive function, there might be more than one frame on
the stack at the same time.
+The top of the stack is the frame for __main__.
+ It is empty because we did not create any variables in __main__
or pass any arguments to it.
Contd..
+The four countdown frames have different values for the
parameter n.
+The bottom of the stack, where n=0, is called the base case.
+It does not make a recursive call, so there are no more frames.
Infinite recursion
+If a recursion never reaches a base case, it goes on making
recursive calls forever, and the program never terminates.
+This is known as infinite recursion.
+Ex:
def recurse():
recurse()
+Python reports an error message when the maximum recursion
depth is reached.
Keyboard input
+Python 2 provides a built-in function called raw_input that gets
input from the keyboard. In Python 3, it is called input.
+Ex:
>>> name = input('What...is your name?n')
What...is your name?
Arthur, King of the Britons!
>>> print (name)
Arthur, King of the Britons!

More Related Content

Similar to Conditional_and_Recursion.pptx

Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parametersKnoldus Inc.
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsDhivyaSubramaniyam
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : PythonRaghu Kumar
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
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 languagechintupro9
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
 
Notes2
Notes2Notes2
Notes2hccit
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxabhi353063
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfIda Lumintu
 

Similar to Conditional_and_Recursion.pptx (20)

Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
What is c
What is cWhat is c
What is c
 
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
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Unit 1
Unit 1Unit 1
Unit 1
 
Chapter - 3.pptx
Chapter - 3.pptxChapter - 3.pptx
Chapter - 3.pptx
 
Notes2
Notes2Notes2
Notes2
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Python basics
Python basicsPython basics
Python basics
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Vb script final pari
Vb script final pariVb script final pari
Vb script final pari
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdf
 

Recently uploaded

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 

Conditional_and_Recursion.pptx

  • 2. Conditional Execution  A control statement is a statement that determines the control fl ow of a set of instructions. A control structure is a set of instructions and the control statements controlling their execution. Three fundamental forms of control in programming are sequential, selection,and iterative control.
  • 4. Boolean Expressions A boolean expression is an expression that is either true or false. Boolean expressions are used to denote the conditions for selection and iterative control statements. Ex: True and False are special values that belong to the type bool; they are not Strings
  • 6. Contd.. +This program asks a user for a name and a password it then check them to make sure that the user is allowed in.
  • 7. Contd.. +The other relational operators are +A common error is to use a single equal sign (=) instead of a double equal sign (==).
  • 8. Logical operators +There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. +For example x>0 and x<10, is true only if x is greater than 0 and less than 10. + n%2==0 or n%3==0, is true if either of the conditions is true, that is, if the number is divisible by 2 or 3. +Finally, the not operator negates a Boolean expression, so not (x > y) is true if x > y is false, that is, if x is less than or equal to y.
  • 9. Contd.. +Python is not very strict. Any nonzero number is interpreted as “true.” + Ex:>>> 17 and True True
  • 10. Operator precedence and Boolean Expressions
  • 11. Conditional Execution In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly.  Conditional statements give us this ability. The simplest form is the if statement An if statement consists of a boolean expression followed by one or more statements.
  • 12. Contd.. Ex: if x > 0: print 'x is positive’ The Boolean expression after the if statement is called the condition.  We end the if statement with a colon character (:) and the line(s) after the if statement are indented. Statements like this are called compound statements
  • 13. Contd.. There is no limit on the number of statements that can appear in the body, but there has to be at least one. We use pass statement when body is with no statements. Ex: if x < 0: pass
  • 14. Alternative execution A second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this: Ex: The alternatives are called branches, because they are branches in the flow of execution.
  • 15. Chained conditionals +Sometimes there are more than two possibilities and we need more than two branches. +One way to express a computation like that is a chained conditional: +Ex:
  • 16.
  • 17. Contd… elif is an abbreviation of “else if.” Again, exactly one branch will be executed. There is no limit on the number of elif statements. If there is an else clause, it has to be at the end, but there doesn’t have to be one. Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
  • 18.
  • 19. Nested conditionals We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in computer programming. nested conditionals become difficult to read very quickly.  In general, it is a good idea to avoid them when you can. Logical operators often provide a way to simplify nested conditional statements.
  • 20.
  • 21. Contd.. +Ex: if 0 < x: if x < 10: print 'x is a positive single-digit number.’ +The above can be simplified as if 0 < x and x < 10: print 'x is a positive single-digit number.'
  • 22.
  • 23.
  • 24. Recursion +It is legal for one function to call another; it is also legal for a function to call itself. +Ex:
  • 25. Stack diagrams for recursive functions +The following is the stack diagram for the above example with n=3.
  • 26. Contd.. +Every time a function gets called, Python creates a new function frame, which contains the function’s local variables and parameters. +For a recursive function, there might be more than one frame on the stack at the same time. +The top of the stack is the frame for __main__. + It is empty because we did not create any variables in __main__ or pass any arguments to it.
  • 27. Contd.. +The four countdown frames have different values for the parameter n. +The bottom of the stack, where n=0, is called the base case. +It does not make a recursive call, so there are no more frames.
  • 28. Infinite recursion +If a recursion never reaches a base case, it goes on making recursive calls forever, and the program never terminates. +This is known as infinite recursion. +Ex: def recurse(): recurse() +Python reports an error message when the maximum recursion depth is reached.
  • 29.
  • 30. Keyboard input +Python 2 provides a built-in function called raw_input that gets input from the keyboard. In Python 3, it is called input. +Ex: >>> name = input('What...is your name?n') What...is your name? Arthur, King of the Britons! >>> print (name) Arthur, King of the Britons!