The University of Texas – Pan American

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Fundamentals of Python: From First Programs Through Data Structures
The University of Texas – Pan American
Fundamentals of Python: First Programs
The University of Texas – Pan American
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Introduction to Computer Programming
Chapter 4 – C Program Control
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Chapter 3: Decisions and Loops
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
JavaScript: Control Statements I
Topics Introduction to Repetition Structures
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
The University of Texas – Pan American
The University of Texas – Pan American
MSIS 655 Advanced Business Applications Programming
The University of Texas Rio Grande Valley
Arithmetic operations, decisions and looping
The University of Texas – Pan American
The University of Texas – Pan American
Chapter 8 JavaScript: Control Statements, Part 2
Additional Control Structures
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
CSCI 3328 Object Oriented Programming in C# Review: Exam I
Chapter 4 - Program Control
The University of Texas – Pan American
Iteration: Beyond the Basic PERFORM
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
The University of Texas – Pan American
Chapter 6 Control Statements: Part 2
The University of Texas – Pan American
CSCI 3327 Visual Basic Review: Exam I
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
The University of Texas – Pan American
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

The University of Texas – Pan American CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu This set of slides is revised from lecture slides of Prof. John Abraham. -- Xiang Lian

Objectives In this chapter, you will do some exercises about: basic control structures in Python

Multiple Choices Typically, ______ statements are used for counter-controlled repetition. A. for B. while C. repetition D. until Typically, ______ statements are used for sentinel-controlled repetition. The _________statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. A. break B. exit C. continue D. return The _____operator can be used to ensure that two conditions are both true before choosing a certain path of execution A. and B. && C. or D. || An infinite loop occurs when the loop-continuation condition in a while statement______. A. never becomes true B. never becomes false C. is false D. is true for finite times

Multiple Choices (cont'd) What is the range of function call range(1, 10)? A. [0, 10] B. [0, 10) C. [1, 10] D. [1, 10) What is the value of the expression of 4/5*2+2**(1+2)? A. 8 B. 10 C. 9.6 D. 8.4 What is the value of 5/4, 5//4, and 5%4? A. 1, 1.25, 1 B. 1.25, 1, 4 C. 1, 1, 1 D. 1.25, 1, 1 Which of the following is the appropriate for statement for varying the control variable over the following sequence of values: 5, 10, 15, 20, 25? A. for i in range(25): B. for i in range (5, 25): C. for i in range(5, 25, 5): D. for i in range(25, 5, -5):

True/False Statements The exponentiation operator ** associates left to right. Function call range (4, 7) returns the sequence 4 to 7, inclusive. Sentinel-controlled repetition uses a counter variable to control the number of times a set of instructions executes. String format specifier "%5d" outputs integer with right alignment by default. The expression ((x>y) and (a<b)) is True if either (x>y) is True or (a<b) is True. An expression containing the or operator is True if either or both of its operands are True. The and operator has a higher precedence than the or operator.

What Does the Code Do? for i in range(1, 11): for j in range (1, 11): print ('@', end = ""); print ("");

Debug Errors counter=1 For i in range (1, 10) counter++

Write a Program Use nested for statement to write a Python program to display the triangle of asterisks. Use the following statements: print ("*", end="") # displays asterisks one at a time print ("") # the cursor goes to a new line print (" ", end="") # inserts a space * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Write a Program (cont'd) Use nested for statement to write a program to display a rectangle. Use the following statements: print ("+", end="") print ("-", end="") print ("|", end="") print ("") print (" ", end="") + - - - - - - - - - - + | |