More on Loops v So far weve learnt

  • Slides: 12
Download presentation
More on Loops v So far, we’ve learnt about ‘while’ loops. v Today will

More on Loops v So far, we’ve learnt about ‘while’ loops. v Today will learn about ‘for’ loops. CS 150 Introduction to Computer Science 1 1

For loops v 3 main things for loops: Ø Initialization of lcv, testing of

For loops v 3 main things for loops: Ø Initialization of lcv, testing of lcv, updating lcv v For loops provide a concise way to do this for (count = 0; count < 5; count++) cout << count << endl; CS 150 Introduction to Computer Science 1 2

General Format for (initialization expression; loop repetition condition; update expression) { statements; } CS

General Format for (initialization expression; loop repetition condition; update expression) { statements; } CS 150 Introduction to Computer Science 1 3

Examples v Write a for loop that outputs odd numbers less than 10 v

Examples v Write a for loop that outputs odd numbers less than 10 v Write a program that computes the factorial of a number CS 150 Introduction to Computer Science 1 4

Localized Declarations for (int i = 0; i < n; i++) cout << i

Localized Declarations for (int i = 0; i < n; i++) cout << i << endl; i is declared ONLY in the loop CS 150 Introduction to Computer Science 1 5

Rewrite using a while loop for (i = 5; i < 10; i+= 2)

Rewrite using a while loop for (i = 5; i < 10; i+= 2) cout << i; What does this output? CS 150 Introduction to Computer Science 1 6

Formatting Output v How can we generate this output? Fahrenheit Celsius 32. 000 0.

Formatting Output v How can we generate this output? Fahrenheit Celsius 32. 000 0. 000 33. 000 0. 556 34. 000 1. 111 35. 000 1. 667 36. 000 2. 222 CS 150 Introduction to Computer Science 1 7

The Program float F, C; F = 32. 0; cout << fixed << setprecision(3);

The Program float F, C; F = 32. 0; cout << fixed << setprecision(3); cout << "Fahrenheit "; cout << "Celsius" << endl; while (F <= 212. 0) { C = (5. 0/9. 0)*(F - 32); cout << setw(10) << F << setw(12) << C << endl; F += 1; } v You must have #include<iomanip> CS 150 Introduction to Computer Science 1 8

Programs v Write a program that will print the sum of the odd integers

Programs v Write a program that will print the sum of the odd integers between 1 and 50 inclusive. Write one program using a while and the other using a for loop. CS 150 Introduction to Computer Science 1 9

Program v Write a program that inputs 50 numbers and outputs the largest and

Program v Write a program that inputs 50 numbers and outputs the largest and the smallest number CS 150 Introduction to Computer Science 1 10

Conditional Loops v Loops that execute until some condition is met v Includes more

Conditional Loops v Loops that execute until some condition is met v Includes more than counting v Use while loops CS 150 Introduction to Computer Science 1 11

Program v Write a flag controlled loop that continues to read pairs of integers

Program v Write a flag controlled loop that continues to read pairs of integers until it reads a pair with the property that the first integer is evenly divisible by the second CS 150 Introduction to Computer Science 1 12