Display next numbers in a sequence?

Joined
Nov 24, 2004
Messages
147
Reaction score
2
Someone advice on my algorithm. I'm having a hard time trying to make a simple number sequence work :(

int a = 1;
int b = 2 ;
int c = 3;
int output = 0;

//(if a is smaller than b, b is smaller than c)
if a < b < c then
{
s = s + 1
}
else
{
(this will happens if a is 3, b is 2, c = 1
s = s - 1
}

for (int i=0; a<d; s)
{
output = c+ (c-b) * i
Console.WriteLine(output)

// if a,b,c is 1,2,3, display 4,5,6
// if a,b,c is 3,2,1, display 0 -1 -2
}
 
Last edited:
Currently facing this issue, please advice whether i can simplify this

for (int i = 1; i < 4; i++)
if i == 1:
if (condition true )
Console.WriteLine("Hello1")
hello1 = 1

if i == 2:
if (condition true)
Console.WriteLine("Hello2")
hello2 = 2

if i == 3:
break

// nested loop
for (int j = 1; j < 4; i++)
if hello1 is true
//do computation here
// formula not shown

else if hello2 is true:
//do computation here
// formula not shown
Console.Write(output)

I want to display the following:

Hello1
1
2
3
Hello2
1
2
3


but its display the following:

Hello1
1
2
3
Hello2
1
2
3
1
2
3

If both the condition is true, i only have two strings to display. The output of the computation is three time which is an issue here
 
Try to wrap your code with ["CODE"] ["/CODE"](without quotes) the # button or ["PHP"]["/PHP"](without quotes) the php button in the editor, will make it look much better:
TakaShi^^ said:
PHP:
for (int i = 1; i < 4; i++)
   if i == 1:
      //if (condition true )
      Console.WriteLine("Hello1")
      hello1 = 1

   if i == 2:
      //if (condition true)
      Console.WriteLine("Hello2")
      hello2 = 2
      if i == 3:
         break

   // nested loop
   for (int j = 1; j < 4; i++)
      if hello1 is true
         //do computation here
         // formula not shown

       else if hello2 is true:
          //do computation here
          // formula not shown
      Console.Write(output)
I want to display the following:
Code:
Hello1
1
2
3
Hello2
1
2
3
but its display the following:
Code:
Hello1
1
2
3
Hello2
1
2
3
1
2
3
The problem is, it hard to tell since we don't know in what language was written, but I'm assuming you forgot to put brackets in your code(some languages use brackets, indentation, or a starting/closing statement)

PHP:
for (int i = 1; i < 4; i++)
{
   if i == 1:
   {
      //if (condition true )
      Console.WriteLine("Hello1")
      hello1 = 1
   }

   if i == 2:
   {
      //if (condition true)
      Console.WriteLine("Hello2")
      hello2 = 2
   }

   if i == 3:
      break

   // nested loop
   for (int j = 1; j < 4; i++)
   {
      if hello1 is true
      {
         //do computation here
         // formula not shown
      }
      else if hello2 is true:
      {
          //do computation here
          // formula not shown
      }
      Console.Write(output)
   }
}
 
Last edited:
Coded in Python

PHP:
message1 = True
message2 = True
for i in range(1,4):
    if message1:
        print("Message")
    if message2:
        print("Message1")


    for j in range(1,4):
        print(j)

Output with this codes:

Message
Message1
1
2
3
Message
Message1
1
2
3
Message
Message1
1
2

Expected output:

Message1
1
2
3

Message2
1
2
3
 
if message1 and message2 are true from the beginning when if checks for message1 or message2 will always be true(same as the if condition not being there, try to check i instead of message1 or message2, like the code from your previous post).
 
Coded in python:

Code:
for i in range(1,3):
	print('Message {0}'.format(i))
	for j in range(1,4):
		print(j)

Output:

Code:
Message 1
1
2
3
Message 2
1
2
3
 
Coded in python:

Code:
for i in range(1,3):
    print('Message {0}'.format(i))
    for j in range(1,4):
        print(j)

Output:

Code:
Message 1
1
2
3
Message 2
1
2
3

Thanks i got this working.. The second challenge is get the 2nd part to display different output

Code:
test = True
test2 = True
for i in range(1,3):
    print('Message {0}'.format(i))
    
    for j in range(1,4):
        if test:
            result = j + 1
         if test2:
            result = j ** 2
        print(result)

In the above code, its display 2 3 4 for two times which is incorrect..

Example:

Message 1:
// First Computation (Example: 0+1, 0+2, 0+3) --> Sorry # work as hash tag here, so i replaced it with //
1
2
3

Message
// Second Computation (Example: Factor of 2, 2^0, 2^1, 2^2)
1
2
4
 
Last edited:
Code:
def compute(p_type, p_constant, p_number):
	if p_type == 'sum':
		print p_constant + p_number
	elif p_type == 'power':
		print pow(p_constant, p_number)
	else:
		print p_number
	return;

for i in range(1,3):
	print('Message {0}'.format(i))
	for j in range(1,4):
		compute('sum', 0, j) if i==1 else compute('power', 2, j-1)

Output:
Code:
Message 1
1
2
3
Message 2
1
2
4

You have to use logic here, what you want is to display another kind of message for the second loop, so you have to control that, ex. if loop == 1 : doSomething else doOtherThing. And what you are doing is set two boolean variables initilized as true, so both times the if statement if going to return true to the test.
 
Last edited:
Code:
def compute(p_type, p_constant, p_number):
    if p_type == 'sum':
        print p_constant + p_number
    elif p_type == 'power':
        print pow(p_constant, p_number)
    else:
        print p_number
    return;

for i in range(1,3):
    print('Message {0}'.format(i))
    for j in range(1,4):
        compute('sum', 0, j) if i==1 else compute('power', 2, j-1)

Output:
Code:
Message 1
1
2
3
Message 2
1
2
4

You have to use logic here, what you want is to display another kind of message for the second loop, so you have to control that, ex. if loop == 1 : doSomething else doOtherThing. And what you are doing is set two boolean variables initilized as true, so both times the if statement if going to return true to the test.

Thanks for the output.. Fixed the power with ** and it works great. Any other ways to achieve this without a function?
 
Code:
for i in range(1,3):
	print('Message {0}'.format(i))
	for j in range(1,4):
		if i==1:
			print(j+1)
		else:
			print(j**2)

Output
Code:
Message 1
2
3
4
Message 2
1
4
9
 
Code:
for i in range(1,3):
    print('Message {0}'.format(i))
    for j in range(1,4):
        if i==1:
            print(j+1)
        else:
            print(j**2)

Output
Code:
Message 1
2
3
4
Message 2
1
4
9

Indeed this look promising. Thanks a lot. Does the if i == 1 means hard coded? Or it is the way we control statements to execute
 
Well, it has to be hardcoded, since you want one logic in the first loop and another logic for the second one.
 
Why do you want one logic in a loop and another one in second anyway? When you have to nest 3 levels deep there's a good chance you haven't properly figured out what it is you want to do. What are these sequences and why do you want to print them?

Let me assume ... uhm homework. ^^

(Funny is, the first posts code even uses a variable d in the for head that was never declared at all.)
 
  • Like
Reactions: Dec
Back