Beginners help: if statements

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Beginners help: if statements

1. Registered User
Join Date
Apr 2004
Posts
136

## Beginners help

Ive been practicing some C++ lately and I cant seem to see why this dont work....I believe theres maybe some ; missing in the if statement ?
As u can tell from the code im a total newby to this...so any help is appreciated =)

Code:
```int Main()

{
int A,B;

cout << "Enter two numbers \n";
cin >> A;
cin >> B;

if A > B
cout << "Your first number was bigger \n"
else
cout << "\n Your second number was bigger \n"

return 0;

}```
Last edited by process; 06-20-2004 at 04:24 PM.

2. ## Re: Beginners help

Originally posted by process
Ive been practicing some C++ lately and I cant seem to see why this dont work....I believe theres maybe some ; missing in the if statement ?
As u can tell from the code im a total newby to this...so any help is appreciated =)

Code:
```int Main()

{
int A,B;

cout << "Enter two numbers \n";
cin >> A;
cin >> B;

if A > B
cout << "Your first number was bigger \n"
else
cout << "\n Your second number was bigger \n"

return 0;

}```
make sure you put the ; and the end of your expessions. and also the if should be like this
Code:
```int main()
{
int A, B;

cout << "Enter two numbers: ";
cin >> A >> B;

if( A > B )
cout << "Your first number is bigger." << endl;
else
cout << "Your second number is bigger." << endl;

return 0;
}```
hope this helps
Last edited by ben; 06-20-2004 at 06:27 PM.

3. Registered User
Join Date
Jun 2004
Location
Posts
27
make sure you understand that the if statement syntax for c++ is:

if (conditional) // either true or false only
{ // execute statements if conditional was true
statements ; // always end a statement with a semicolon
} // end of block for if statements

4. Registered User
Join Date
Apr 2004
Posts
136
so also the if statements are contained within a block like

Code:
```if( A > B )
{
cout << "Your first number is bigger." << endl;
else
cout << "Your second number is bigger." << endl;
}
return 0;
}```
:confused:

5. Originally posted by process

so also the if statements are contained within a block like

//..... I assume some code is here that starts a function....
if( A > B )
{
cout << "Your first number is bigger." << endl;

} // Closing the true part of the if....
else
{ // starting new block for else (false part of if)....
cout << "Your second number is bigger." << endl;
}
return 0;
}

Because the blocks for this if....else statement are one line, you could also have done:

if( A > B )
cout << "Your first number is bigger." << endl;
else
cout << "Your second number is bigger." << endl;

return 0;
}

Note that the above doesn't show indenting

6. Registered User
Join Date
Apr 2004
Posts
136
thx for all ur help chaps...seems more clear now :cool:

7. Registered User
Join Date
May 2004
Posts
12
What is the "endl" after each statement? I thought the semi-colon was enough to end a statment. But you guys put << endl;??? what's this for? :confused:

8. Registered User
Join Date
Apr 2004
Posts
136
signals a new line

endl
endline

9. Registered User
Join Date
May 2004
Posts
12
What's wrong with using \n for signalling a new line? Is "endl" just an alternative?

What is the "endl" after each statement? I thought the semi-colon was enough to end a statment. But you guys put << endl;??? what's this for? :confused:
If you want to have the user input data right after the output as in cout << "please enter your name"; <- leaves cursor on the current line;

Doing it this way cout << "Please enter your name " << endl;
moves the cursor to a new line below "Please enter your name".

I thought I read somewhere that endl clears the buffer where in "\n" does not. Feel free to correct me if im wrong.

11. endl does two things: i flushes the stream, which means forcing output to the screen without delay, and it moves it to the next line.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise