Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» GG Stuff

Follow us on Twitter!

Get the GG toolbar today (for firefox only)
» Recent Threads
Go to first new post Who is your favorite...
06-28-2007 04:47 PM
Last post by Oops
Today 09:35 AM
92 Replies, 1,221 Views
Go to first new post MotM February Voting!
Today 09:05 AM
by Zombie
Last post by MeinKampfyChair
Today 09:20 AM
3 Replies, 17 Views
Go to first new post What are you currently...
03-04-2010 02:21 PM
by Oops
Last post by Axed
Today 09:16 AM
10 Replies, 205 Views
Go to first new post Loyal User Perk
Yesterday 05:01 PM
by BSavage
Last post by l_royalty_l
Today 08:53 AM
20 Replies, 206 Views
Go to first new post RSBot - Download Center
04-20-2009 04:05 PM
by Chinese
Last post by pitbulll
Today 08:34 AM
74 Replies, 157,490 Views
Reply
 
LinkBack Thread Tools Display Modes

 C++ Programming: Chapter 4
Old 02-02-2009, 01:58 PM   #1 (permalink)
Full Member

Male Arachnid is offline
 
Arachnid's Avatar
 
Join Date: Sep 2008
Posts: 738
GPoints: 4,213
iTrader: 0 / 0%
Arachnid Is Popular
Rep Power: 6
C++ Programming: Chapter 4

Chapter 4

In this chapter, I will learn you about the if statement, it's siblings, and how it lets the user, through input, make a choice. But before we cover that I need to tell you about something I have neglected to say. It isn't rerquired but it is good habit that every time you use an open delimiter (curly bracket) you indent, and you go back one indent every time you use a close delimiter. Now back to the if statements. There are different types of the if statement. They are:
if
if else
if else if
nested if
switch



If statement

