CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Reading: K&R 3.1-3.3.
Please submit a question by 9 p.m. on Tuesday, 22 February 2011.
So an "else if" statement is like an if statement but it's like "if this happens, do this." but rather than "else" only, it goes into another if statement. Is that correct?
Yes. If the first test fails, it goes on and does the next test. If the first test succeeds, it skips the rest.
Why does it say
if else if else if else if...
instead of just saying
if if if....
What's the difference between a series of else-if statements and a series of if statements? If the if returns false, won't it just move on to the next statement in order anyways?
Because the first form makes it easier to select exactly one consequent. Consider the case of determining what letter grade a number corresponds to. (You may remember a similar problem from 151.) We can write
if (grade >= 90)
printf ("A\n");
else if (grade >= 80)
printf ("B\n");
else if (grade >= 70)
printf ("C\n");
else if (grade >= 60)
printf ("D\n");
else
printf ("F\n");
In contrast to
if (grade >= 90)
printf ("A\n");
if ((grade >= 80) && (grade < 90))
printf ("B\n");
...
And, as that example suggests, the great thing about the sequential
if else if else if ... is that you can have a final
else clause that holds if none of the other conditions
hold.
How does the code react to other nested ifs instead of else if?
If you don't have the else if, it doesn't really nest.
In the code for binsearch at the end of K&R 3.3, why isn't
there a check for whether v is sorted or not? I'm also confused about
how the search works. In scheme we divided the thing being searched and
discarded the half that couldn't include our key but I don't see code
indicating that?
A check is more expensive than searching, so we don't want to do the check. And we are doing the splitting. We'll go over it in class.
In binsearch, what if v is not in increasing order? Is there a way to sort v after it has been set so it becomes that way?
If the array is not sorted, binary search will rarely work correctly. There are a wide variety of sorting algorithms, some of which we'll cover this semester.
A question about the example program on page 58: in this example, and in other examples in the book, there is no "main". In the first section of K&R, they write "every program must have a "main" somewhere"; so, is the example on 58 just part of a larger program? If not, you could explain more what "main" does, and why we wouldn't always need it?
When you have a lot of functions in a program, the computer needs
to know where to start. The main routine is where it
starts running the program.
Why is assigning l-r or r-l important?
The order is less important than you knowing what happens. But the
order is natural. Consider a situation in which x is 5,
y is 6, and z is 7. If we write x
= y = z, it seems to make more sense that all three get the
value 7 (right to left) than if x gets 6 (the old value
of y) and then y gets 7 (the value of
z).
What does the (?) do?
It's part of the conditional expression
.
Is there an easier way to say "if not" besides if ((expression)
= 0)? Or is that what (! expression) does?
Yes, that's what (! expression) does.
Conditional expressions are evaluated to a value of zero if false and to a non-zero value if true. If we write a function that returns 0 or -1 for two different cases that would make something false, are we not following standardized practice because we are treating the non-zero value "-1" as false?
You are not following standards, but it will still work fine. When
your function is used in a conditional (e.g., with &&,
||, or !), we'll end up with 0 or 1.
Why didn't we read this chapter at the beginning of the semester, when we first learned about if statements? The information was really useful.
You'll find the same thing when we cover loops, functions, and the
rest. We started with a quick introduction. Now we're going back to
look at things in more depth. It's the immersion
approach.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed Feb 23 10:29:57 2011.
The source to the document was last modified on Wed Feb 23 10:29:56 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/conditionals-1.html.