LEC02_notes

# LEC02_notes - 1 14:440:127 Introduction to Computers for...

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

1 14:440:127– Introduction to Computers for Engineers Notes for Lecture 02 Rutgers University, Spring 2011 Instructor - Brenda V. Cortez 1 Introduction In the previous lecture notes, you learned about variables, their data types, the di ff erent variety of operators and their functionality, and you also learned how to interact with the user. Those lecture notes also mentioned that when you receive data from the user, it is most likely because the user wants some information back, so in these lectures notes you will be introduce you to concepts you can use to do more than just print a message. An example of a program in which the user wants information back would be the previously men- tioned program of the student who wants to know the equivalent letter grade of his/her score. In order to accomplish this task, the program would have to be able to make a decision about the letter grade based on the range in which the student’s score falls. Now consider the case of a professor who wants to know how many students passed the class. He/she would have to be able to repeat (loop) the task of determining the student’s status, based on the student’s score, for however many students there are in the class. 2 Compound Statements & Escape Sequences Compound statements refer to a block of statements that are enclosed within { } . When the program is compiled, the compiler treats this block of statements as a single statement. The main function you have been working with so far, is an example of a compound statement , but you can also have compound statements within the main function. These embedded blocks of code are called nested statements and as you continue to program, you will have to use them in your code. Consider the following example illustrating a nested statement : #include <iostream> // don’t forget this if you are using cout or cin using namespace std; // always put this in your programs int main(){ int age = 21; if (age >= 21){ cout << ‘‘You can legally drink.\n’’; } else cout << ‘‘You shouldn’t be drinking.\n’’; return 0; }

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

View Full Document
2 NOTE: When printing strings to the screen, you can introduce a new line escape sequence, \n , as I did above, instead of << endl; . This will send your cursor to a new line, so that your cursor does not end up on the same line as the message to the user. Without a newline, the user will be forced to type right after the message, which can get messy, so don’t forget to include a new line after each statement. There are other escape sequences you can use as part of your strings, when needed. Following is a list of those which you may have to use in your programs: \n used to introduce a new line, such that your curser will be positioned on the line after your output \’ when you need to have single quote character \" double quotes character \\ use it if you need to have a backslash character Exercise 1: Write a program that has a three variables, each containing a character from the list above (skip \n ). Then print them to the screen as part of the sentence: I can use use ’, ”, or \ in my code.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern