If, else…

I’m a teacher by profession and I feel like these two words come out of my mouth far too often! But they’re really useful and I can actually start writing programs now ^^*.

Simply, if somethings true, do this. Else if that’s not true and this other thing is true, do this. Else, do this other thing. Here’s simple code I made.


#include <iostream>

using namespace std;

int main()
{
 int score = 0;
 cout<<"What's your score out of 100? \n";
 cin>> score;

 if (score == 100)
 {
 cout<<"You got a perfect score!";
 }
 else if (score > 90 && score <100)
 {
 cout<<"You scored an A";
 }
 else if (score > 80 && score <90)
 {
 cout<<"You scored an B";
 }
 else if (score > 70 && score <80)
 {
 cout<<"You scored an C";
 }
 else if (score > 60 && score <70)
 {
 cout<<"You scored an D";
 }
 else if (score >= 0 && score <60)
 {
 cout<<"You scored an F";
 }
 else
 {
 cout<<"Did you enter a number between 0-100?";
 }

}

I got a little head of myself when I wrote this and started using Boolean operators; AND &&, OR || and NOT !. NOT confused me at first but a good example is !(TRUE) = FALSE.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s