Tag c++ string

C++ While Loop

C++ Loops Loops can execute a block of code as long as a specified condition is reached. Loops are handy because they save time, reduce errors, and they make code more readable.

C++ Switch

C++ Switch Statements Use the switch statement to select one of many code blocks to be executed. Syntax switch(expression) {   case x:     // code block     break;   case y:     // code block     break;   default:…

C++ If … Else Examples

Real Life Example This example shows how you can use if..else to “open a door” if the user enters the correct code : Example int doorCode = 1337; if (doorCode == 1337) {   cout << “Correct code.\nThe door is…

C++ Short Hand If Else

Short Hand If…Else (Ternary Operator) There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line, and…

C++ Else If

The else if Statement Use the else if statement to specify a new condition if the first condition is false. Syntax if (condition1) {   // block of code to be executed if condition1 is true } else if (condition2)…

C++ Else

The else Statement Use the else statement to specify a block of code to be executed if the condition is false. Syntax if (condition) {   // block of code to be executed if the condition is true } else…

C++ If … Else

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:…

C++ Boolean Examples

Real Life Example Let’s think of a “real life example” where we need to find out if a person is old enough to vote. In the example below, we use the >= comparison operator to find out if the age…

C++ Boolean Expressions

Boolean Expression A Boolean expression returns a boolean value, which is either 1 (true) or 0 (false). This is useful for building logic and finding answers. You can use a comparison operator, such as the greater than (>) operator, to…