16.4: Chapter 5
- Page ID
- 117500
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)5.1 While loop
- b. The loop runs six times for values of
g =1,2,3,5,8, and13. - c. 21 is the first Fibonacci number larger than 20, and for
g = 21, the condition will evaluate toFalsefor the first time. - a. The code first prints the variable
f's initial value. Then, at each iteration,g's value is printed, andg's value throughout the code's execution will be1,2,3,5,8, and13. Thus, all printed values are1,1,2,3,5,8, and13. - b. The loop runs for
n = 4,3,2,1. 5 - b. The loop's termination condition is
n = 0; therefore, the value ofnafter the loop equals0. The last printed line will be:value of n after the loop is 0. - c. Since the initialization value for
nequals4and the value ofnis only increasing, the loop condition will always remain true. The loop will never terminate.
5.2 For loop
- b. Eight characters exist in the
str_var. Iterating over all characters and incrementing the value ofcountvariable at each iteration results in thecount'svalue of8. - a. Since the initial
count's value is0, multiplyingcountby any number results in the output of0. - c. The added line prints each character that is looped on (character
c), and prints an additional * after the character. - a. The output will be an increasing sequence between
10and22(inclusive of both):10,13,16,19, and22. - c. The expression starts a sequence from
5, decreases by 1 at every step, and ends at1. - a. The
range()function generates a descending sequence, beginning from-1until-2. Hence, the output is-1. - c. When the step size is negative, the generated sequence will be in descending order. Hence,
startmust be larger thanend; otherwise, no sequence will be generated. - c. When two arguments are provided to the
range()function, arguments are considered asstartandendof the sequence, and the step size will be1. When step size is positive, the generated sequence will be in ascending order, and hence,startmust be less thanend. Otherwise, no sequence will be generated.
5.3 Nested loops
- b. The combination of
iandjvalues,(i, j), that meet the inner and outer loop conditions are as follows:
(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (4, 1)- b. Each inner loop's full execution prints three "*" characters, and the outer loop executes twice. 3. b. Each line
iof the output (i =1,2,3, and4) containsitimes values1through4. - Hence, the outer loop executes while
i <= 4, and the inner loop iterates overjwhilej's value is less than or equal to4. - a. The outer loop's execution range is
0,1, and2; hence, the outer loop executes three times. - b. The inner loop is evaluated four times (
j = 0,1,2, and3) for everyi's value set by the outer loop. In the given examplei = 0,1, and2; hence, the total number of times the inner loop executes is3 * 4 = 12. - c. The printed output displays three rows with each row
idisplayingi* values in the range of0to3. Hence, the code must iterate over row ID in the outer loop, and in each row, iterate over elements' ID in the range of0to3.
5.4 Break and continue
- c. The code prints all characters before the first occurrence of a space character in separate lines. Hence the output is:
H
e
l
l
o
- b. As soon as the
printstatement is executed, thebreakstatement causes the loop's termination. Also, fori = 15, the condition(i%3 == 0 and i%5 == 0)is met, and hence the print statement will be executed exactly once. - b. The loop finds the first five numbers that are divisible by
2or3. These numbers are2,3,4,6, and8, and thus8is the finali's value. - b. The print statement is executed for numbers within the range of 0 and 10 (inclusive) that are divisible by 3. The printed output is:
9
6
3
0
- b. The loop checks whether the character being evaluated is a space character or not. If so, the remainder of the loop will be skipped and the space character will not be printed.
5.5 Loop else
- b. The variable
iis initialized to16, and thewhileloop executes withi = 16,8,4, and2. In each loop's iteration, the variableexp's value is incremented by 1. Hence, at the end of the loop,exp = 4. Since the loop terminates without interruption, theelsestatement after the loop is executed and prints16 is 2 to the 4. - a. The initial
i's value is7. Hence, in the first loop's iteration, theelsestatement within the loop executes, and thebreakstatement terminates the loop. Since the loop's execution is interrupted, theelseafter the loop will not execute and there will be no output. - b. The loop iterates over all integer values in the list. If a value is less than
5, the value will be printed, and if the value is greater than5, the code prints "Not all numbers are less than 5." and the code's execution terminates.


