CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Control statements Mostafa Abdallah
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Switch Statements Comparing Exact Values
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CprE 185: Intro to Problem Solving (using C)
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Chapter 6 More Conditionals and Loops
Loop Structures.
Programming Fundamentals
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Control Structures.
Chapter 6 More Conditionals and Loops
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
The ‘while’ Statement September 27, 2006
Chapter 3: Program Statements
3.5- The while Statement The while statement has the following syntax:
What output is produced by the following fragment?
CprE 185: Intro to Problem Solving (using C)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
CSCI 1100/1202 February 6, 2002.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

CprE 185: Intro to Problem Solving (using C) Instructor: Alexander Stoytchev http://www.cs.iastate.edu/~alex/classes/2008_Fall_185/

Loops (part 1) CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Administrative Stuff Midterm grades due next Friday Oct 17 Please double check your grades on Web CT

Administrative Stuff HW5 is out (due next Wed, Oct 15 @ 8pm) HW4 is due this Wed, Oct 8 @ 8pm

Quick Review of Last Lecture

The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements The flow of control transfers to statement associated with the first case value that matches © 2004 Pearson Addison-Wesley. All rights reserved

The switch Statement Often a break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this may be appropriate, but often we want to execute only the statements associated with one case © 2004 Pearson Addison-Wesley. All rights reserved

The switch Statement An example of a switch statement: switch (option) { case 'A': aCount++; break; case 'B': bCount++; case 'C': cCount++; default: otherCount++; } © 2004 Pearson Addison-Wesley. All rights reserved

The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch © 2004 Pearson Addison-Wesley. All rights reserved

The switch Statement The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int,) or a char It cannot be a floating point value (float or double) The implicit test condition in a switch statement is equality You cannot perform relational checks with a switch statement © 2004 Pearson Addison-Wesley. All rights reserved

The switch Statement The general syntax of a switch statement is: and case are reserved words switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } If expression matches value2, control jumps to here © 2004 Pearson Addison-Wesley. All rights reserved

Chapter 5 (Loops)

Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the do loop the for loop The programmer should choose the right kind of loop for the situation © 2004 Pearson Addison-Wesley. All rights reserved

There are three loop constructs in C do-while loop (or do loop for short) while loop for loop We’ll quickly cover all of them today. We’ll revisit them again next time. Loops = repetition statements

Logic of an if statement condition evaluated false statement true © 2004 Pearson Addison-Wesley. All rights reserved

Logic of a do Loop statement true condition evaluated false © 2004 Pearson Addison-Wesley. All rights reserved

The do Statement A do statement has the following syntax: do { } while ( condition ); The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false © 2004 Pearson Addison-Wesley. All rights reserved

The do Statement An example of a do loop: int count = 0; do { count++; printf(“%d\n”, count); } while (count < 5); The body of a do loop is executed at least once © 2004 Pearson Addison-Wesley. All rights reserved

Example: Fixing Bad Keyboard Input Write a program that refuses to accept a negative number as an input. The program must keep asking the user to enter a value until he/she enters a positive number. How can we do this?

Example: Reverse a number

Logic of a while Loop condition evaluated false true statement © 2004 Pearson Addison-Wesley. All rights reserved

The while Statement A while statement has the following syntax: while ( condition ) statement; If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false © 2004 Pearson Addison-Wesley. All rights reserved

The while Statement An example of a while statement: int count = 1; while (count <= 5) { printf (“%d\n”, count); count++; } If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times © 2004 Pearson Addison-Wesley. All rights reserved

The while Statement Let's look at some examples of loop processing A loop can be used to maintain a running sum A sentinel value is a special input value that represents the end of input A loop can also be used for input validation, making a program more robust © 2004 Pearson Addison-Wesley. All rights reserved

Comparing while and do The while Loop The do Loop statement condition true false condition evaluated The while Loop true condition evaluated statement false The do Loop © 2004 Pearson Addison-Wesley. All rights reserved

Logic of a for loop initialization condition evaluated false statement true increment © 2004 Pearson Addison-Wesley. All rights reserved

The for Statement A for statement has the following syntax: The initialization is executed once before the loop begins The statement is executed until the condition becomes false for ( initialization ; condition ; increment ) statement; The increment portion is executed at the end of each iteration © 2004 Pearson Addison-Wesley. All rights reserved

The for Statement A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; } © 2004 Pearson Addison-Wesley. All rights reserved

The for Statement An example of a for loop: for (int count=1; count <= 5; count++) printf (“%d\n”, count); The initialization section can be used to declare a variable Like a while loop, the condition of a for loop is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times © 2004 Pearson Addison-Wesley. All rights reserved

The for Statement The increment section can perform any calculation Int num; for (num=100; num > 0; num -= 5) printf (“%d\n”, num); A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance © 2004 Pearson Addison-Wesley. All rights reserved

The for Statement Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the increment is left out, no increment operation is performed © 2004 Pearson Addison-Wesley. All rights reserved

TO BE CONTINUED…