Introduction to C++ (Part II)

EdPsycho Hub
0
In part I, we talked about code structures and variables in C++. If you still need to read it, please click this.

Getting Inputs



This is how to get inputs in C++. We use the "cin" keyword for it. If you are a learner, I recommend that you code these examples yourself. First, you should initialize a value to get input. 



This is my console after I input the value as 5. Try yourself.


Operators


Addition, Subtraction, Multiplication, and Division are the four main operators in C++. They follow the normal order of operations in mathematics. Also, there are integer division and modular division. In C++ 5/2 is not equal to 2.5. "/" mark works as an integer division. Therefore the answer should be 2. When you try these codes yourself, don't panic if the answer to 3/6 equals 0. There are no sixes for 3 to be divided into, so the result should be zero.

Logical Operators



These are the syntax of logical operations in C++. In C++ 1 is true and 0 is false. If you want to know how to output true and false please comment below.

Conditional Statements

If you need to make a decision using a condition you can use "If, else" statements. Also, you can use the "else if" statement if you have more than two conditions.




Try this code yourself and get output for different "time" values. For example, change the time to 5, then your output should be "Good morning." Then try to improve this code to get the "time" value as input. 

Conclusion

This session covers the basics of C++, from getting inputs to conditional statements. It still needs to be finished. In the next part, we will discuss other fundamental concepts in C++ like loops, arrays, and functions.
Tags

Post a Comment

0Comments

Post a Comment (0)