CSE100Lab04 - Lab Section: _ Name: _ Lab 4 Decision...

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

View Full Document Right Arrow Icon
Lab Section: _____ Name: _________________________________ Lab 4 Decision Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. In this lab session, you will learn: How and when to put decision structures in code How to include if, if/else, and switch statements in pseudocode What the syntax is for an if , if/else , and switch statements. What logic evaluations are As we've mentioned in lecture, sometimes we want something done only under certain conditions. Sometimes we only care if something is or is not the case ("Do I have puppies?"), and other times we have multiple options to choose from ("How many puppies do I have?"). Today we will learn how to code these condition statements. Decision or condition statements can be broken into three types. The first type is when we want statements to be done under certain conditions but if those conditions aren't met we don't want anything done at all. This is called a single alternative condition or an if statement. The second type is when we want things to be done under certain conditions but when those conditions aren't met we want a different set of statements done. This is called an if/else statement. The third type is when we want more than two alternatives of code to be available. There are two different ways this can be handled with either chained if/else statements or with a switch statement. Let's look at the if statement in detail. We already covered how to write an if statement in an algorithm. The syntax for the if statement is if ( some Boolean expression ) { some statements } Here are some things to note about the syntax. A Boolean expression is something that when evaluated is either true or false. The Boolean expression is in parentheses () and there is no semicolon after the parentheses. When there are several lines of code to be executed, they are grouped using brackets {}. When there is only a single, simple statement for the if, then the brackets are not required. In other words, if you forget the brackets, the if will only consider the first statement after it to be attached to it and all other statements will be done. Lets look at the difference between the following two sets of code. What do you think the output of these two pieces of code will be? CSE 100 - Lab 4 1/10
Background image of page 1

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

View Full DocumentRight Arrow Icon
int a = 10; if (a > 11) cout << "hi"; cout << "there"; cout << “!”; int a = 10; if (a > 11) { cout << "hi"; cout << " there"; } cout << “!”; Did you get these? there! ! Since there were no brackets around the first if statement, the computer evaluated a > 11 to be false and then skipped over, did not do, the first cout statement. Since there were no brackets connected with the first if statement, the rest of the statements were done afterwards. Since there were brackets connected with the second if statement, when the computer evaluated a > 11 to be false, it skipped over everything inside the brackets. A little aside here before we start into if/else statements. Keeping track of brackets can be tricky and
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.

Page1 / 10

CSE100Lab04 - Lab Section: _ Name: _ Lab 4 Decision...

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