CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Chapter 4: Control Structures II
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
The ‘while’ loop ‘round and ‘round we go.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CMCS 104, Lecture 25 - REVIEW1 Review l Read textbook chapters l Read Lectures l General Programming Tips: ouse Top-Down Design ouse Incremental.
Final Review1 Final Exam l Final Exam: Thursday December 15, 2005 at 8:30 pm in room MP-008, the regular classroom. If you can not make it to the exam.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
- Standard C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
The C “switch” Statement
The while Looping Structure
The C “switch” Statement
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
Program Control Topics While loop For loop Switch statement
UMBC CMSC 104 – Section 01, Fall 2016
REPETITION STATEMENTS
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
The switch Statement Topics Multiple Selection switch Statement
The while Looping Structure
UMBC CMSC 104 – Section 01, Fall 2016
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops

CMSC 1042 #include main () { int i = 1;initialization of loop control variable /* count from 1 to 10 */ while ( i < 11 )test condition that terminate loop { printf (“%d “, i); i++;modification of loop control }variable } Counter-Controlled Repetition with a while loop

CMSC 1043 The for loop Repetitive Structure l The for loop handles details of the counter-controlled loop automatically l The initialization of the the loop control variable, termination conditional test and modification are handled in for loop structure for ( i = 1; i < 11; i++) { initializationmodification }test

CMSC 1044 When does the for loop initialize, test and modify ? l Just as in the while loop that counted, the for loop oInitializes the loop control variable before beginning omodified the loop control variable at the very end of each iteration of the loop operforms the conditional termination test before each iteration of the loop l The for loop is easier to write

CMSC 1045 A for loop that counts from 0 to 9 for (i = 0; i < 10; i++) { printf (“%d”, i); } printf (“\n”);

CMSC 1046 We can count backwards, too for (i = 10; i > 0; i--) { printf (“%d”, i); } printf (“\n”);

CMSC 1047 We can count by 2’s... or 7’s... or whatever for (i = 0; i < 10; i += 2) { printf (“%d”, i); } printf (“\n”);

CMSC 1048 The do-while repetitive structure do { statement(s) } while (condition); l The body of the do-while is ALWAYS executed at least once

CMSC 1049 do-while example do { printf (“Enter a positive number: “); scanf (“%d”, &num); if (num <= 0) { printf (“\nThat is not positive, try again\n”); } } while (num <= 0);

CMSC A while that tests input Compare with do-while printf (“Enter a positive number: “); scanf (“%d”, &num); while (num <= 0) { printf (“\nThat is not positive, try again\n”); printf (“Enter a positive number: “); scanf (“%d”, &num); }

CMSC for vs while l use a for loop when your program “knows” exactly how many times to loop l use a while loop when there is a condition that will terminate your loop

CMSC for loops for specified number of iterations printf (“Enter the number of students: “); scanf (“%d”, &numStudents); /* we now “know” how many times to loop */ for (student = 1; student < numStudents; student++) { printf (“Enter grade: “); etc. }

CMSC use while when a condition terminates your loop l the use of a sentinel is a good example...your program doesn’t “know” when the SENTINEL will be encountered printf (“Enter grade: “); scanf (“%d”, &grade); while (grade != SENTINEL) { etc. }

CMSC while vs do-while l while required “priming read” l do-while required extra test l use do-while when body must be executed at least once

CMSC break l break can be used in while, do-while and for loops to cause premature exit of the loop. l THIS IS NOT A RECOMMENDED CODING TECHNIQUE

CMSC Example break in a loop #include main ( ) { int i; for (i = 1; i < 10; i++) { if (i == 5) { break; } printf (“%d “, i); } printf (“\nbroke out of loop at i = %d\n”, i); } OUTPUT: Broke out of loop at i = 5

CMSC continue l continue can be used in a for, while, or do-while loop l It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop. The loop continues with the next iteration

CMSC Example of continue in a loop #include main ( ) { int i; for (i = 1; i < 10; i++) { if (i == 5) { continue; } printf (“%d”, i); } printf (“\n”); } OUTPUT:

CMSC Nested for loops for (i = 1; i < 5; i++) { for (j = 1; j < 3; j++) { if (j % 2 == 0) { printf (“O”); } else { printf (“X”); } printf (“\n”); } How many times is the ‘if’ statement executed? What is the output ??

CMSC The char data type l The char data type holds a single characterchar ch; l The char is held as a one-byte integer in memory. The ASCII code is what is actually stored, so we can use them as characters or integers, depending on our purpose l Use scanf (“%c”, &ch); to input 1 char

CMSC Character Example #include main ( ) { char ch; printf (“Enter a character: “); scanf (“%c”, &ch); printf (“The value of %c is %d.\n”, ch, ch); } If the user entered an A the output would be The value of A is 65.

CMSC The getchar ( ) function l We can also use the getchar() function that is found in the stdio library l The getchar ( ) function reads one character from stdin and returns that character (value) l The value can then be stored in either a char variable or an integer variable

CMSC getchar () example #include main ( ) { char grade; printf (“Enter a letter grade: “); grade = getchar ( ); printf (“\nThe grade you entered was %c.\n”, grade); }