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