Solved

# c++ nested if

Posted on 2009-05-02
Medium Priority
392 Views
Hi , i am trying to create a nested if in c++ , i can't get the logic right ,code here
``````if (position_six_A !='15')
{
position_six = position_six+1;

}

else if (position_six_A = '15')
{
position_six=0;
}
else if (position_five_A !='J')
{
position_five = position_five - 1;
position_six = position_six + 1;
}
else if (position_five_A ='J')
{
position_five=32;
}
else if (position_four_A !='15')
{
position_four = position_four + 1;
position_five = position_five - 1;
position_six = position_six + 1;
}
else if (position_four_A ='15')
{
position_four = 0;
}
else if (position_three_A != 'J')
{
position_three = position_three - 1;
position_four = position_four + 1;
position_five = position_five - 1;
position_six = position_six + 1;
}
else if (position_three_A ='J')
{
position_three=32;
}
``````
0
Question by:skywaker
• 4
• 4
• 2
• +3

LVL 53

Expert Comment

ID: 24285935
1) There are no loops in the code you posted. For help on loops, I suggest reading this tutorial :

http://cplusplus.com/doc/tutorial/control/

(consider also reading the rest of that tutorial, to get a better understanding of programming in C++)

2) '15' is not a valid character literal, as it contains two characters.

3) = is not the same as ==. The first (=) is the assignment operator, while the second (==) is a comparison operator.

Other than that, I'm not sure what you're trying to do.
0

Author Comment

ID: 24285957
hi , sorry , it is not a loop but a nested if
0

Author Comment

ID: 24285987
hi the 15 should be 5
0

LVL 53

Expert Comment

ID: 24285990
>> hi , sorry , it is not a loop but a nested if

Yes, I see you edited the question ;)

The same tutorial I posted earlier also explains if statements.  I still recommend reading it.

And, I'm still not sure what you're trying to do, or what your question is.
0

Author Comment

ID: 24286048
I am trying to test each of the conditions. Ie: if condition 1 is true it must test the next condition

if position_six_A  !='5'
then position_six=position_six+1

then it must check
position_five

if position_five_A !="J"
position_five=position_five-1

i want to test each condition.
0

LVL 53

Expert Comment

ID: 24286056
So, something like this ?
``````if (condition1) {
if (condition2) {
/* ... */
}
else {
/* ... */
}
}
else {
/* ... */
}
``````
0

Author Comment

ID: 24286165
hi , does not work  below is the vb code of what  want t o do
``````If position_six_A <> "5" Then
position_six = position_six + 1
ElseIf position_six_A = "5" Then
position_six = 0
Else
If position_five_A <> "E" Then
position_five = position_five - 1
position_six = position_six + 1
ElseIf position_five_A = "E" Then
position_five = 32
Else
If position_four_A <> "5" Then
position_four = position_four + 1
position_five = position_five - 1
position_six = position_six + 1
ElseIf position_four_A = "5" Then
position_four = 0
Else
If position_three_A <> "E" Then
position_three = position_three - 1
position_four = position_four + 1
position_five = position_five - 1
position_six = position_six + 1
ElseIf position_three_A = "E" Then
position_three = 32
Else
If position_two_A <> "5" Then
position_two = position_two + 1
position_three = position_three - 1
position_four = position_four + 1
position_five = position_five - 1
position_six = position_six + 1
ElseIf position_two_A = "5" Then
position_two = 0
Else
If position_one <> "5" Then
position_one = position_one + 1
position_two = position_two + 1
position_three = position_three - 1
position_four = position_four + 1
position_five = position_five - 1
position_six = position_six + 1
ElseIf position_one <> "0" Then
position_one = 0
end If
End If
End If
End If
End If
End If
``````
0

LVL 53

Expert Comment

ID: 24286245
What does not work ?
0

LVL 40

Expert Comment

ID: 24286720
Unless I am very much mistaken (I could be, I don't know VB), your VB code doesn't contain any nested ifs, it is just a whole bunch of if/else statements one after the other and it's just the way it's formatted that make it look nested. I thinks just the way that it has been formatted that is making it look like they are nested and, therefore, confusing you.

0

LVL 1

Expert Comment

ID: 24286822
give more detail so that we can understand your problem clearly
the code you post so confused, plz describe via natural language :D
0

LVL 1

Expert Comment

ID: 24286853
and the VB code you've post, I thnk it wrong(???)
0

LVL 1

Expert Comment

ID: 24289797
if you look at your VB logic it fails...
If position_six_A <> "5" Then
position_six = position_six + 1
ElseIf position_six_A = "5" Then.................this is implied if initial if failed
position_six = 0
Else ....... ??????
.... never can get here
Endif
0

Accepted Solution

emilmgeorge earned 750 total points
ID: 24290724
I think this is what you are looking for...
C++ Code:

if(position_six_A !='5')
{

position_six++;

if(position_five_A !='J')
{
position_five--;
position_six++;

if(position_four_A !='5')
{
position_four++;
position_five--;
position_six++;

if(position_three_A != 'J')
{
position_three--;
position_four++;
position_five--;
position_six++;

if(position_two_A != '5')
{
position_two++;
position_three--;
position_four++;
position_five--;
position_six++;

if(position_one_A != 'J')
{
position_one--;
position_two++;
position_three--;
position_four++;
position_five--;
position_six++;

}
else
{
position_one=32;
}

}
else
{
position_two=0;
}

}
else
{
position_three=32;
}

}
else
{
position_four = 0;
}
}
else
{
position_five=32;
}
}
else
{
position_six=0;
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month9 days, 11 hours left to enroll

#### 612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.