Tuesday, February 16, 2016

3 Basic Conditionals

Hello World,

Today we will be discussing conditionals. These conditionals were pretty easy for me to understand. Basically you have 3 conditionals, or at least 3 that we will cover today. They are “if”, “else if”, and “else”. You may be familiar with these conditionals if you have had any programming experience before in a common programming language.

Conditionals are commonly used to test a comparison. I made a basic program that uses user input to test which number is the largest of 3 numbers. Of course anyone can tell you which number is largest off first glance of 3 numbers. However, this program uses all 3 conditionals in an easy to understand way.

Below is my program.




Basically, it starts by asking the user to enter 3 numbers. The numbers are instance variables as learned in a previous post. Then there are some conditionals. A, b, and c correspond to the first number inputted, the second number inputted, and the third number inputted respectively. The first tests if a is larger than both c and b. If it is, then it is displayed as the largest. Then within each if and if else statement, there is a test to see if the highest number is positive or negative. If it is greater than zero, it is positive. Otherwise it is negative (else). The second conditional asks if c is larger than b and a. If it is, the program figures out if it is negative or positive again. The third conditional asks if b is larger than both a and c, then figures out if it is negative or positive. Then the last else if statement tests if a is larger than b, but c is larger than b, then tests the negative and positive.

This is how a potential output may look.


Hope you enjoyed this lesson. Let me know if anything is unclear.


Thanks!

Tuesday, February 9, 2016

Constants, Variables, and Arithmetic

Hello World,

Today we will be discussing how to declare variables, constants, and basic arithmetic using C++. Fortunately for me, due to my prior experience with Java, today’s post is a simple review. However, one thing that is necessary for your code to execute properly is to make sure everything prints correctly. What I mean by this, at the top of your compiler, verify that this line of code exists: using namespace std;. This is the code that prints any output. I had to add this line of code to my program because it wasn’t already a default in my compiler.

Let’s start with declaring a variable. C++ has many of the same features as Java when it comes to declaring variables. For instance if you are making an instance variable in your main called volume, you just type: int volume;. Now that the instance variable volume is defined you can assign it to a number by saying volume = 1; on the line after. And you can then print that by typing: cout << volume;. Please refer to the picture below to get a better understanding of what I am talking about.



Constants are slightly different that how Java does it. In Java, at the top of the page you can just say int constant; and that is that. C++ has syntax that is slightly different. At the top of the page you must type #define “(constant’s name)” and then add a number. Also semicolons are not necessary in defining constants. Below is how to define a constant properly.


Finally arithmetic is exactly the same in Java as it is for C++. Some of the commands you can do in arithmetic are: addition, subtraction, multiplication, division, mod, incrementing by 1, decrementing by 1, divide and assign, multiply and assign, and mod and assign. Below is a picture taken from wikiversity.org. It shows how to write the arithmetic commands in C++. If you are familiar with Java then this should be an easy review.


Now let’s take what we just learned and apply it to a basic arithmetic function. I defined 3 constants, LENGTH, WIDTH, and HEIGHT, at the top of the page. Then I made an instance variable called volume. I set volume equal to LENGTH*WIDTH*HEIGHT and then I print out the result. Below is a picture of this little program.


I hope you enjoyed this post and please let me know if you have any questions pertaining to some of the information we covered.

Thanks!

Monday, February 1, 2016

Hello World Program

Hello World,

Today I will show you how to do some of the simple features of C++. In particular I will be doing a program involving print statements and possibly implementing a scanner to get user input in the program. This should not difficult to implement even for a beginner. I found this program fun and interesting. 

For this program to work successfully, you will need a working compiler. As I mentioned last week, the compilers that I found are free and require no installation. The compiler that I will be using is called C++ Shell. There is also another compiler on Code Chef’s website which is free and requires no installation as well.

In many ways, from the research I have been doing on the language so far, C++ is similar to the languages I have previously worked with. However, there are minor syntax differences. In order to get a working program, you must have a main method or somewhere to call upon your previous work. You can make a main method by stating: int main() {….// this is where your work is and then finish with }. Just like Java, two slashes marks a comment.


The 1st line is the comment “Example program.” You can tell it’s a comment by the two slashes. The 2nd and 3rd lines include iostream and string is there because you have to tell the program all the components that you will be using to write out your program. This is just like how Java makes the user import rectangles or ellipses if you are making graphics. Iostream is basically the input for telling the program that you will be including declarations of the basic input and outputs your program will have. In other words you will be stating if you are printing out text. The 5th line is just making the main method. Line 7 is how to make a print statement. It starts with std::cout << “…” includes \n and ends with a semicolon. Std stands for standard and cout means character output. \n is just like Java because it allows for your previous text to be on its own line. Line 8 defines the string you will be using and it is called name. Line 9 prompts the user to enter their name and then line 10 retrieves the input you just typed in and prints a line in 11 saying “Hello, (insert name)! Thank you for experiencing this memorable occasion with me!

Lastly, I included a short video of how the program may function. Hope you enjoy it!




The information I got pertaining to this introduction to the language was obtained at www.CPlusPlus.com