0% found this document useful (0 votes)
50 views12 pages

Tutorial 4

This document provides an overview of control structures for iteration in C++ programming. It discusses while loops and for loops, including their basic formats and usage. It provides examples of calculating Fibonacci numbers and printing a pyramid of asterisks using these looping constructs. Students are assigned to write programs to implement these examples and submit them by the given deadline.

Uploaded by

Dao Phuong Anh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views12 pages

Tutorial 4

This document provides an overview of control structures for iteration in C++ programming. It discusses while loops and for loops, including their basic formats and usage. It provides examples of calculating Fibonacci numbers and printing a pyramid of asterisks using these looping constructs. Students are assigned to write programs to implement these examples and submit them by the given deadline.

Uploaded by

Dao Phuong Anh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Tutorial 4

Control Structures for Iteration


CSIS1117B Computer Programming I

Outline
Review on looping constructs
While loop For loop

Exercise

Looping constructs
Mainly while-loop and for-loop When we use looping constructs?
When statements are executed repeatedly until a particular condition is satisfied
N matter we know the number of iterations or not No tt k th b f it ti t

Which one is better


total = 0; 0 cin >> score; if (score >=0 ) total = total } if (score >=0 ) total = total } ... ... if (score >=0 ) total = total } { + score; cin >> score; { + score; cin >> score;

VS

total = 0; cin >> score; while (score >=0 ) { total = total + score; cin >> score; }

{ + score; cin >> score;

Which one is better


total total total total total total = = = = = = 0; total total total total total total = 0; for (int i=0; i<5; i++) { total = total + score; }

+ + + + +

score; score; score; score; score;

VS

Multiple efforts to type the codes Multiple efforts to do the debugging (if there are errors) Multiple efforts to make modifications Multiple chances to make mistakes

Use looping construct p g whenever possible !

While loop
Format
while (condition) { body }

condition
Has the value of type bool (true or false) If condition is evaluated to true, then the block body is executed

When executing a while-loop


Step 1: condition is evaluated Step 2:
If condition is true, the block body is executed, and after completion, goto step 1 If condition is false, the while loop terminates without executing the bl k th block body.

Make sure the while-loop can terminate

For loop
Format
for (initialization action; condition; update action) { ( ; ; p ) body }

E.g.

for (int i=0; i<5; i++) { body i=i+1 }

When executing a for-loop


Step 1: initialization action is executed p Step 2: condition is evaluated
If condition is true, the block body is executed If condition is false, the for-loop terminates without executing the block body. body

Step 3: update action is executed, and goto step 2

Nested loops
Loops can appear inside loops
while (...) { ... while (...) { ... } ... } for (...) { ... for (...) { ... } ... }

Note the indentation

Exercise 1
Fibonacci numbers are a sequence of numbers defined by the q y following relations:

0 1 Fn F F n2 n 1

if n 0 if n 1 if n 1

Write a program fibonacci.cpp to read in a non-negative integer n and determine the n th Fibonacci number n-th number. Hints can be found in the last slide.

Sample run of the program:


C:\tutorial4>fibonacci 0 0

C:\tutorial4>fibonacci 1 1

C:\tutorial4>fibonacci 10 55

C:\tutorial4>fibonacci 46 1836311903

Exercise 2
Write a program pyramid.cpp to read in a non-negative integer non negative n, and print out a pyramid shape made of n lines of *

Sample output of the program


C:\tutorial4>pyramid Please input the height of the pyramid: 4 * *** ***** *******

Submit fibonacci.cpp & pyramid.cpp to the Assignment T4 folder of the webhandin Deadline: 11:59pm Oct 7, 2011
Sample solutions will be provided after the deadline

Hints on question 1
#include <iostream> using namespace std; int main(){ // get a non-negative integer n int n; cin >> n; if (n <= 1) cout << n << endl; else { int Fn 2 = 0; // F(n-2) Fn_2 int Fn_1 = 1; // F(n-1) int Fn; // F(n) for (int i = 2; i <= n; i++){ Fn = Fn_2 + Fn_1; Fn 2 Fn 1; ... } ... } return 0; }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy