7 Replies - 478 Views - Last Post: 28 April 2010 - 09:03 AM Rate Topic: -----

#1 Guest_joebob*


Reputation:

explain this code

Posted 27 April 2010 - 12:56 PM

How does this code work? and why does it print out what it does?

#include <iostream>

using namespace std;

int surprise(int x)
{
	int result = 0;

switch (x)
{
	case 0:
	case 1:
	result = x;
	break;

	case 2:
	case 3:
	case 4:
	result = x * x;
	break;

	default:
	break;
}

return (result);

}

int main()
{
	int i = 0;

do {
	cout << surprise(i) << endl;
	i++;
} while (i < 5);


}

Is This A Good Question/Topic? 0

Replies To: explain this code

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: explain this code

Posted 27 April 2010 - 12:57 PM

you wrote it, you tell us.


maybe you should Google on these topics:

function parameters
switch statement
case structure
while loops
functions


I bet if you do that then you could tell us yourself

This post has been edited by ImaSexy: 27 April 2010 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_joebob*


Reputation:

Re: explain this code

Posted 27 April 2010 - 12:58 PM

i actually came across it in a book
Was This Post Helpful? 0

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: explain this code

Posted 27 April 2010 - 01:01 PM

What exactly are you not familiar with in that code?
is it functions? a switch statement? do\while loop?
Was This Post Helpful? 0
  • +
  • -

#5 Guest_joebob*


Reputation:

Re: explain this code

Posted 27 April 2010 - 01:04 PM

the switch is confusing me because it has several cases that dont do anything. Im also finding it difficult to follow the flow/logic of the program if that makes sense.
Was This Post Helpful? 0

#6 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Re: explain this code

Posted 27 April 2010 - 01:07 PM

if a case doesn't has a break it goes to the next.
i.e.
case 0:
case 1:
case 2:
return x * x;
break
If my input is 0 then it will go past 0, 1 and do whats inside 2. In this case multiply x with x.

The do loop is run as long as x is under 5.

This post has been edited by FrozenSnake: 27 April 2010 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: explain this code

Posted 27 April 2010 - 01:09 PM

switch (x)
{
        case 0: //no break in this case
        case 1: //so if x is equal to 0 or 1
        result = x; // do this
        break; //break out of switch statment

        case 2: //no break
        case 3: //no break
        case 4: // if x is equal to 2,3, or 4 then 
        result = x * x;  //do this
        break; //break out of switch

        default: // if x doesnt equal any of these cases
        break; ///break out of switch
}


Was This Post Helpful? 0
  • +
  • -

#8 Guest_joebob*


Reputation:

Re: explain this code

Posted 28 April 2010 - 09:03 AM

Why does it print zero first? besides that does this basically cover what it does?

It will print out five integers, the first one being zero. It is a function that has parameters and it uses a switch statement in conjuncture with a do loop. In the switch statement there is no break after case ‘0’ (or case 2 and 3) so if x is equal to zero or one, result is equal to x, in this example i is initialized to 0 and after the loop has executed 1 is added to i. The for loop is continually executed while i < 5 (5 times).
Was This Post Helpful? 0

Page 1 of 1