Lesson 3

Welcome back, coding explorer! Today, we're taking a new turn — mastering **Input/Output**, arithmetic expressions, and variable applications in `C++`

. These skills are stepping stones to creating programs that interact with users. Brace yourself for our journey!

- First, we'll explore the lands of Input/Output in
`C++`

. - Next, our mission will be collecting user inputs and displaying outputs.
- Then, we'll dig into the treasure trove of arithmetic expressions.
- Finally, we'll learn to apply variables within these expressions.

By the end of our journey, you'll wield the prowess of basic forms of Input/Output in `C++`

, crafting arithmetic expressions, and using variables within them.

Imagine programming as a river with two streams: `std::cin`

and `std::cout`

. `std::cin`

pours data into your program, and `std::cout`

drains data from your program to the user.

Think of `std::cin`

and `>>`

as a way to gather user input, and `std::cout`

and `<<`

as the microphone for your program to speak to users. However, due to platform constraints, we'll use constants instead of `std::cin`

in our examples.

In a typical programming environment, `std::cin >>`

couples with a variable to take user input.

C++`1int userAge; 2std::cin >> userAge;`

After the execution, `userAge`

will contain the value inputted from the console.

To output data to the user, we invoke the `std::cout <<`

mantra:

C++`1#include <iostream> 2 3int main() { 4 std::cout << "Hello, programming apprentice!"; 5 6 return 0; 7}`

But what about personalizing outputs? Just involve variables in your mantra:

C++`1#include <iostream> 2 3int main() { 4 int userAge = 15; 5 6 // The below line will print "You entered age as 15 years." 7 std::cout << "You entered age as " << userAge << " years."; 8 9 return 0; 10}`

In this snippet, we combine a string and a variable to display a tailored message to the user!

Now, let's unveil arithmetic expressions — they are simply mathematical formulas operating on variables and values. Take a glimpse of this mathematical wizardry:

C++`1#include <iostream> 2 3int main() { 4 int x = 10; 5 int y = 5; 6 7 std::cout << x * y; // prints 50 8 9 return 0; 10}`

Here, we've used a multiplication operation `x * y`

, and `std::cout <<`

delivers the result as `50`

!

Combining all these insights, let's apply variables within expressions. Marvel at this:

C++`1#include <iostream> 2 3int main() { 4 int x = 15, y = 3; // Defining two variables on the same line by splitting them with a comma 5 6 int res = 2 * (x + y * y) ; // results in 48 7 8 std::cout << "The result is: " << res; // displays: The result is: 48 9 10 return 0; 11}`

In this snippet, we define two variables, `x`

and `y`

, assign constant values to them, use them in an expression to calculate the result, and then echo the result.
In the same way, you can use other mathematical operations with variables such as:

`-`

for subtraction,`/`

for division,`%`

for the modulus operation (which returns the remainder of a division).

Congratulations! You've journeyed through the territories of Input/Output, expressions, and the application of variables. You've learned:

- How to unearth Input/Output to craft interactive programs.
- How to master expressions and understand arithmetic operators.
- How to apply variables in expressions.

Now, brace yourself for practice exercises to cement your newfound treasures. Remember, practice makes a coder proficient, so let's dive into practice!