The if statement is used to say if this statement is true, then do this before continuing on, otherwise be on you way. The syntax for the if statement is pretty simple. It is as follows:
Code:
int var1 = 1;
if (var1 == 1)
statement;
or it could be done like this for multiple statements:
Code:
int var1 = 1;
if (var1 == 1)
{
statments;
}
Notice that at the end of the if statement, that there is not a semi-colon. That is because if there was a semi-colon, the compiler would say ,"okay so if var1 is 1 then do everything after the if statement." But putting a semi-colon at the end says this is the end of the if statement which would cause all of the statements that you wanted to be controlled by the if statement to execute no matter what. The next control structure is the if else statement. It acts like a two way choice. The syntax for it is:
Code:
bool var1 = true;
if (var1 == true)
{
statements;
}
else
{statements;
}
Now you may be saying to yourself, "What is bool?" Well if you read one of those links I provided in chapter 2 about variable types, then you would know that bool is a bolean variable type that can have only one of two values: true or false. So that is how wer do a two way choice controlled structure. But what if you want to have a multiple choice function? Well you can do one of two things. Either nest the ifs or use an if else if statement. To nest if statements is pretty easy. To do that, do this:
Code:
if (condition)
{
if (second_condition)
{
if (third_condition)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
}
else
{
statements;
}
That to me was very easy because what you are saying is if this is true go to the next if otherwise do this statement. To use the if else if statement which is much easier, then do this:
Code:
int var1;
cout << "Enter a number from 1 to 3: ";
cin >> var1;
if (var1 == 1)
{
statements;
}
else if (var1 == 2)
{
statements;
}
else if (var1 == 3)
{
statements;
}
else if (var1 < 1 || var1 > 3)
{
cout << "Not a valid number";
}
Notice the last else if. I made it to where if some idiot user tries to put in the wrong number then it says "FU" and continues on its way. Now let us take a look at the switch statement. I decided to put this with the if statements because that was who it was with in the if statement's section of the book. The switch statement gives the power to choose to the computer (sorta). To do a switch command, do this:
Code:
switch (expressionn)
{
case value1:
statements;
break;

case value2:
statements;
break;

case value3:
statements;
break;

...

case valueN:
statements;
break;

default:
statements;
}
Case, break, default, and switch are all reserved words. Break is used to escape the switch statment early. In fact you can use the break statement to escape any function early. Now what happens when this executes is that the computer sees the word switch and then evaluates the expression. Whatever the result of the expression is determines what happens. If the expression does not evaluate to any of the values listed, then the program executes the statements under default. The switch statement is very good for when the user must input something because the can not only try to enter in a wrong value of the same type but they can try to enter in anything and it will just do the default. Now let's review everything we have learned by me showing a program that incorporates most of what we have learned so far.
Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
string loop_var = "yes";
while (loop_var == "yes")
{
int var1;
cout << "Enter the number that corresponds to the month of your birth: ";
cin >> var1;
switch (var1)
{
case 1:
cout << "Your month of birth is January." << endl;
break;

case 2:
cout << "Your month of birth is February." << endl;
break;

case 3:
cout << "Your month of birth is March." << endl;
break;

case 4:
cout << "Your month of birth is April." << endl;
break;

case 5:
cout << "Your month of birth is May." << endl;
break;

case 6:
cout << "Your month of birth is June." << endl;
break;

case 7:
cout << "Your month of birth is July." << endl;
break;

case 8:
cout << "Your month of birth is August." << endl;
break;

case 9:
cout << "Your month of birth is September." << endl;
break;

case 10:
cout << "Your month of birth is October." << endl;
break;

case 11:
cout << "Your month of birth is November." << endl;
break;

case 12:
cout << "Your month of birth is December." << endl;
break;

default:
cout << "That is not a valid option." << endl;
}
cout << "Would you like to calculate another birth month? Type \"yes\" to continue. ";
cin >> loop_var;
}
cin.get();
cin.get();
return EXIT_SUCCESS;
}
What that basically does is ask you what is your birth month (number) and converts it to the month's name. It then asks you would you like to do it again.
I am not going to make another chapter for a while because I am at the limit of my knowledge so when I learn more from my class, I will most likely make another chapter. Until then, peace.

~Daniel
__________________
Fruitflies.

Last edited by Arachnid; 02-03-2009 at 06:56 AM..
  Reply With Quote

 
Old 02-02-2009, 02:24 PM   #2 (permalink)
G3
I don't never troll

G3 is online now
 
G3's Avatar
 
Join Date: Jun 2009
Location: live? wut.
Posts: 6,918
GPoints: 16,890
iTrader: 0 / 0%
G3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYYG3 Is MANLYYYYYYY
Rep Power: 30
Well I don't see anything wrong with your code but does your teacher teach you about indentation?

Indenting helps you a lot, it makes it easier to locate things.

Or was it just that you couldn't indent on the gg post box?
  Reply With Quote

 
Old 02-02-2009, 03:19 PM   #3 (permalink)
Full Member

Male Arachnid is offline
 
Arachnid's Avatar
 
Join Date: Sep 2008
Posts: 738
GPoints: 4,213
iTrader: 0 / 0%
Arachnid Is Popular
Rep Power: 6
Quote:
Originally Posted by |G3| View Post
Well I don't see anything wrong with your code but does your teacher teach you about indentation?

Indenting helps you a lot, it makes it easier to locate things.

Or was it just that you couldn't indent on the gg post box?
Well I usually do indent when I do make something but I wasn't sure if it would on here.
__________________
Fruitflies.
  Reply With Quote

 
Old 02-02-2009, 06:04 PM   #4 (permalink)
Banned

Male Checker is offline
 
Join Date: Feb 2008
Posts: 2,646
GPoints: 1,628
iTrader: 8 / 100%
Checker - Total CelebrityChecker - Total CelebrityChecker - Total CelebrityChecker - Total CelebrityChecker - Total CelebrityChecker - Total Celebrity
Rep Power: 0
Good job explaining it. It works fine ( I tested it ) and you did an ok job on keeping your explanation idiot proof.
  Reply With Quote
