Unit 05 - Unit05 ControlStructures UnitObjectives

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

View Full Document Right Arrow Icon
Unit 05 Control Structures Unit Objectives   Learn C++ Selection Control Structures (if, else, switch) Learn C++ Iteration Control Structures (for, while, do while)   Introduction I think you will find this unit to be more fun than the last one. Have you noticed that all of the program  commands we have written so far were processed in  sequential order ? I.e. the commands are executed  in the order we wrote them, down the page. Sure it might skip a few lines (i.e. an IF statement that is  false) but executed statements are always organized top to bottom. So lets say you want to perform the same calculation 10 times. Do you cut and paste that command 10  times? What about a 100? What about a 1,000? Nope. What we need is a way to jump around our  program so we can perform the same calculation a bunch of times  without  all that copying and pasting.  That is what this section is all about, and the C++ commands that directs the compiler to run a certain line  of code multiple times (or not at all) are called  Control Structures. This unit will cover just a few topics, but we will do a lot of examples. I strongly recommend you try each  and every one. Understanding control structures is fundamental to good programming. There is not a lot  of material here, but you need to know it better than anything you have seen so far. Good luck! Task 1 - if, else, and else if We have already been using IF  so we could do some more interesting programs early on, but here we  cover the functionality in more detail. Below is the IF control structure in its simplest form. if( test ) { // these command statements are executed if the // test is true and skips them if it is false
Background image of page 1

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

View Full DocumentRight Arrow Icon
command statement; command statement; } // whether the test is true or false, statements // outside the parentheses will get executed. more command statements; Compile the code below. Run it two times where you swap the variable values by moving the comment  slashes. int main(void) { int iA = 1, iB = 2; //int iA = 2, iB = 1; if( iA < iB ) { cout << "The test was true!" << endl; } cout << "You should always see me!" << endl; return 1; } Simple enough right? Even simpler since you have already seen it. Sometimes you will want to do one  thing if the statement is true and something  else  if the test is false. If so, you can use the following control  structure. if( test ) { command statement(s); } else { command statement(s); } more command statements;
Background image of page 2
Notice again that control structures do not generally need semicolons - only command statements do.  Here is an example that builds on the one we had above. int main(void)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/02/2011 for the course ME 205 taught by Professor Koen during the Spring '07 term at University of Texas at Austin.

Page1 / 17

Unit 05 - Unit05 ControlStructures UnitObjectives

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

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