CSE100Lab - Lab Letter: _ Name: _ Lab 1 Algorithms, the...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Letter: _____ Name: _________________________________ Lab 1 Algorithms, the cout Statement, and Building Your First Program Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. In this lab, you will learn: how to write an algorithm for a program how to use comments in C++ code how to create a "hello world" program Algorithm: Before you begin writing a program, you should "sketch out" the structure and flow of your code. This helps you to avoid nasty surprises ("Oh shoot, I forgot to get user input!"), and it gives your code a more structured appearance. The best way to plan out your code is to break it up into smaller pieces, separating large processes into distinct steps. Then, using comments (explained below), type those steps into your code using language similar to how your code might work. This is called writing an algorithm . Here is an example of an algorithm for a simple "hello world" program: (don't worry about the slashes, they'll be explained later) // declare necessary includes for the program // the main function header // print "hello world" to the screen Here's what the program might look like after adding C++ code: (again, don't worry if you don't understand the code, we'll go over it in a bit) // declare necessary includes for the program #include <iostream> using namespace std; // the main function header int main() { // print "hello world" to the screen cout << "Hello World!" << endl; return 0; } As you can see, the algorithm matches up with the real code needed to complete the program, and it helps explain what each part of the program is doing. This is called commenting your code. Commenting your code helps you remember what the different parts of your program do, and it helps others (like your TA :-) ) understand what your code does (or is trying to do). It is always a good idea to comment your code, and pseudocoding is a great way to do it. CSE 100 Lab 1 1 /5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Commenting: Comments are parts of your code that C++ ignores. They allow you to insert human-readable notes into your code so that you (and others) can better understand what's going on. In C++, anything that follows two forward slashes ("//", without the quotes) on the same line will be ignored. In this example: cout << "It was nice to meet you," << endl; // you serpent-tongued charlatan
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/20/2010 for the course CSE 100 taught by Professor Na during the Spring '09 term at University of Arizona- Tucson.

Page1 / 5

CSE100Lab - Lab Letter: _ Name: _ Lab 1 Algorithms, the...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online