The Decision Control Structure Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Default Control Structure:

A

Sequence Control Structure.

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

Decision control instruction is implemented using:

A
  1. The if statement
  2. if - else
  3. Conditional Operators.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

if

Syntax:

A

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.

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

Decisions. In c?

A

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.

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

Relational Operators:

A
x == y            x is equal to y
  !=                      not equal
   <                    smaller than
     >                   greater than
< =  ,  > =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What if a variable is not initialised:

A

Then it contains some unpredictable value, aka GARBAGE Value.

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

Default Scope of If statement:

and else

A

Immediately next statement after it.

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

The if - else statements:

A
else allows us to execute statments when condition is false.
if (Exp)
   {  Statement(s);  }
else
   { Statements(s); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Nested if-else:

A

adding if and else blocks within if or else blocks.

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

Logical Operators:

A

&&= AND
|| = OR
! = NOT

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

Use of logical operators:

A

Allows us to combine 2 or more conditions.

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

else if Clause:

A
if()
{}
else if ()
{}
else
{}

last else is optional.

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

Use of logical operators to optimize the code:

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Precedence:

A
! 
> *,/,% > +,- 
 >  
'', '>=', '<=',  
> ==, != 
> &amp;&amp; 
>|| 
> =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What if you add semicolon at the end of if’s condition
if ();
{ if block }

A

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 }

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

if( i = 5)
printf (‘true’);
output?

A

You will always get true as ans cuz its basically
if (5)
printf(‘true’);
as assignment operator is used and not ==.

17
Q

The Conditional Operators:

A

aka ternary operators, take 3 arguments.

expression 1 ? expression 2 : Exp 3

if-then-else.

Ex:
y = (x>5?3:4);
printf(“%c”, (a >=’a’ ? a : ‘!’) ); [execute this, testing should be declared before or add quotes.]
They can be nested as well.

18
Q

Lvalue Required

Error:

A

a > b ? g = a : g = b;
will give that error.
a > b ? g = a : (g = b);

In absence of parentheses, the compiler believes that b is being assigned to result of the expression to the left of 2nd =. Hence it reports an error.

Works fine

int main()
{
    int a,b,g;
    a = 5; b = 10; g = 0;
    a > b ? g = a : (g = b);
    printf("%d",g);
return 0; }
19
Q

limitation of ? :

A

: after this only one statement alowed…