473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

If..else in loop

Hey all,

I'm writing a function that takes a number and if it is even divides it by 2 n number of times and if it is odd times it by 3 and adds one n number of times.
I use and if..else obviously to see if it is even or odd but I can only make it do the calculation once, not n times.
Should I use a for loop for it and if yes put it before the if... else or do two, one after if and one after else?

Thanks
May 1 '07 #1
7 1938
JosAH
11,448 Expert 8TB
Hey all,

I'm writing a function that takes a number and if it is even divides it by 2 n number of times and if it is odd times it by 3 and adds one n number of times.
I use and if..else obviously to see if it is even or odd but I can only make it do the calculation once, not n times.
Should I use a for loop for it and if yes put it before the if... else or do two, one after if and one after else?

Thanks
I guess you want to stop the loop when the numer equals one. A while loop
would do fine here:
Expand|Select|Wrap|Line Numbers
  1. while (number != 1) {
  2.    // your code here dealing with even and odd numbers
  3. }
kind regards,

Jos
May 1 '07 #2
I guess you want to stop the loop when the numer equals one. A while loop
would do fine here:
Expand|Select|Wrap|Line Numbers
  1. while (number != 1) {
  2.    // your code here dealing with even and odd numbers
  3. }
kind regards,

Jos
That doesn't work because the number doesn't actually get updated so its always bigger then 1 and the compilier just keeps going.
I hope that makes sense, so the number of times it has to be done has nothing to do with the number being changed.
May 1 '07 #3
JosAH
11,448 Expert 8TB
That doesn't work because the number doesn't actually get updated so its always bigger then 1 and the compilier just keeps going.
I hope that makes sense, so the number of times it has to be done has nothing to do with the number being changed.
Then I must've misunderstood your question. From what I read you wanted to
find the series (say, starting at 10) 10, 5, 16, 8, 4, 2, 1 ...

kind regards,

Jos
May 1 '07 #4
Sorry, I'll try and explain it better...
Two numbers are input ( a month and a year)
the year is taken and if it is even... year = year/2
if it is odd... year = year*3+1
this happens month times
so if the month was 3 year would be updated 3 times..
Does that make any sense???
May 1 '07 #5
JosAH
11,448 Expert 8TB
Sorry, I'll try and explain it better...
Two numbers are input ( a month and a year)
the year is taken and if it is even... year = year/2
if it is odd... year = year*3+1
this happens month times
so if the month was 3 year would be updated 3 times..
Does that make any sense???
Yep, you *do* want to evaluate that series but you only want to do it 'month'
times. Have a look at this:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 1; i <= month; i++) {
  2.    // change the 'year' value here for even/odd values
  3. }
kind regards,

Jos
May 1 '07 #6
Thanks for that, it works. I had tried it before but had a value in the wrong place.
It is amazing what one tiny little thing wrong can do!!
Thanks again
May 1 '07 #7
JosAH
11,448 Expert 8TB
Thanks for that, it works. I had tried it before but had a value in the wrong place.
It is amazing what one tiny little thing wrong can do!!
Thanks again
You're welcome of course and yes, C and C++ are extremely terse languages,
i.e. every semicolon, operator and bracket counts. There is not much syntactical
redundancy nor syntactic sugar in those languages. Actual programming is an
extremely secure activity so one better gets his/her design right first before
one has to start that tedious typing ;-)

kind regards,

Jos
May 1 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
5
by: WindAndWaves | last post by:
Hi Team The function below searches all the tables in a database. However, if subsearch = true then it searches all the objects listed in a recordset (which are all table names). I thought to...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
25
by: metaperl.etc | last post by:
A very old thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e discusses the optional "else:"...
7
by: Ixiaus | last post by:
I have just come across a site that discusses Python's 'for' and 'while' loops as having an (optional) 'else' structure. At first glance I interpreted it as being a bit like the 'default'...
23
by: bearophileHUGS | last post by:
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.