
|
Home Blog Me Funbox Photo Gallery Entertainment Scripts Tutorials Programming Fundamentals Programming in C++ Part 1 Part 2 Part 3 Part 4 Part 5 Part 6 Part 7 Part 8 Part 9 Part 10 Visual BASIC Programming Horoscope Writing Site Center Links/Blogrolls Living Room Furniture |
If statementThere are times when you want the program to respond differently to different events. Say for example you wanted to display a certain message if the name you entered in the last tutorial was Ernie. You would use an if statement when you want to compare two or more things and have the code coninue based on the result will continue accordingly. Some of the comparison operators that the if statement uses should be familiar from math class.
The comparison needs to go in side Parentheses (). If there's only one line of code then it can go right on the next line, if the code spans more than one line then it goes inside curly braces {}. Personally I always use curly braces because it makes it easier to read. You can also nest if statements inside of if statements. One thing I must point out is the if statement is a line of code that can't have a semicolon.
Else StatementsElse statements are used when you want want something to happen if the comparison is false. The else statement comes in handy when say you want your program to do something if your name is Jimmy but do something completely different if your name was Sandra. You can never have an else statement by itself, it always needs to accompany an if statement, and like the if statement, all code must go inside curly braces {}.
As you can see if the GPA was an 83 then it would skip the first if statement but go to the second one and execute that. Say you wanted to make it check if it was between 90 and 100 and if it was between that then its on high honor roll, between 80 and 90 its just honor roll, 70 to 79 is passing, 0 to 69 is failing and anything else is an error. To do this you would need to combine if statements using ands and ors. In C++ the and statement consists of two ampersands (&&) while or is two pipes(||)
SwitchElse ifs can get messy if you aren't careful. If you have too many choices then you may want to use a switch statement. Switch statements are like using if statements, but really clean up the code a lot. With a switch you need an int or char variable that will be the focus of the statement. Every different outcome of that variable is called a case. The cases will go inside of curly braces {}, with every case you need a colon (:) to tell where the case begins, and most of the time you will want to have a break statement with your case. Without break statements, the case won't end and it will continue into the next case. Sometimes you will want the case to fall through to the next one. At the end of the switch you should have a default case. The default case is if it doesn't fit in any of the other cases. In this example we'll see why you'd want to leave out some of the break statements.
Whether you use ifs and else ifs, or switch statements is a matter of circumstance and preference. Personally if its more then three else ifs then I use the switch statement, otherwise else ifs. Get used to using the conditionals, as they are common in everyday programs. Next Section: Loops |
Copyright © 2004-2008 Sean Noble, All rights reserved
Read our Disclosure Policy


