Conditional Statements in C++ Programming Flashcards
[ Identification ]
It evaluates TRUE or FALSE statement.
Boolean Expression
[ Identification ]
- Developed by George Boole.
- A branch of discrete mathematics that is dedicated to the study of the properties and the manipulation of logical expressions.
- Uses relational operators to compare expressions.
Boolean Algebra
[ Identification ]
He developed Boolean Algebra.
George Boole
[ Identification ]
Used to compare the value of two variables.
Relational Operators
[ Identify / illustrate the operator being described. ]
Used when the value of two variables are equal.
==
[ Identify / illustrate the operator being described. ]
Used when the value of two variables are not equal.
!=
[ Identify / illustrate the operator being described. ]
Used when one value is greater than another.
>
[ Identify / illustrate the operator being described. ]
Used when one value is greater than or equal to another.
> =
[ Identify / illustrate the operator being described. ]
Used when one value is less than another.
<
[ Identify / illustrate the operator being described. ]
Used when one value is less than or equal to another.
<=
[ Identification ]
- Also known as selection statements
- Used to make decisions based on a given condition.
Conditional Statements
[ True or False ]
If the condition evaluates to TRUE, a set of statements is executed, otherwise another set of statements is executed.
True
Conditional statements are also known as _______ ________.
selection statements
[ What are these? ]
- Simple IF Statement
- IF - ELSE Statement
- NESTED IF Statement
- SWITCH Statement
Forms of Conditional Statement
[ Identification: Forms of Conditional Statement ]
- A powerful statement for decision making and is used to control the flow of execution of statements.
- A two-way decision-making statement and is used in conjunction with an expression.
Simple IF Statement
[ Identification: Forms of Conditional Statement ]
STRUCTURE:
if(condition)
{ statement; }
Example:
if(x==100)
{
cout«“x is 100”;
}
Simple IF Statement
[ Fill in the blanks. ]
An __ ______ can be followed by an optional ____ _______, which executes when the Boolean expression is false.
An if statement can be followed by an optional else statement, which executed when the Boolean expression is false.
[ True or False ]
If the Boolean expression evaluates to true, then the else block will be executed, otherwise, the if block will be executed.
False.
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed.
[ Identification: Forms of Conditional Statement ]
- An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
IF - ELSE Statement
[ Identification: Forms of Conditional Statement ]
STRUCTURE:
if(condition)
{
statement 1;
}
else
{
statement 2;
}
Example:
if(Grade>=75)
{
cout«“Congratulations, You Passed!”;
}
else
{
cout«“You failed, better luck next time!”;
}
IF - ELSE Statement
[ Identification: Forms of Conditional Statement ]
- Use one if or else if statement inside another if or else if statement(s).
NESTED IF - ELSE Statement
[ Identification: Forms of Conditional Statement ]
STRUCTURE:
if(condition)
{
if(condition 1)
{ statement 1; }
else
{ statement 2; }
}
else
{ statement 3; }
Example:
int ctr = 0;
int Password;
if(Password==”JRUat100”)
{
cout«“Login Succesfully!};
}
else
{
cout«“Login Failed!”;
ctr++;
if (ctr==3){
cout«“Pease Try again later!”; }
}
NESTED IF - ELSE Statement
[ Identification: Forms of Conditional Statement ]
- Allows a variable to be tested for equality against a list of values.
- Used when you have multiple possibilities for the if statement.
SWITCH Statement
[ Identification: Forms of Conditional Statement ]
STRUCTURE:
switch(variable)
{
case 1:
//execute your code
break;
case n:
//execute your code
break;
default:
//execute your code
}
Example:
int num = 2;
cout«“Pls enter number”;
cin»num;
switch(num) {
case 1:
cout«“You selected 1.”;
break;
case 2:
cout«“You selected 2.”;
break;
default:
cout«“Pls select a number from 1-2”;
}
SWITCH / SWITCH CASE Statement
[ Fill in the blanks: Rules in Switch Statement ]
The expression used in a switch statement must have an ______ or enumerated type or a class type in which the class has a single conversion function to an ______ or enumerated type.
integral
[ Fill in the blanks: Rules in Switch Statement ]
You can have any number of _____ ______ within a switch. Each case is followed by the value to be compared to and a colon.
case statements
[ Fill in the blanks: Rules in Switch Statement ]
When a _____ statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
break
[ Fill in the blanks: Rules in Switch Statement ]
A switch statement can have an optional ______ case, which must appear at the end of the switch. The _____ case can be used for performing a task when none of the cases is true. No _____ is needed in the ____ case.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
[ Identification ]
Used to perform logical operations on the given two variables.
Logical Operators
[ Identify / illustrate the operator being described. ]
Executes TRUE statement if all conditions are TRUE.
&& - AND
[ Identify / illustrate the operator being described. ]
Executes TRUE statement if either of the conditions is TRUE.
|| - OR
[ Identify / illustrate the operator being described. ]
Executes TRUE statement if the condition is FALSE.
Executes FALSE statement if the condition is TRUE.
! - NOT