0% found this document useful (0 votes)
5 views4 pages

C++ If ... Else

The document provides an overview of conditional statements in C++, including the use of 'if', 'else', 'else if', and 'switch'. It explains how to use these statements to execute different blocks of code based on logical conditions. Examples are provided to illustrate the syntax and functionality of the 'if' statement.

Uploaded by

Elsa Olivia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

C++ If ... Else

The document provides an overview of conditional statements in C++, including the use of 'if', 'else', 'else if', and 'switch'. It explains how to use these statements to execute different blocks of code based on logical conditions. Examples are provided to illustrate the syntax and functionality of the 'if' statement.

Uploaded by

Elsa Olivia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

 Tutorials  Exercises  Get Certified  Services  Search...

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

C++ If ... Else


❮ Previous Next ❯

C++ Conditions and If Statements


You already know that C++ supports the usual logical conditions from mathematics:

• Less than: a < b


• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C++ has the following conditional statements:

• Use if to specify a block of code to be executed, if a specified condition is true


• Use else to specify a block of code to be executed, if the same condition is
false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed

The if Statement
Use the if statement to specify a block of C++ code to be executed if a condition is
true .
 Tutorials  Exercises  Get Certified  Services 

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Syntax

if (condition) {
// block of code to be executed if the condition is true
}

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.

In the example below, we test two values to find out if 20 is greater than 18. If the
condition is true , print some text:

Example

if (20 > 18) {


cout << "20 is greater than 18";
}

Try it Yourself »

We can also test variables:


 Tutorials  Exercises  Get Certified  Services 


Example
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}

Try it Yourself »

Example explained

In the example above we use two variables, x and y, to test whether x is greater than
y (using the > operator). As x is 20, and y is 18, and we know that 20 is greater than
18, we print to the screen that "x is greater than y".

C++ Exercises

Test Yourself With Exercises

Exercise:
Print "Hello World" if x is greater than y .
 intTutorials
x = 50;
 Exercises  Get Certified  Services 

HTML
 intCSSy = 10;
JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
(x y) {
cout << "Hello World";
}

Submit Answer »

Start the Exercise

❮ Previous Log in to track progress Next ❯

COLOR PICKER

You might also like