473,467 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with if statement

65 New Member
whats wrong with this function:

Expand|Select|Wrap|Line Numbers
  1. int max(int x, int y) {
  2.     return (if (x > y) { return x; } else { return y; });
  3. }
i get an "expected an expression" error
Sep 25 '07 #1
5 1235
Meetee
931 Recognized Expert Moderator Contributor
Use this instead:

Expand|Select|Wrap|Line Numbers
  1. int max(int x, int y)
  2. {
  3.     if (x > y)
  4.         return x; 
  5.     else
  6.         return y;
  7. }
Regards
Sep 25 '07 #2
stmfc
65 New Member
Use this instead:

Expand|Select|Wrap|Line Numbers
  1. int max(int x, int y)
  2. {
  3.     if (x > y)
  4.         return x; 
  5.     else
  6.         return y;
  7. }
Regards
thanks for the reply.
i know that there are many alternative ways to perfom that functionality.
what i want to figure out is the rule which doesnt allow the above code to be considered as legal
Sep 25 '07 #3
Meetee
931 Recognized Expert Moderator Contributor
thanks for the reply.
i know that there are many alternative ways to perfom that functionality.
what i want to figure out is the rule which doesnt allow the above code to be considered as legal
Your function has return type int. In your previous code, you have taken return withing return like:

Expand|Select|Wrap|Line Numbers
  1. return(if(...) return x; else return y;) 
That is not proper way to return the value.
Sep 25 '07 #4
dmjpro
2,476 Top Contributor
whats wrong with this function:

Expand|Select|Wrap|Line Numbers
  1. int max(int x, int y) {
  2.     return (if (x > y) { return x; } else { return y; });
  3. }
i get an "expected an expression" error
Try it.
Expand|Select|Wrap|Line Numbers
  1. return x>y?x:y;
  2.  
Kind regards,
Dmjpro.
Sep 25 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
whats wrong with this function:


Code: ( text )
int max(int x, int y) {
return (if (x > y) { return x; } else { return y; });
}


i get an "expected an expression" error
The function has an int return type so you have to return an int. That means the code between the parentheses must evaluate to an int. It doesn't.

The key is in the "expected an expression" reported by the compiler. An expression must evaluate to true or false where false is 0 and true is not false.

if statements don't have a value.
Sep 25 '07 #6

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

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
4
by: Dani | last post by:
Hi everyone Description of the problem: Using a PreparedStatement to write down an integer (int) plus a timestamp for testing purposes. When read out again the integer looks very different. We...
3
by: Andy_Khosravi | last post by:
I have been trying to build a user friendly search engine for a small database I have created. I'm having some particular problems with one of my date fields. Here's the setup: I'm using...
1
by: tom.eeraerts | last post by:
Hello, I have a problem migrating an application from v5r2 to v5r3. The problem is with the prepared statements. To see what the problem is, i extracted a small piece of code and debugged the...
3
by: shark | last post by:
Hi all. i am facing a deadlock problem .i have included the -t1204 and -T3605 trace flags and have got the following o/p pu tin sqls server logs. 2006-06-01 17:49:21.84 spid4 2006-06-01...
2
by: mob1012 via DBMonster.com | last post by:
Hi All, I wrote last week about a trigger problem I was having. I want a trigger to produce a unique id to be used as a primary key for my table. I used the advice I received, but the trigger is...
8
by: Ian Mackenzie | last post by:
Hi Guys I am VERY new to DB2 and have created a workingdays function to return the working days between 2 dates, but I get some compiler errors when running it: CREATE FUNCTION WORKINGDAYS...
22
by: b_r | last post by:
Hi, I'm trying to make a simple operation (insert into DB) in VB 2005 and SQL Server. The code is as follows: Dim sConnectionString As String = _ "Data...
3
by: Tony K | last post by:
I'm note sure how to state this but, I have a Windows form that contains 2 tables. Details View on top and Datagrid on the bottom. The two tables are related. One of the fields in the details...
0
by: aashiss03 | last post by:
hi everyone I m restorimg a database , during which i m facing the peoblem Here is the details description the problem. RESTORE DATABASE NSWD FROM "C:\db2_backups" TAKEN AT...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.