If statement
There 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.
< | less than |
> | greater than |
== | equal to |
<= | less than or equal to |
>= | greater than or equal to |
!= | not equal to |
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.
if (1 < 2) { //Code goes here }if (1 <= 2) { //Code goes here } if (name == “Bob”) if (trueorfale == true) |
Else Statements
Else 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 {}.
int GradePointAverage = 83; if (GradePointAverage > 90) { cout<<“You’are on the high honor roll \n”; } else if (GradePointAverage >= 80) { cout<<“You’are on honor roll \n”; } else { cout<<“You aren’t on the honor roll this semester \n”; } |
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(||)
int GradePointAverage = 40; if (GradePointAverage >= 90 && GradePointAverage <= 100) { cout<<“You’are on the highhonor roll”; } else if (GradePointAverage <= 89 && GradePointAverage >=80) { cout<<“You are on the honor roll this semester”; } else if (GradePointAverage <= 79 && GradePointAverage >=70) { cout<<“You passed the course “; } } else if (GradePointAverage <= 69 && GradePointAverage >=0) { cout<<“You failed the course “; } else { cout<<“There’s an error with your grade”; } |
Switch
Else 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.
char grade=’C’; switch(grade) { case ‘a’: case ‘A’: cout<<“You got an A”; break; case ‘b’: case ‘B’: cout<<“You got a B”; break; case ‘c’: case ‘C’: cout<<“You got an C”; break; case ‘d’: case ‘D’: cout<<“You got an D”; break; case ‘f’: case ‘F’: cout<<“You got an F”; break; default: cout<<“There’s an error with your grade”; } |
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.