It appears you have not yet registered with our community. To register please click here.

Origin XT RPG Network Home







XT Project Introduction
Blah blah intro to this place. To be composed later.

C++ - Tutorial, Lesson 4, Conditional Statements and Switches


Mar 26 2007, 03:54 PM (Post #1)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM

Lesson 4 - Conditional Statements and Switches


Welcome to the fourth lesson in my C++ tutorial. I hope you enjoy it.

What is a Conditional Statement?


A conditional statement describes syntax to organize logical statements: if x, then y, else z. (etc) If you don't understand that logic, then don't worry. The code will hopefully help you out a bit.

Example One


CODE

#include<iostream>
using namespace std;

int main()
{
int i;
int j;

cout<<"Please input a number."<<endl;
cin>>i;
cout<<"Please input a number."<<endl;
cin>>j;

if (j > i)
{
cout<<"j is greater than i!"<<endl;
}

else
{
cout<<"j is less then or equal to i!"<<endl;
}

//note the brackets in the above conditional
//because the conditional only contains one line
//these brackets are not nessasary to use
//however, it is good practice to use them

system("PAUSE");
return 0;
}

That's basically it, if something is true, then something will happen, else something else will happen. You can also nest conditionals together to make lots of conditions.

Example Two


CODE

#include<iostream>
using namespace std;

int main()
{
int i;
int j;

cout<<"Please input a number."<<endl;
cin>>i;
cout<<"Please input a number."<<endl;
cin>>j;

if (j>i)
{
if(j>10)
{
cout<<"j is greater than i and j is greater than 10!"<<endl;
}
}

//this code could also look like this:

if (j>i && j>10)
{
cout<<"j is greater than i and is greater than 10!"<<endl;
}

//but nesting is good if you want to do this:

if (j>i)
{
if(j>10)
{
cout<<"j is greater than i and j is greater than 10!"<<endl;
}
else
{
cout<<"j is greater than i but is not greater than 10!"<<endl;
}
}
else
{
cout<<"j is not greater than i nor is it greater than 10!"<<endl;
}

system("PAUSE");
return 0;
}

Conditional statements are very useful in C++, and are very easy to manipulate. Have fun with them!

What are Switches?


Switches are useful if you have tons of conditions you want to check; it compares a given value to a list of constants. Let me show you an example.

Example Three


CODE

#include<iostream>
using namespace std;

int main()
{
int choice;

cout<<"Welcome to my program!"<<endl;
cout<<"Menu: "<<endl;
cout<<endl;
cout<<"1 - Split File"<<endl;
cout<<"2 - Merge Files"<<endl;
cout<<"3 - Edit File"<<endl;
cout<<"4 - Exit"<<endl;

cin>>choice;

//this would take a long time with normal conditional statements
//so lets use switches!
//but first we must do error checking

if (choice < 1 || choice > 4)
{
cout<<"Input the correct number you idiot!"<<endl;
}

//the double bars in the conditional mean OR
//so if some fool decides to input the wrong number
//the program will close
//now lets use a switch

switch (choice)
{
case 1:
cout<<"The program will split the file."<<endl;
break; //this breaks the switch, so it doesnt go through the rest of the
//conditions; that would be bad.

case 2:
cout<<"The program will merge files."<<endl;
break;

case 3:
cout<<"The program will edit a file."<<endl;
break;

case 4:
cout<<"The program will exit."<<endl;
//no break is needed here, since it is the last case in the switch
}

system("PAUSE");
return 0;
}

Conjugating Symbols for Conditionals


&& - and
|| - or
> - greater than
< - less than
>= - greater than or equal to
<= - less than or equal to
!= - does not equal
== - does equal

Afterword


That pretty much covers everything that needed to be covered in this lesson. You're now ready for Lesson 5. If you've made it this far, you're no longer a beginner C++ programmer, and you're ready for the intermediate level stuff. Congratulations!

Next Lesson:
Lesson 5 - Functions, Classes, and OOP

~Nesticles~

This post has been edited by Jinghao: Jun 16 2007, 12:59 AM
Post Options

 
Mar 26 2007, 06:40 PM (Post #2)
Member Of The Year 2005
* * * * * * * * *
Posts: 10,363
Cash: -77,174 / 11,869,762
Group: Administrator
Joined: 2/28/03 10:58 AM
Thanks for doing these man, keep it up ssmile.gif
Post Options

Mar 30 2007, 03:51 AM (Post #3)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM
No problem, glad you enjoy them.
Post Options

Mar 30 2007, 04:31 AM (Post #4)
Not Odd anymore
* * * * * * * * * *
Posts: 45,875
Cash: 1,915,578 / 1,817,041,051
Group: Administrator
Joined: 7/10/02 09:48 PM
These are good tutorials ssmile.gif I'll probably start using C++ instead of C soon.
Post Options

Mar 30 2007, 03:54 PM (Post #5)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM
Thank you, Jinghao. I'll probably post the next tutorial tonight or tomorrow.
Post Options