Enter your Sign on user name and password.

Forgot password?
• Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to C++

• ## Related Services

Lecture Comments (3)
 0 answersPost by Donald Sullenger on October 7, 2015I'm having a heck of a time with homework number 6 (the computer guessing problem).  I'm sure I'm missing something simple but I'm just not coming up with it.  My code works through the second guess (whether high or low) but, after that, it just repeatedly picks the same number (eg., 100/2 = 50; 50/2 = 25; then it keeps picking 25.  This is because the variables have a set formula but I don't see how to get the computer to take the result of the second guess and apply the formula to that result.  Where am I going wrong here?#include "stdafx.h"#include using namespace std;int main(){int max = 100;int min = 1;int firstGuess = max / 2;int highGuess = ((firstGuess + 1) / 2);int lowGuess = ((max + 50) / 2);char correct = 'y' || 'n';int counter = 1;   cout << "I know your number is between 1 and 100.  I'll guess it!" ;   cout << "Is your number " << firstGuess << "?  (Enter 'y' or 'n')  " ;   cin >> correct;// if first guess was correct, display message and end program    if (correct == 'y') {         cout << "I told you I was good!" ; }// if first guess was wrong, begin binary search for correct number    else {        do { cout << "Was I too high or too low?  (Enter 'h' or 'l')  " ; char answer = 'h' || 'l'; cin >> answer; // if firstGuess was too high, take product of firstGuess and // divide in half. if (answer == 'h') {       cout << "Is your number " << highGuess << "?  " ;    cin >> correct; } // if firstGuess was too low, add range maximum and midpoint // then divide by two else if (answer == 'l') {     cout << "Is your number " << lowGuess << "?  " ;    cin >> correct; }   } while (correct == 'n') ; while (correct == 'n') {  cout << "That is " << counter << " incorrect answer so far." ;  counter++; } }    return 0;} 1 answerLast reply by: Alvin SylvainSun May 5, 2013 7:23 PMPost by yufeng wang on May 2, 2013at 7:31, why do we have to define int guess =0?i thought it's enough to define guess, but not to give it a value 0?

### Looping

• Loops execute a code block more than once, as many times as needed
• Early computing used “goto” – modern computing uses various explicit looping constructs
• “while” loop, tests an expression for “true” before each loop – code block may be skipped completely if expression starts off “false”
• “do-while” loop, tests an expression for “true” after each loop – code block is guaranteed to run at least once
• “for” loop, usually has an explicit loop counter – code block is looped a very specific number of times
• “break” leaves a loop early
• “continue” returns to the beginning of a loop
• Nested loops operate on each sub-item of each item
• Nested loops should use different counters and different termination expressions, otherwise subtle bugs may be introduced
• Here is a link that discusses various kinds of C++ loops: http://www.cplusplus.com/doc/tutorial/control/

### Looping

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

• Intro 0:00
• Overview 0:08
• Important Kind of Branch 1:17
• Branch That Loops 1:43
• In The Beginning 3:51
• 'While' Loop - Pre-Test 4:45
• 'Do-While' Loop - Post-Test 6:47
• 'Do-While' Run It Again 7:31
• 'For' Loop-Counting Loops 9:38
• 'For' Loop-Most Typical Use 11:46
• Useful Feature
• 'For' Loop - Similar to 'While' 13:47
• Example
• Equivalent 'While' Loop
• 'Break' Out of Loop 15:50
• 'Break' Examples 17:07
• 'Continue' Back to Beginning 18:49
• 'For' Loop
• 'Continue' Caution 20:05
• Infinite Loop
• Loop Inside of Loops 21:44
• Typical Bug 22:50
• More Looped Loops 24:08
• What a Loop! 27:46