Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 7. Control Flow Statements > Using if-else Conditionals

Using if-else Conditionals

You can add an else clause to an if statement to execute a block of statements if the condition is false.

To create an if-else conditional:

  • Type:

    if condition:
    								block1
    else:
      block2
    							

    condition is evaluated. If it's true, block1 is executed; otherwise, block2 is executed (Figure 7.13).

    Figure 7.13. Only one of the blocks in an if-else conditional is executed.


    block1 and block2 are indented blocks of statements, and condition is a Boolean expression (see “Using Boolean Operators” in Chapter 2).


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint