If/else

Hey there

I'm new to this forum

is anyone able to explain the brackets in the if/else statements to me?

my statements are all being printed no matter what the guard is.

Cheers
can you put your code to know what is your problem?
When the if statement is true, the next line will be printed

eg:
1
2
3
if (true)
    cout << "The statement is true" << endl;
cout << "This sentence will be displayed even if the statement is false" << endl;


when 2 or more commands need to be executed when an if statement is true, you use the brackets {}

eg:
1
2
3
4
5
6
if (true)
{
    cout << "The statement is true" << endl;
    cout << "This sentence will only be displayed if the statement is true" << endl;
    //other commands that will be executed when the if statement is true
}


if/else can be used as follows:
1
2
3
4
5
6
if (number == 1)
    //do this
else if (number == 2)
    //do that
else
    //if the number is not 1 nor 2, do this 
Topic archived. No new replies allowed.