selection Flashcards

1
Q

Selection Statements Used to allow your program to

A

“think” and make logical choices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Logical expressions are used to provide decision-making ability
a they protect the body of a selection statement.

A

In other words,
you will ONLY perform the body of a selection statement if the logical
expression evaluates to TRUE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The body of a selection statement uses the

A

same brackets as the main
function {}. These brackets denote the start and end of a selection
statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

You can put anything that you want inside of the body of a selection
statement (Ex. Declare variables, do output, ask for input, etc.).

A

However, they will only be valid within the body of the selection
statement. In other words, once you leave the body it no longer exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Independent IF Statements

- Each independent IF statement is processed

A

and evaluated independently

of any other statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Syntax if

A

if(Logical Expression)
{
Statement(s)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  • Example:
    int temp = 0;
          temp = 27;
A

if(temp <= 32)
{
cout &laquo_space;“It is cold outside.” &laquo_space;endl;
}

		if(temp <= 55)
		{
			cout << "It is cool outside." << endl;
		}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

IF/ELSE Statements

- The IF and ELSE are

A

dependent on each other
You must choose one or the other. If the first one is true,
then the second one must be false and vice versa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The else statement acts as the

A

default and therefore NEVER needs

a logical expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

IF/ELSE Statements syntax

A
if(Logical Expression)
				{
					Statement(s)
				}
				else
				{
					Statement(s)
				}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

-Example:
int temp = 0;
temp = 27;

A
if(temp <= 32)
			{
				cout << "It is cold outside." << endl;
			}
			else
			{
				cout << "It is not that cold outside." << endl;
			}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

IF/ELSE IF/ELSE Statements

- This works similar to the IF/ELSE

A

This is used when you want to extend the number of options
to choose from. You can add as many “else if” as needed
to increase the number of choices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Each ELSE IF has its own

A

logical expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The IF, ELSE IF(s) and ELSE are all

A

dependent on each other
- You must choose one from the group. The first one that is
evaluated to true is selected even if other logical expressions
in the group are true as well

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The else statement acts as the default

A

and therefore NEVER needs

a logical expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

IF/ELSE IF/ELSE syntax

A
if(Logical Expression #1)
				{
					Statement(s)
				}
				else if(Logical Expression #2)
				{
					Statement(s)
				}
				else if(Logical Expression #3)
				{
					Statement(s)
				}
				else
				{
					Statement(s)
				}
17
Q
IF/ELSE IF/ELSE  syntax 
Example:
			int temp = 0;
			temp = 45;
			if(temp <= 45)
A

{
cout &laquo_space;“It is cold outside.” &laquo_space;endl;
}
else if(temp >= 85)
{
cout &laquo_space;“It is hot outside.” &laquo_space;endl;
}
else
{
cout &laquo_space;“It is nice outside.” &laquo_space;endl;
}

18
Q
Example #1 - Write a selection statement that will display "You passed"
	//if the user enters a grade greater than or equal to 70. Write a selection
    //statement that will display "Perfect Score" if the user enters a grade 
	//greater than or equal to 100.
A

double grade = 0; //Declare variable
cout &laquo_space;“Enter your grade: “; //Request
cin&raquo_space; grade; //Response

	//Check for grade GTE 70
	if(grade >= 70)
	{
		cout << "You passed" << endl;
	}
	//Check for grade GTE 100
	if(grade >= 100)
	{
		cout << "Perfect Score" << endl;
	}
19
Q
//Example #2 - Write a selection statement that allows the
    //user to choose either door #1 or door #2.
	//Display the users selection.
A

int door = 0; //Declare variable
cout &laquo_space;“Please choose a door (1 or 2): “; //Request
cin&raquo_space; door; //Response

	//Check for door #1 or door #2
	if(door == 1)
	{
		cout << "You chose door #1." << endl;
	}
	else
	{
		cout << "You chose door #2." << endl;
	}
20
Q
//Example #3 - Write a selection statement that allows the
    //user to choose either door #1, door #2, or door #3. If the
	//user chooses an invalid selection, display "Invalid choice".
	//Display the users selection.
A

int door = 0; //Declare variable
cout &laquo_space;“Please choose a door (1, 2, or 3): “; //Request
cin&raquo_space; door; //Response

	//Check for door #1, door #2, door #3, or invalid choice
	if(door == 1)
	{
		cout << "You chose door #1." << endl;
	}
	else if(door == 2)
	{
		cout << "You chose door #2." << endl;
	}
	else if(door == 3)
	{
		cout << "You chose door #3." << endl;
	}
	else 
	{
		cout << "Invalid choice" << endl;
21
Q
/Example #4 - Write a selection statement that allows the
    //user to choose an operation by entering the symbol (+, -, *, /).
	//If the user chooses an invalid selection, display "Invalid operation".
	//Display the users selection.
A

char op = ‘ ‘; //Declare variable
cout &laquo_space;“Please select an operaton (+, -, *, /): “; //Request
cin&raquo_space; op; //Response

	//Check for +, -, *, /, or invalid operation
	if(op == '+')
	{
		cout << "You chose addition." << endl;
	}
	else if(op == '-')
	{
		cout << "You chose subtraction." << endl;
	}
	else if(op == '*')
	{
		cout << "You chose multiplication." << endl;
	}
	else if(op == '/')
	{
		cout << "You chose division." << endl;
	}
	else 
	{
		cout << "Invalid operation." << endl;
	}