The Following User Says Thank You to Checker For This Useful Post:
Arachnid (02-02-2009)

 
Old 02-02-2009, 06:08 PM   #5 (permalink)
Full Member

Male Arachnid is offline
 
Arachnid's Avatar
 
Join Date: Sep 2008
Posts: 738
GPoints: 4,213
iTrader: 0 / 0%
Arachnid Is Popular
Rep Power: 6
Quote:
Originally Posted by Checker View Post
Good job explaining it. It works fine ( I tested it ) and you did an ok job on keeping your explanation idiot proof.
Well I'm trying to make it to where it is beginner friendly and easy to understand (if they started out by reading the other chapters). Thanks for your time.
__________________
Fruitflies.
  Reply With Quote

 
Old 02-02-2009, 06:15 PM   #6 (permalink)
Underground

Male Fewmitz is offline
 
Fewmitz's Avatar
 
Join Date: Nov 2006
Location: fewmitz@live.com
Posts: 7,186
GPoints: 22,008
iTrader: 1 / 100%
Fewmitz Is a Lord of AwesomenessFewmitz Is a Lord of AwesomenessFewmitz Is a Lord of AwesomenessFewmitz Is a Lord of Awesomeness
Rep Power: 24
Thanks man, you really learned me good.

I already knew it, but my joke is still important.
__________________
Paradise is in the soul and your eyes are the gate.
  Reply With Quote

 
Old 02-02-2009, 06:19 PM   #7 (permalink)
Full Member

Male Arachnid is offline
 
Arachnid's Avatar
 
Join Date: Sep 2008
Posts: 738
GPoints: 4,213
iTrader: 0 / 0%
Arachnid Is Popular
Rep Power: 6
Quote:
Originally Posted by Fewmitz View Post
Thanks man, you really learned me good.

I already knew it, but my joke is still important.
Lol. So to what point did you exactly just get fed up and stopped learning C++, Fewmitz? Was it way past this?
__________________
Fruitflies.
  Reply With Quote

 
Old 02-02-2009, 06:27 PM   #8 (permalink)
Underground

Male Fewmitz is offline
 
Fewmitz's Avatar
 
Join Date: Nov 2006
Location: fewmitz@live.com
Posts: 7,186
GPoints: 22,008
iTrader: 1 / 100%
Fewmitz Is a Lord of AwesomenessFewmitz Is a Lord of AwesomenessFewmitz Is a Lord of AwesomenessFewmitz Is a Lord of Awesomeness
Rep Power: 24
I never really liked C++. I dunno if it was the syntax or that fact that the console is ugly.

I had a half-class for a semester where we messed with C. I did, I think everything about the console, and then we fucked around with its GUI a little bit, but we never covered anything useful and we didn't even get a proper explanation on pointers/classes.

This wouldn't be a much of a problem, but I just lost interest. I like Java better, plus none of the classes offered thus far actually have to do with what I need to learn.
__________________
Paradise is in the soul and your eyes are the gate.
  Reply With Quote

 
Old 02-02-2009, 10:03 PM   #9 (permalink)
Full Member

Undisclosed whitealexander is offline
 
whitealexander's Avatar
 
Join Date: Jan 2009
Location: 1116 Lowndes Hill Park Road Bakersfield
Posts: 124
GPoints: 1,915
iTrader: 0 / 0%
whitealexander Is Recognizable
Rep Power: 4
G3 was right indentations makes the code more understandable..
what if you have thousands of code and you need to find it?
spaces and indentations are a good coders habit..
__________________
just do what you think...
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Powered by vBadvanced CMPS v3.1.0

All times are GMT -7. The time now is 09:37 AM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.28630090 seconds (100.00% PHP - 0% MySQL) with 21 queries