ATS 315: Working With Loops in C Loops in C ATS 315.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 4 - Control Statements
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Files Working with Files in C ATS 315. Files Misunderstandings about “files” In Windows and on Macs, we tend to think of files as “containing something”.
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Counting Loops.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Lesson #5 Repetition and Loops.
User-Written Functions
Introduction To Repetition The for loop
Lecture 7: Repeating a Known Number of Times
Lesson #5 Repetition and Loops.
Chapter 4 Loops DDC 2133 Programming II.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
Looping.
For Loops October 12, 2017.
Arrays, For loop While loop Do while loop
Lesson #5 Repetition and Loops.
Structured Program
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Chapter (3) - Looping Questions.
Chapter 3 - Structured Program Development
3 Control Statements:.
INC 161 , CPE 100 Computer Programming
Chapter 3 - Structured Program Development
Computing Fundamentals
Computer programming Lecture 3.
Objectives You should be able to describe: The while Statement
-2- Introduction to C Programming
Lesson #5 Repetition and Loops.
Building Java Programs
Introduction to C Programming
ICS103: Programming in C 5: Repetition and Loop Statements
Looping and Repetition
Presentation transcript:

ATS 315: Working With Loops in C Loops in C ATS 315

ATS 315: Working With Loops in C Loops Are used to perform some action multiple times. Are crucial to most C programs that are useful.

ATS 315: Working With Loops in C Two Main Kinds of Loops Loops that run a specific number of times. –“for” loops Loops that run a flexible number of times, if at all. –“while” loops

ATS 315: Working With Loops in C “for” loops Require a “counter” that keeps track of how many times the loop has run. Typically this is an integer, but it doesn’t have to be.

ATS 315: Working With Loops in C “for” loop syntax Sample loop that will run 5 times. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); }

ATS 315: Working With Loops in C “for” loop syntax On the first pass through the loop, i=0. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); }

ATS 315: Working With Loops in C “for” loop syntax “What the loop does” is inside a set of curly braces. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); }

ATS 315: Working With Loops in C “for” loop syntax Traditionally we indent the stuff inside a loop a little farther. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); }

ATS 315: Working With Loops in C “for” loop syntax After each pass through the loop, the “loop control variable” will be changed as shown here. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); }

ATS 315: Working With Loops in C “i++” ?!?!?!?!!!!??? i++ is a shortcut for “i=i+1” To increase in a different “step”, just be more explicit about it: i=i+2

ATS 315: Working With Loops in C “for” loop syntax As long as the loop control variable passes the “condition”, it will continue to loop. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); }

ATS 315: Working With Loops in C “for” loop syntax This loop will run 5 times: i=0,1,2,3,4 main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); }

ATS 315: Working With Loops in C “for” loop syntax Traditional format: –There are three “arguments” in the for loop: for(i=0;i<n;i++) { }

ATS 315: Working With Loops in C “for” loop syntax Traditional format: –1. The starting value of the loop—usually zero. for(i=0;i<n;i++) { }

ATS 315: Working With Loops in C “for” loop syntax Traditional format: –2. The “condition” for the loop—it will run as long as this is true. for(i=0;i<n;i++) { }

ATS 315: Working With Loops in C “for” loop syntax Traditional format: –3. The “increment” of the loop—how to modify the value of the loop control variable. for(i=0;i<n;i++) { }

ATS 315: Working With Loops in C Floating Point Loops for(pressure=1000.0;pressure<1024.0; pressure=pressure+4.0) { }

ATS 315: Working With Loops in C Nested Loops One thing that could be inside of a loop is another loop. Excellent for working with grids of data…

ATS 315: Working With Loops in C Nested Loops for(i=0;i<5;i++) { for(j=0;j<3;j++) { printf (“%d %d\n”,i,j); }

ATS 315: Working With Loops in C The “while” Loop Is used when you don’t know in advance how many times the loop should run… it just needs to run “while” something is true.

ATS 315: Working With Loops in C “while” loop syntax Loop control variable needs to be “initialized”. tempF = 0.; while (tempF > -98.0) { printf (“Enter a temp in F to convert to C.\n”); printf (“Or enter -99 to end.\n”); scanf(“%f”,&tempF); tempC = (tempF-32.)*(5./9.); printf (“The temperature is %4.1fC.\n\n”); }

ATS 315: Working With Loops in C “while” loop syntax Loop will repeat “while” condition is true. tempF = 0.; while (tempF > -98.0) { printf (“Enter a temp in F to convert to C.\n”); printf (“Or enter -99 to end.\n”); scanf(“%f”,&tempF); tempC = (tempF-32.)*(5./9.); printf (“The temperature is %4.1fC.\n\n”); }

ATS 315: Working With Loops in C “while” loop syntax Loops are generally indented. tempF = 0.; while (tempF > -98.0) { printf (“Enter a temp in F to convert to C.\n”); printf (“Or enter -99 to end.\n”); scanf(“%f”,&tempF); tempC = (tempF-32.)*(5./9.); printf (“The temperature is %4.1fC.\n\n”); }

ATS 315: Working With Loops in C Uses of “while” “while” loops are generally used to trap errors.

ATS 315: Working With Loops in C Your Assignment Make a table of wind chill temperatures! 1.Prompt the user for an air temperature 2.Prompt the user for a lowest wind speed 3.Prompt the user for a highest wind speed 4.Prompt the user for an interval