CS50 - Section 2

Section Information

CS50 Helpful Hints

Here are some hints for how to do well in CS50. (Actually, they apply to practically any course.)

Programming in C

What is a Program?

A program is the expression or implementation of an algorithm.

There are a number of ways that algorithms can be expressed or implemented. In this class, we'll spend the first few weeks studying C which is one way to express algorithms.

C is a particularly useful way to express algorithms, since computers "understand" algorithms expressed in C. This is because C is a computer programming language- a language that can be used to express algorithms in a way that both computers and their programmers can understand.

Although we'll be concentrating on the details of C for a while, most of the principles we'll be learning (such as the principles of design and hierarchical decomposition) apply to writing programs in any computer language.

Conditional Execution

In the first few programs we saw, execution started at the beginning of the main function and proceeded to the end. That's just fine for programs like hello, but for more complicated programs we may want the execution to proceed in a different way when particular conditions occur. This is called conditional execution.

C provides several straightforward ways to do conditional execution. The most fundamental of these is the if statement, and we'll see others later in the semester.

The if statement

The if statement has two forms, depending on whether or not there is an else statement:

if ( condition ) if-stmnt

if ( condition ) if-stmnt else else-stmnt

If the condition expression is true (which in C means that it evaluates to anything non-zero), then execute the if-stmnt. Otherwise, if there is an else-stmnt (as shown in the second form of the if statement), then execute it.

Boolean operators

In order to help write condition expressions, a number of Boolean operators are available in C. A Boolean operator is an operator that evaluates to either true or false (i.e. non-zero for true, and zero for false).

Relational Operators

C includes several relational operators, such as <, >, ==, !=, <=, and >=. These are used to compare two numeric values, and evaluate to Boolean values.

Be careful to not mix up = and ==; they do very different things.

One very important property of C's relational operators (which causes many bugs for beginning C programmers) is, as mentioned above, that the relational operators only relate numeric values. In particular, they cannot be used to compare two strings (or arrays, which we'll talk about next week) for equality or compare them to see if one precedes the other in alphabetical order, etc. Interestingly enough, C will permit you to write expressions like:

		"hello" > "goodbye"

The answers will not generally be what you might expect, however! Use the Roberts libraries to compare strings.

Logical Operators

There are also several logical operators, such as &&, || and ! (logical and, inclusive or, and not). These can be used to construct more complicated condition expressions.

