Welcome! COMP s1. Programming Fundamentals

Similar documents
Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Welcome! COMP s1. Programming Fundamentals

COMP s1 Lecture 1

Welcome! COMP s1. Programming Fundamentals

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Repetition and Loop Statements Chapter 5

Repetition Structures II

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Loops. CSCI 112: Programming in C

Loops / Repetition Statements

REPETITION CONTROL STRUCTURE LOGO

CpSc 1111 Lab 4 Formatting and Flow Control

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso

Introduction. C provides two styles of flow control:

(Refer Slide Time: 00:26)

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Programming. Lecture 2: Introduction to C

A Look Back at Arithmetic Operators: the Increment and Decrement

Dept. of CSE, IIT KGP

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

(6-1) Iteration in C H&K Chapter 5. Instructor - Andrew S. O Fallon CptS 121 (February 11, 2019) Washington State University

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Welcome to... CS113: Introduction to C

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

C 101. Davide Vernizzi. April 29, 2011

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Computer Programming: Skills & Concepts (CP) Variables and ints

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Loops / Repetition Statements

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Control Structure: Selection

Control Structure: Loop

CMPT 102 Introduction to Scientific Computer Programming

Module 4: Decision-making and forming loops

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Fundamentals of Programming Session 7

LECTURE 5 Control Structures Part 2

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Pointers and scanf() Steven R. Bagley

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Laboratory 5: Implementing Loops and Loop Control Strategies

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

CS110D: PROGRAMMING LANGUAGE I

Computer Systems Lecture 9

Structured Program Development in C

Chapter 3. Iteration

CpSc 1111 Lab 5 Formatting and Flow Control

Variables and literals

C++ Programming: From Problem Analysis to Program Design, Third Edition

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Lecture 10. Daily Puzzle

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

(4-2) Selection Structures in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 12, 2018) Washington State University

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

Fundamentals of Programming

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

n Group of statements that are executed repeatedly while some condition remains true

Chapter 13 Control Structures

EECE.2160: ECE Application Programming Fall 2017

Semester 2, 2018: Lab 1

204111: Computer and Programming

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Lab 2: Structured Program Development in C

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

More About WHILE Loops

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Chapter 13. Control Structures

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

DECISION CONTROL AND LOOPING STATEMENTS

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

EECE.2160: ECE Application Programming Spring 2018

Chapter 5: Control Structures

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

Topic 6: A Quick Intro To C

Lecture 7 Tao Wang 1

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

1007 Imperative Programming Part II

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 13 Control Structures

Quiz Determine the output of the following program:

Transcription:

Welcome! 0 COMP1511 18s1 Programming Fundamentals

COMP1511 18s1 Lecture 5 1 More Loops Andrew Bennett <andrew.bennett@unsw.edu.au> while loops loops inside loops stopping loops

2 Before we begin introduce yourself to the person sitting next to you why did they decide to study computing?

Overview 3 after this lecture, you should be able to understand the basics of while loops understand the basics of nested while loops write programs using while loops to solve simple problems know about the course style guide (note: you shouldn t be able to do all of these immediately after watching this lecture. however, this lecture should (hopefully!) give you the foundations you need to develop these skills. remember: programming is like learning any other language, it takes consistent and regular practice.)

Admin 4 Don t panic! lecture recordings are on WebCMS3 Echo360 was sad last night :( weekly tests start this week don t be scared! course style guide published

Loops 5 what if we want to do something multiple times? Use a loop! keep doing this while this condition is true

Anatomy of a Loop 6 initialisation. condition. statements. update.

Anatomy of a Loop 7 initialisation set up our variables condition. statements. update.

Anatomy of a Loop 8 initialisation set up our variables condition while something statements. update.

Anatomy of a Loop 9 initialisation set up our variables condition while something statements things we do inside our loop update.

Anatomy of a Loop 10 initialisation set up our variables condition while something statements things we do inside our loop update move along to the next iteration

Aside: Definitions 11 iterate perform repeatedly iteration the repetition of a process

A Counting Loop 12 Do this thing n different times sometimes, it s explicit: e.g. print out hello world! 10 times sometimes, it s not: e.g. print out the numbers from 1-10 e.g. calculate the power of a number (e.g., )

A Counting Loop 13 Do this thing n times use a loop counter a variable that we use in our loop to count how many times we ve done something

A Counting Loop 14 do something until we ve done it n times e.g. print out hello world! 10 times counter starts at 0 print hello world! ; increase counter to 1 (we ve done it once) print hello world! ; increase counter to 2 (we ve done it twice) print hello world! ; increase counter to 3 (we ve done it three times) print hello world! ; increase counter to 9 (we ve done it 9 times) print hello world! ; increase counter to 10 (we ve done it 10 times) now stop, because we ve done it 10 times.

A Counting Loop 15 how would we code this? start our counter at 0 print hello world! while counter is less than 10, increase our counter by 1

Anatomy of a Loop 16 initialisation set up our variables condition while something statements things we do inside our loop update move along to the next iteration

17???? while (?????) {???????? }

18 initialisation // set up our loop counter, start at 0 while (?????) {???????? }

19 initialisation condition // set up our loop counter, start at 0 while (something) {???????? }

20 initialisation condition statements // set up our loop counter, start at 0 while (something) { // do something???? }

21 initialisation condition statements update // set up our loop counter, start at 0 while (something) { // do something // move to the next iteration of the loop }

22 initialisation condition statements update int i = 0; while (something) { // do something // move to the next iteration of the loop }

23 initialisation condition statements update int i = 0; while (i < 10) { // do something // move to the next iteration of the loop }

24 initialisation condition statements update int i = 0; while (i < 10) { printf ("hello, world!\n"); // move to the next iteration of the loop }

25 initialisation condition statements update int i = 0; while (i < 10) { printf ("hello, world!\n"); i = i + 1; }

how do we know when to stop? 26

Loop Counters 27 int i = 0; while (i < 10) { printf ("hello, world!\n"); i = i + 1; }

Loop Counters 28 // Print out "hello, world!" n times, // where n is chosen by the user. int num; printf ("Enter a number: "); scanf ("%d", &num); int i = 0; while (i < num) { printf ("hello, world!\n"); i = i + 1; }

Sentinel Value (Flag) 29 int finished = 0; while (!finished) { printf ("hello, world!\n"); finished = 1; }

Sentinel Value (Flag) 30 // Print out the number that the user entered // Stop when they type 0 int n = 1; while (n!= 0) { printf ("You entered: %d\n", n); scanf("%d", &n); }

what is a style guide? 31

Style Guide 32 https://cgi.cse.unsw.edu.au/~cs1511/resources/style_guide.html linked from WebCMS3

nested loops 33 loops inside loops

nested loops 34 while (something) { while (somethingelse) { } }

Demo: Printing a Square 35 scan in a number: width print out a square of width * width stars. e.g. for width = 4: * * * * * * * * * * * * * * * * challenge: can you just print the outside? * * * * * * * * * * * *

Feedback? 36 bit.do/comp1511-feedback-week3 alternate link: https://andrewb3.typeform.com/to/kuvzp4