Skip to content

If Statements

In C/C++, the if statement is used in the same way it is in C#: to branch between two instruction paths based on a condition.

Conditions here are formed as boolean expressions, as they were in C#. C/C++ also uses the same comparisons and boolean operators as C#.

KindDescriptionOperator
EqualAre the values the same?a == b
Not EqualAre the values different?a != b
Larger ThanIs the left value larger than the right?a > b
Less ThanIs the left value smaller than the right?a < b
Larger Or EqualIs the left value larger than or equal to the right?a >= b
Less Or EqualIs the left value smaller than or equal to the right?a <= b
DescriptionOperatorExample
AndAre both values true?&&a && b
OrIs at least one value true?||a || b
XorIs one value true, and the other false?^a ^ b
NotIs the value false?!! a

Example

Here is the same example we worked through on the if statement page. To get this working, we need to use the SplashKit library to get access to read_line and write_line.

#include "splashkit.h"
int main()
{
string language;
write("What language do you use? ");
language = read_line();
if (language == "C#")
{
write_line("Good choice, C# is a fine language.");
}
else if (language == "C" || language == "C++")
{
write_line("These are great low level languages - we will be using these soon!");
}
else
{
write_line("Well... good luck with that!");
}
write_line("Great chat!");
}