Fern   davin.50webs.com
New Zealanders making it harder to hate computers

       Main Menu        Research Projects       Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address     Computer Games        Web Design           Java Programming       The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      


S.J.S. tutorial 3: loops that use the for construct

SECTION(3, Tutorial 3) m5_question(BO(Question 3.1a: For loops that count up in steps of one.) Study the following code and verify that it prints out TT("2 3 4 5 6 7 8 9 10") by compiling and running it. Notice that the SYSTEM_OUT_PRINT function call doesn't print a carriage return after printing the argument value. That is why the SYSTEM_OUT_PRINTLN function call is needed at the end of the EM(for) loop, to print a carraige return at the end of the line. Also note the use of the plus sign to concatenate a string and the number to produce another string. m4_begin_indent NU()CLASS CLSS(ForTest) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD FUR (VAR INT i=NUMB(2); LTEQ(i,NUMB(10)); i=i+NUMB(1)) SYSTEM_OUT_PRINT(STRI(" ") + i); EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 3.1b:) Change the for loop to print out TT("5 6 7 8 9 10").) m5_question(BO(Question 3.1c:) Change the for loop to print out TT("234 235 236 237 238").) m5_question(BO(Question 3.1d:) Change the for loop to print out TT("48 49 50 ... 75 76").) m5_question(BO(Question 3.1e:) Change the for loop to print out TT("-5 -4 -3 -2 -1 0 1 2 3").) m5_question(BO(Question 3.2a: For loops that count up in steps greater than one.) Study the following code and verify that it prints out TT("10 15 20 25 30 35 40") by compiling and running it. m4_begin_indent NU()CLASS CLSS(ForTest) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD FUR (VAR INT VARI(i)=NUMB(10); LTEQ(i,NUMB(40)); i=i+NUMB(5)) SYSTEM_OUT_PRINT(STRI(" ") + i); EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 3.2b:) Change the for loop to print out TT("20 25 30 35 40").) m5_question(BO(Question 3.2c:) Change the for loop to print out TT("100 105 110 115 120 125").) m5_question(BO(Question 3.2d:) Change the for loop to print out TT("2 4 6 8 10 12 14").) m5_question(BO(Question 3.2e:) Change the for loop to print out TT("10 13 16 19 22 25").) m5_question(BO(Question 3.3a: For loops that count down in steps of one.) Study the following code and verify that it prints out TT("10 9 8 7 6 5 4 3 2 1") by compiling and running it. m4_begin_indent NU()CLASS CLSS(ForTest) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD FUR (VAR INT VARI(i)=NUMB(10); GTEQ(i,NUMB(1)); i=i-NUMB(1)) SYSTEM_OUT_PRINT(STRI(" ") + i); EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 3.3b:) Change the for loop to print out TT("10 9 8 7 6 5 4").) m5_question(BO(Question 3.3c:) Change the for loop to print out TT("20 19 18 17 16 15 14 13 12").) m5_question(BO(Question 3.3d:) Change the for loop to print out TT("66 65 64 ... 47").) m5_question(BO(Question 3.3e:) Change the for loop to print out TT("3 2 1 -1 -2 -3 -4 -5 -6 -7").) m5_question(BO(Question 3.4a: For loops that count down in steps greater than one.) Study the following code and verify that it prints out TT("100 90 80 70 60 50 40 30 20") by compiling and running it. m4_begin_indent NU()CLASS CLSS(ForTest) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD FUR (VAR INT VARI(i)=NUMB(100); GTEQ(i,NUMB(20)); i=i-NUMB(10)) SYSTEM_OUT_PRINT(STRI(" ") + i); EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 3.4b:) Change the for loop to print out TT("80 70 60 50 40 30 20").) m5_question(BO(Question 3.4c:) Change the for loop to print out TT("500 490 480 470 460").) m5_question(BO(Question 3.4d:) Change the for loop to print out TT("10 8 6 4 2 0"). ) m5_question(BO(Question 3.4e:) Change the for loop to print out TT("33 28 23 18 13 8 3").) m5_question(BO(Question 3.5a: For loops that use floating point numbers to count.) Study the following code and verify that it prints out TT("1.1 2.2 3.3 4.4") by compiling and running it. The type name EM(double) is short for EM(double precision floating point). It is natural to ask: why not use single precision floating point? The answer to this question is that double precision floating point gives fewer compilation errors than single precision floating point does. m4_begin_indent NU()CLASS CLSS(ForTest) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD FUR (VAR DOUBLE VARI(i)=NUMB(1.1); LTEQ(i,NUMB(4.4)); i=i+NUMB(1.1)) SYSTEM_OUT_PRINT(STRI(" ") + i); EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 3.5b:) Change the for loop to print out TT("0 2.2 4.4 6.6"). Note that rounding errors may prevent you from getting this exact answer. Also note that the answer to this question is not what you would naively expect without running the code.) m5_question(BO(Question 3.5c:) Change the for loop to print out TT("-30 -19.9 -9.8 0.3 10.4 20.5").) m5_question(BO(Question 3.5d:) Change the for loop to print out TT("100.0 96.7 93.4 90.1 86.8 83.5 80.2 76.9").) m5_question(BO(Question 3.5e:) Change the for loop to print out TT("-100.0 -105.5 -111.0 -116.5").) m5_question(BO(Question 3.6a: For loops that use chars to count.) Study the following code and verify that it prints out TT("a b c d e f g h i j k l m n o p q r s t u v w x y z") by and running it. m4_begin_indent NU()CLASS CLSS(ForTest) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD FUR (VAR CHAR VARI(i)=STRI('a'); LTEQ(i,STRI('z')); i=i+NUMB(1)) SYSTEM_OUT_PRINT(STRI(" ") + i); EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 3.6b:) Change the for loop to print out TT("a b c d e f").) m5_question(BO(Question 3.6c:) Change the for loop to print out TT("z y x w v u t s r q p o n m l k j i h g f e d c b a").) m5_question(BO(Question 3.6d:) Change the for loop to print out TT("p o n m l k j i h").) m5_question(BO(Question 3.6e:) Change the for loop to print out TT("A [[B]] C D E [[F]] G H I J K L M N O P Q R [[S]] T U V W X Y Z").)
Back to Main Menu
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Programming | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |

Please report any broken links to the Web-person
Last modified: Tue Mar 25 12:26:49 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.