cs31 lecture 5

Do 2 str readfromkeyboard str 3 while not

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: er that Algorithm ReadInt: Output: • i: integer number typed by the user 1. do 2. str := readFromKeyboard() str 3. while (not isInteger(str)) 4. return convertStrToInt(str) 14 Why multiple kinds of loops? x Technically, all three can be expressed with a while loop x Other loop kinds are syntactic sugar x But, depending on the exact behavior expected, one kind But, of loop will lead to the most clear / most compact code of 15 Typical errors x Writing the loop iterator in the loop body for a for loop Example: for i in 1..10 do print i print i := i + 1 := done x Having a loop upper bound / exit condition which never Having happens (infinite loop) happens Example: i := 0 do print i print i := i + 1 := while (i >= 0) while 16 And now in C++! x For loops, While loops and Do loops are available in C++ x The syntax is precisely defined, esp. for the for loops x There is in C++ an equivalence between all three loops Example: all are while loops in GCC 17 for loops in C++ x Syntax for ([<init>]; [<test>]; [<increment>]) <instructions> <instructions> x It can be seen as: <init> while (<test>) do <instructions> <instructions> <increment> <increment> done done 18 for loops in C++ x Example: for (i = 0; i < 10; ++i) std::cout << i << std::endl; std::cout x Some properties: The <init> statement is executed only once, and can contain The variable declarations variable The <test> is executed at each iteration, including the first one. The The loop iterates while this test is true The The <test> can be an arbitrarily complex expression that can be The converted to int 19 More examples x Example: multiple initializations: for (int i = 1, j = 2; j <= 10 && i < 100; i++) x Example: multiple increments: Example: for (int i = 1, j = 2; j <= 10 && i < 100; i++, j++) for x Example: infinite loop: Example: for (int i = 1, j = 2; ; i++, j++) for 20 While loops x Straightforward adapting from algorithmic language! x Syntax: while (<condition>) <instructions> <instructions> x Example: while (i < 10) std::cout << i << std::endl; std::cout 21 Do loops x Straightforward adapting from algorithmic language! x Syntax: do <instructions> <instructions> while (<condition>) x Example: do std::cout << i << std::endl; std::cout while (i < 10) 22 A reminder about blocks of instructions x A block of more than one instruction is encapsulated in block { and } and Example: { std::cout << i << std::endl; std::cout i = i + 1; 1; } x In loops in C++, <instructions> is either a single In instruction, or a block of instructions encapsulated in { } instruction, 23 More C++ examples 24...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online