C++ Programming: CS150 For.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
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.
Chapter 5: Control Structures II (Repetition)
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
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 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 4: Control Structures II
1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Structures
Introduction to Computer Programming
Topic 4: Looping Statements
Review 1.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Controlling execution - iteration
Chapter 5: Control Structures II
Control Structures II (Repetition)
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 5: Control Structures II
Lecture 4B More Repetition Richard Gesick
Control Structures Lecture 7.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Repetition Control Structure
Program Control Topics While loop For loop Switch statement
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
2.6 The if/else Selection Structure
Chapter 5: Control Structures II (Repetition)
Control Statements Paritosh Srivastava.
Chapter 4 Repetition Structures
Presentation transcript:

C++ Programming: CS150 For

for Looping (Repetition) Structure The general form of the for statement is: The initial statement, loop condition, and update statement are called for loop control statements initial statement usually initializes a variable (called the for loop control, or for indexed, variable) In C++, for is a reserved word

for Looping (Repetition) Structure (continued)

| for statement header components.

Examples Using the for Statement Vary control variable from 1 to 100 in increments of 1 for ( int i = 1; i <= 100; i++ ) Vary control variable from 100 to 1 in increments of -1 for ( int i = 100; i >= 1; i-- ) Vary control variable from 7 to 77 in steps of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in steps of -2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 ) Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 )

for Looping (Repetition) Structure (continued)

for Looping (Repetition) Structure (continued) C++ allows you to use fractional values for loop control variables of the double type Results may differ The following is a semantic error: The following is a legal for loop: for (;;) cout << "Hello" << endl;

for Looping (Repetition) Structure (continued)

do…while covert to For

Choosing the Right Looping Structure All three loops have their place in C++ If you know or can determine in advance the number of repetitions needed, the for loop is the correct choice If you do not know and cannot determine in advance the number of repetitions needed, and it could be zero, use a while loop If you do not know and cannot determine in advance the number of repetitions needed, and it is at least one, use a do...while loop

break and continue Statements break and continue alter the flow of control break statement is used for two purposes: To exit early from a loop Can eliminate the use of certain (flag) variables To skip the remainder of the switch structure After the break statement executes, the program continues with the first statement after the structure

break & continue Statements (continued) continue is used in while, for, and do…while structures When executed in a loop It skips remaining statements and proceeds with the next iteration of the loop

Convert to For n = 5; j = 1; while ( j < 4 ) { if ( n >= 10 ) n = n – 2; else n = n * j; j++; } cout<<“ The n is ”<< n;

Count-controlled Loop int count ; for ( count = 4 ; count > 0 ; count-- ) { cout<< count<<endln ; } Cout << “Done” ; OUTPUT: 4 3 2 1 Done

Find the output for ( m=1 ; m<=5 ; m=m+1 ) { x = pow ( m , 2 ); cout<<m<< x; }

Find the output for ( m=1 ; m<=5 ; m=m+2 ) { x = pow ( m , 2 ); cout<<m<< x; }

Example Write a program to ask the user for a positive integer, and then display its factorial. Given that factorial(n) is 1 X 2 X 3 …… x n 

Example Write a program that does a survey on a certain question. The question has four possible answers. Run the survey on 20 people and then display the number of people who chose each answer. Example: What is your favorite subject? A. Mathematics B. Economics C. Programming D. Physics

Example Write a program to display all the numbers divisible by 5 in the range 0 to 5000.

Example write a program that reads in the grades of 50 students in a course (out of 100 points each ) and then count the number of A students ( grade > 85 ) and the number of B students (grade > 75 ).

Temperature example A program calculates the average temperature for a city, based upon the average temperature of each month. Each month has a different number of temperature readings. Month howMany Readings 1 5 18 14 15 17 12 2 2 19 21 3 3 23 22 26 ...

What is the out put #include<iostream> using namespace std; void main() { int i; for(i=1;i<10;i++) cout<<i<<endl; } cout<<endl<<i<<endl;

// Using the continue statement in a for structure #include <iostream> int main() { for ( int x = 1; x <= 10; x++ ) { if ( x == 5 ) continue; cout << x << " "; } cout << "\nUsed continue to skip printing the value 5" << endl; return 0;

Nested Control Structures #include <iostream> int main () { int i,j; for (i = 1; i <= 5 ; i++) for (j = 1; j <= i; j++) cout << "*"; } return 0;

Write a C program that reads in 30 integer numbers and then print out their sum and average

Write a program that reads 100 integer numbers, determines how many positive and negative values have been read, and computes the average of the input values.

A shop need to keep an inventory of its 100 items for sale A shop need to keep an inventory of its 100 items for sale. Write application to input the item code, price and quantity of each item. Then the program displays a neat table of all items (code, price and quantity) with two asterisks ** next to items whose price > 100 pounds, and one asterisk * next to items whose price is between 50 and 100 pounds.

Write a C program to calculate the following formula for any given n Write a C program to calculate the following formula for any given n. The program should read n from the user.