/
If, else, If, else,

If, else, - PowerPoint Presentation

lindy-dunigan
lindy-dunigan . @lindy-dunigan
Follow
406 views
Uploaded On 2017-07-08

If, else, - PPT Presentation

elif Using the if statement Branching is a fundamental part of computer programming It basically means making a decision to take one path or another Through the if statement your programs can branch to a section of code or just skip it all based on how youve set things up ID: 567713

code statement password program statement code program password print expression block indented true returns elif conditional syntax run access

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "If, else," is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

If, else, elifSlide2

Using the if statement

Branching

is a fundamental part of computer programming. It basically means making a decision to take one path or another. Through the if statement, your programs can branch to a section of code or just skip it, all based on how you’ve set things upSlide3

Introducing the Password program

Open the file password.py in the Chapter 3 folder.

Right click, and select Edit with IDLE 3.5

The password program uses the if statement to simulate the login procedure of a highly secure computer system. The program grants the user access if he or she enters the correct password. Slide4

Examining the if statement

The key to the program is the if statement

The if statement is pretty straightforward. You can probably figure out what’s happening just by reading the code. If the password is equal to “secret”, then “Access Granted” is printed and the program continues to the next statement. But, if it is not equal to secret, the program does not print the message and continues directly to the next statement following the if statement.Slide5

Conditional statement syntax

If is

a conditional statement that executes some specified code after checking if its expression is 

True

Here is an example of if syntax statement:

In this example, 8 < 9 is the checked expression and print (“Eight is less than nine!” is the specified code.Slide6

Using indentation to create blocks

Notice in the second line of the in statement, print (“Eight is less than 9”),

is indented. By indenting this line, it becomes a

block.

A block is one or more consecutive lines indented by the same amount. Indenting sets lines off not only visually, but logically too. Together they form a since unit.

Blocks

can be used as part of an if statement. They’re the statement or group of statements that gets executed if the condition is True. In the password program, the block is the single statement print(“Access Granted.”)Slide7

Conditional Statement Syntax

Do you think the print statement will print to the console in the following code?

What will happen when you change today to “November 8”Slide8

If statements

Looking at the example

below, in

the event that 

some_function

() returns True, then the indented block of code after it will be executed. In the event that it returns False, then the indented blocked will be skipped

*Also, make sure you notice the colons at the end of the if statement. They are importantSlide9

Practice:

In the figure below, there are two functions. Don’t worry about anything unfamiliar. We will be going over that soon.

Replace the first underline with an expression that returns True

Replace the second underline

with an expression that returns

True

Run the code

What does the code print?Slide10

The else statement

The else statement complements the if statement. An if/else pair says: “If this expression is true, run this indented code block; otherwise run this code after the else statement.”

Unlike if, else doesn’t depend on an expression.

For example:Slide11

Introducing the granted or denied program

The Password program did not do anything if the wrong password was entered.

Open Program Granted or Denied in the Chapter 3 Folder

This program solves this problem by using the else clauseSlide12

Practice

Complete the else statement below and run the code. Note the indentation for each line. What did the code do?Slide13

The Elif

statement

Elif

” is short for “else/if.” It means exactly what it sounds like: “otherwise, if the following expression is true, do this!”

In the example below, the

elif

statement is only checked if the original if statement is FalseSlide14

Practice

In the first blank, fill in the if statement to check

if

answer is greater than 5.

In the second blank, fill in the

elif

so that the function outputs -1 if answer is less than 5

Related Contents


Next Show more