Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: CS150 For.

Similar presentations


Presentation on theme: "C++ Programming: CS150 For."— Presentation transcript:

1 C++ Programming: CS150 For

2 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

3 for Looping (Repetition) Structure (continued)

4 | for statement header components.

5 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 )

6 for Looping (Repetition) Structure (continued)

7 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;

8 for Looping (Repetition) Structure (continued)

9 do…while covert to For

10 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

11 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

12 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

13 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;

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

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

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

17 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 

18 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

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

20 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 ).

21 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 ...

22 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;

23 // 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;

24 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;

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

26 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.

27 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.

28 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.


Download ppt "C++ Programming: CS150 For."

Similar presentations


Ads by Google