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
7 1938
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: -
while (number != 1) {
-
// your code here dealing with even and odd numbers
-
}
kind regards,
Jos
I guess you want to stop the loop when the numer equals one. A while loop
would do fine here: -
while (number != 1) {
-
// your code here dealing with even and odd numbers
-
}
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.
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
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???
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: -
for (int i= 1; i <= month; i++) {
-
// change the 'year' value here for even/odd values
-
}
kind regards,
Jos
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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:...
|
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 -...
|
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...
|
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...
|
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...
|
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:"...
|
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'...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |