The Decision Control Structure Flashcards
Default Control Structure:
Sequence Control Structure.
Decision control instruction is implemented using:
- The if statement
- if - else
- Conditional Operators.
if
Syntax:
if ( this condition is true)
execute this statement;
if (expression)
{
statement(s);
}
expression = 0 means false. non zero is true.
If { } is absent then immediately next statement after if is executed on satisfaction of expression.
Decisions. In c?
When certain conditions are met we make decisions.
If they are not met, we don’t make that decision or don’t execute that statement.
Keyword if tells C that its time to make a decision.
Relational Operators:
x == y x is equal to y != not equal < smaller than > greater than < = , > =
What if a variable is not initialised:
Then it contains some unpredictable value, aka GARBAGE Value.
Default Scope of If statement:
and else
Immediately next statement after it.
The if - else statements:
else allows us to execute statments when condition is false. if (Exp) { Statement(s); } else { Statements(s); }
Nested if-else:
adding if and else blocks within if or else blocks.
Logical Operators:
&&= AND
|| = OR
! = NOT
Use of logical operators:
Allows us to combine 2 or more conditions.
else if Clause:
if() {} else if () {} else {}
last else is optional.
Use of logical operators to optimize the code:
- If the ans boils down to 2 then. often called yes/No problem.
if (exp1 || exp2.1 && exp2.2 || Exp 3 )
{ans 1}
else
{ ans 2 } - To test if the value falls within a range or not.
Precedence:
! > *,/,% > +,- > '', '>=', '<=', > ==, != > && >|| > =
What if you add semicolon at the end of if’s condition
if ();
{ if block }
Then if condition satisfied a null statement will get executed which does ntg, if not satisfied then it won’t get executed. either ways the if block will get executed.
if ()
;
{ if block }