Hossain Shahriar shahriar@cs.queensu.ca CISC 101: Fall 2011 Hossain Shahriar shahriar@cs.queensu.ca.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
CS101 Computer Programming I Chapter 4 Extra Examples.
Hossain Shahriar Announcement and reminder! Final exam date December 19: 4pm-6pm Topics to be covered in this lecture Sort.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Hossain Shahriar Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
UCT Department of Computer Science Computer Science 1015F Iteration
ECE Application Programming
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
CS 115 Lecture Flags.
The switch Statement, and Introduction to Looping
While Loops in Python.
Chapter 8: Control Structures
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures - Repetition
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Repetition and Loop Statements
Arrays, For loop While loop Do while loop
Topics Introduction to File Input and Output
- Additional C Statements
Outline Altering flow of control Boolean expressions
CS1100 Computational Engineering
CS 115 Lecture Flags.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
COMP 110 Loops, loops, loops, loops, loops, loops…
Intro to Nested Looping
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Module 4 Loops.
Intro to Nested Looping
UMBC CMSC 104 – Section 01, Fall 2016
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
Python programming exercise
EECE.2160 ECE Application Programming
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders All assignments are now posted.
Functions continued.
Python Basics with Jupyter Notebook
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
EECE.2160 ECE Application Programming
Dale Roberts, Lecturer IUPUI
CISC101 Reminders Quiz 1 marking underway.
More Loops Topics Counter-Controlled (Definite) Repetition
Matlab Basics.
More Loops Topics Counter-Controlled (Definite) Repetition
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Topics Introduction to File Input and Output
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CprE 185: Intro to Problem Solving (using C)
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Programming Fundamental
Presentation transcript:

Hossain Shahriar shahriar@cs.queensu.ca CISC 101: Fall 2011 Hossain Shahriar shahriar@cs.queensu.ca

Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s) More on range function While loop Break

Range function >>> range(5) [0, 1, 2, 3, 4] # we are getting one less element that expected >>> range(5, 10) [5, 6, 7, 8, 9] # we are getting one less element that expected >>> range(4, 12, 3) [4, 7, 10] >>> range(-10, -100, -30) [-10, -40, -70] Write a program that sum the series: 2+ 4+ ..+ 10 sum =0; For i in range (2, 12, 2): sum+= i

While loop while expr1 : statement Two things to keep in mind for using while loop expr1 must include an iterating variable You are responsible for incrementing/decrementing the iterating variable to terminate the loop Let us sum the series: 2 + 4+ … 10 [watch the red lines!!] sum =0 i=2 while i < 10: sum+= i i+= 2

Loop (cont.) – Break statement Useful in certain situations and often used in an if-else structure within a loop Imagine, we want to iterate a list to find at least one negative number We do not need to continue the loop once we find a case for x in len (list) If x < 0: Print ‘found a negative number, goodbye!’ break Caution: if you have multiple loops, then break will only affect the immediate loop where it is located For …. : #loop1 For … : #loop2 If … break # break loop2 only and loop1 will continue as usual

Loop (cont.) – Break statement Take home assignment: (3 marks) Write a program that checks if a given number N is prime or not. Tips: N is a prime number when it not divisible by any number other than 1 and N You need to use a for/while loop, if-else, and a break statement Iterate from 1 to N/2 and check the remainder is always non zero to conclude that N is a prime number

Exception A generic way to handle incompatible input E.g., a user provides string instead of a number def main(): try: x = int (input ("enter a number : ")) … except Exception: print "The supplied input is not a number" main()

Tomorrow’s class File handling