8 Replies - 665 Views - Last Post: 19 August 2019 - 04:01 AM Rate Topic: -----

#1 beginnerC++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 19

if, and If-else statements

Posted 14 August 2019 - 06:29 AM

#include <iostream>
 using namespace std;
 int main( )
{
    int number,score;
    double result;
    cout<<"What was the score"<<endl;
    cin>>score;

    if((score > 0)&& (score <10));
            cout<<"score is between 0 and 10"<<endl;
    else

    cout <<"score is not between 0 and 10 "<<endl;
return 0;

}





**** I am just learning C++ on my own, this is not homework. I am trying to see what is wrong with this code because it does not run. It says it has one error. I am just a beginner. Thanks for your feedback**

This post has been edited by no2pencil: 14 August 2019 - 06:30 AM
Reason for edit:: ** Added Code Tags **


Is This A Good Question/Topic? 0
  • +

Replies To: if, and If-else statements

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6816
  • View blog
  • Posts: 31,414
  • Joined: 10-May 07

Re: if, and If-else statements

Posted 14 August 2019 - 06:31 AM

View PostbeginnerC++, on 14 August 2019 - 09:29 AM, said:

this code ... does not run. It says it has one error.

What error are you getting?

Line 10 you end your if statement with a semi-colon. You want to have an open bracket here to encapsulate the if conditional. White space can be used like you did on line 12. But it's cleaner & easier to follow, imo to use brackets.

    if((score > 0)&& (score <10)) {
            cout<<"score is between 0 and 10"<<endl;
    }
    else {
      cout <<"score is not between 0 and 10 "<<endl;
    }


Was This Post Helpful? 0
  • +
  • -

#3 beginnerC++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 19

Re: if, and If-else statements

Posted 14 August 2019 - 06:40 AM

no2pencil

Thank you so much for helping me. YEP the brackets fixed it all. one more question, when if , if else statements i ALWAYS have to end with a bracket instead of a semicolon?

** sorry if i dont reply in the right box, i am still trying to figure out how the website works*
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5745
  • View blog
  • Posts: 17,601
  • Joined: 25-December 09

Re: if, and If-else statements

Posted 14 August 2019 - 08:48 AM

Normally if()/else statements don't end in a semicolon. If a semicolon is present it means that the body of the statement is blank (the same as if(somecondition){}). This means that having semicolon after an if() statement is usually a bug.

Likewise using braces is not mandentory, just highly recommended since without brackets one and only one line that follows would be part of the body of the if() statement.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 276
  • Joined: 25-May 16

Re: if, and If-else statements

Posted 14 August 2019 - 12:40 PM

View PostbeginnerC++, on 14 August 2019 - 06:40 AM, said:

no2pencil

Thank you so much for helping me. YEP the brackets fixed it all. one more question, when if , if else statements i ALWAYS have to end with a bracket instead of a semicolon?

** sorry if i dont reply in the right box, i am still trying to figure out how the website works*

Whenever you are using if, if-else, else, statements, the syntax is as follows

if(SOMETHING HERE)
{
  // do something here
}
else if(SOMETHING ELSE HERE)
{
  // do something else there
}
else
{
  // do another thing here if the conditions don't meet above
}



If it's just one-liners, you can do this but it can be confusing to read at times

if(SOMETHING)
// do this
else if(SOMETHING ELSE)
// do this instead
else
// do this instead of the others


Was This Post Helpful? 1
  • +
  • -

#6 beginnerC++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 19

Re: if, and If-else statements

Posted 17 August 2019 - 09:17 PM

@fearfulsc2:

THANK YOU SO MUCH

THANK YOU SO MUCH

@jimblumberg:

THANKS A LOT TO EVERYONE

THANKS A LOT. I APPRECIATE IT

This post has been edited by Skydiver: 18 August 2019 - 08:32 AM
Reason for edit:: Removed unnecessary quote.

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7020
  • View blog
  • Posts: 23,840
  • Joined: 05-May 12

Re: if, and If-else statements

Posted 18 August 2019 - 08:31 AM

In the future, there is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area. If you want to address a specific item, use a quote and trim it down to the specific thing. If you just want to acknowledge somebody, just use the Twitter convention of @username, or BBCode: [member = username] (remove the extra spaces around the '='. I just put them there to keep the forum software for actually parsing the BBCode.
Was This Post Helpful? 0
  • +
  • -

#8 beginnerC++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 19

Re: if, and If-else statements

Posted 18 August 2019 - 07:59 PM

@skydriver thank you. when posting my code how to I put tags around my code? it seems so much cleaner and easy to follow but i dont find the option.

@skydiver
thank you. when posting my code how to I put tags around my code? it seems so much cleaner and easy to follow but i dont find the option.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7020
  • View blog
  • Posts: 23,840
  • Joined: 05-May 12

Re: if, and If-else statements

Posted 19 August 2019 - 04:01 AM

Look at the watermark of the Fast Reply area. Type things in that way is one option.

The other way is to type/paste in all your code, select it, then press the [ CODE ] button on to toolbar. This will surround your code with code tags.

And yet another way is to first press the [ CODE ] button, then type/paste in your code between the tags.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1