Once again, be careful to use && and || and not & and |. These are very different operators! (We'll talk about what they do later in the semester.)

C evaluates Boolean expressions from left to right, but uses short circuit evaluation. What this means is that C will stop evaluating a Boolean expression as soon as it becomes clear whether the expression is true or false. (This is different from the behavior of some other programming languages, so take note if you're used to programming in some other language.) For example, consider the following expression:

	condition1 && conditon2

If condition1 is false, then the entire expression must also be false- irregardless of the value of condition2. Therefore C will not evaluate condition2 if condition1 is false.

The same principle applies to ||:

	condition1 || conditon2

If condition1 is true, then the entire expression is also true, no matter what the value of condition2 is, so if condition1 is true, C will not bother to evaluate condition2.

Putting it together

OK, so now we know how to do conditional execution, and how to write conditions. Let's do something! Here's a simple program that uses these ideas:

#include	<stdio.h>

#include	<genlib.h>
#include	<simpio.h>

main ()
	int		input	= GetInteger ();

	if (input > 10) {
		printf ("%d is > than 10.\n", input);
	else {
		printf ("%d is <= to 10.\n", input);


Iteration (or looping) is a similar idea to conditional execution, except that instead of determining whether or not a statement should be executed, iteration provides a way to indicate that a statement should be executed a number of times- and control the number of times that it is executed.

The for Loop

The for statement has the following form:

for ( initialization ; condition ; increment ) body-stmnt

Some notes:

  • Some texts (and the lecture notes) use the name reinitialize instead of increment. The Roberts book uses step.

  • Each of the initialization, condition, and increment expressions are optional. Omitting them all (i.e. for (;;)) is a standard way to get an "infinite" loop (which can be handy, although you still need a way to get out, such as break).

The while Loop

The while statement has the following form:

while ( condition ) while-stmnt
The while statement is exactly equivalent to a for statement with no initialization or increment expressions, but while is still included in C because it helps to write programs that are easier to read.

The do-while Loop

The do statement has the following form:

do do-stmnt while ( condition ) ;
Note that the do statement requires a semicolon (;) after the closing parenthesis of the condition expression, unlike the other loop statements.

The break Statement

Another statement that you should be aware of is the break statement. One use of the break statement is to jump out of one level of for while or do loops.

Ordinarily, you should try to write programs in such a way that the loop condition is the only thing that will cause the program to stop looping, but sometimes this is inconvenient or makes the loop condition large and unwieldy.

For example, you might want to write a loop which reads integers and then prints them out, but stops right away (before printing the number) when the user types in a negative integer. This can be written in any number of ways, but using a break makes it very simple:

	for (;;) {
		int		number;

		number = GetInteger ();

		if (number < 0) {

		printf ("%d\n", number);
The continue Statement

The continue statement is relatively rare (unlike the break statement, which sees a lot of use), but you will encounter it from time to time- and when used properly, it can be quite useful.

When C executes a continue statement, it immediately jumps back to the "top" of the loop (i.e. to the increment expression of a for loop or the condition expression of a while loop.

Assignment 2


Please read the course packet notes on programming style. Adopting a good programming style is essential to effective programming. It is also essential for getting a good grade in this course, because we will deduct points from programs that are unclear or hard to read, even if they appear to be correct in all other aspects.

Fixing awful.c

  • Do not simply rewrite this program from scratch. Tempting though it might be, that's not the skill that this exercise is trying to teach.


    You may find the documentation on Makefiles that is accessible from my home page to be useful.

    Writing stripcom

    Exploring the WWW

    The WWW (which stands for the World Wide Web) or Web is a system that allows people to share information with each other. Later in the semester, we'll discuss what all of this means, and how it actually works, but for today we'll leave it at that.

    Web Browsers

    A Web browser is a program that helps you to fetch information from the web, and display it in a useful and pleasant way. There are lots of browsers available (and more keep popping up every day), but the most popular are Netscape, Mosaic, and lynx. Netscape has a number of nice features, and is probably the most popular browser for Macs and PCs. Mosaic is decent, and is free, so it still has a following. Lynx is useful because it runs quickly and works on ordinary terminals (it doesn't support any graphics), but aspects of the web are hard to access without graphics.


    As you have probably heard many times, the web is filled with interesting information of every type. However, before you can access any of that information, you need to have a handle on where the information resides. In the web, these handles are called URLs (or Universal Resource Locators). A URL specifies where something is on the web, what format the object is in (text, graphics, etc) and tells your browser how to get it.

    A URL consists of the following parts, arranged in the following form:


    The protocol to use to talk to the server. Different servers expect the browser to converse with it in different ways; this tells the browser what the server expects.


    The name of the computer that the server is on.

    Servers are often referred to as web sites.


    The "connection" in the computer that the browser should use to talk to the server. This part is usually left off, because it's usually not necessary- most servers use the default ports.


    The name of the file that your browser wants to fetch from the server. These are also referred to as web pages.

    In many cases, the name of the file actually represents the name of a program to run, instead of a real file. This gets complicated; please allow me to gloss over it for now.

    The suffix of the file tells your browser how it should display the file. (Note that many browsers make assumptions based on the suffix of the filename, not the contents of the file, so you need to use the same conventions as everyone else if you want to share stuff via the web!) For example, files that end in .txt are assumed to be text, and files that end in .gif are assumed to be graphical images.

    The file path can also contain an optional name, prefixed with a # character. This tells the browser to search for an anchor with the given name inside the file. (See the next section for a description of anchors.)

    For example, the following URL is for the index of the web pages that I've put together for my CS50 stuff (including this document):
    This URL specifies that the protocol is http, the server is www.fas.harvard.edu, the port number is 80, and the file to get from the server is ~ellard2/cs50-96/index.html- and since the filename ends in .html, the browser should display the contents of this file using whatever method is appropriate for an HTML file.


    HTML (which stands for Hypertext Markup Language is a document format that is used heavily in the web. You can think of it as a very simple programming language that is interpreted by a browser- it tells the browser what the document should look like, and how it should show it to you. As such, it is a pretty weak formatting language (practically any word processor is much, much more powerful), but it does have a redeeming property- it allows anchors to be placed in documents. Anchors (also called hyperlinks) are places in your document that either connect your document to another document via a URL, or can be part of a a URL that another document can use to find your document.

    For a more detailed explanation of HTML, consult the online help of your favorite browser.

    vi Tips and Tricks

    If you've been using vi then you should be familiar with the basic commands by now. There are tons of commands, however, so you're probably just scratching the surface (unless you've bought the vi book and memorized it already).

    I'll include a few tips in each handout. Here's this week's dose:

    UNIX Mysteries

    After the first assignment, you should be familiar with the basics of using UNIX. Although it may still seem awkward or unusual to you, the good news is that by the time you finished assignment 1, you had used almost all the UNIX tools that we're going to use all semester.

    Now that you've had a chance to poke around a little, here's some information that you may find interesting:

    Please bring any errors or inconsistencies in this document to the attention of the author.

    Dan Ellard <ellard@deas.harvard.edu>