CHAPTER 3 Conditional Instruction

 

What us conditional instruction in c language ?

CONDITIONAL INSTRUCTION

Sometimes we want to watch comedy videos on Youtube if the day is Sunday

Sometimes we order junk food if it is our friend’s birthday in the hostel

You might want to buy an umbrella if its raining and you have the money

you order the meal if dal or your favorite bhindi is listed on the menu.

All these are decisions which depend on a condition being met.

In c language too, we must be able to execute instructions on a condition being met.

learn c programming

Ques1 :-What are decision making instructions in c?



Decision Making Instructions in C

  • if-else statement
  • switch statement

Ques 2 : -What is if-else statement in c ?

  • if-else statement

The syntax of an if-else statement in C look like:

if (condition to be checked) {

statement-if-condition- true;

}

else{

statement-if-condition-false;

]

code example:-

int a= 23;

if (a>18){

printf(”you can drive\n”);

}

Note that else block is not necessary but optional.




Ques 3 :- What is relational operator in c?

Relational operator in C

Relational operator are used to evaluate conditions(true or false) inside the if statements Some examples of relational op;erators are:_

==, equals

= greater than or equal to

<, greater than

, ***** less than****

<=, less than or equal to

!= not equal to



Important note:- ‘=’ is used for assignment where as ‘==’ is used for equally check.

The condition can be any valid expression, In C a non-zero value is considered to be true.

Ques 4;- What are logical operator in c?

Logical Operators;-

&& ,|| and! are there logical operator in c

There are read as “Land”,”or” and ‘not’

They are used to provide logic to our C programs

usage of logical operators

  1. && → 1and → is true where both the conditions are true .

“1 and 0” is evaluated as false

“0 and 0” is evaluated as false

“1 and 1” is evaluated as true

  1. ||→ or→ is true when at least one of the conditions is true (1 or→1) (1or1→1)
  2. !→ return true if given false and false if given true

!(3==3)→ evaluates to false

!(3>3a)→ evaluate to true

As the number of condition increase the level of indentation increases. This reduce readability logical operator come to reuse in such cases.

Ques 5 :- What is elseif clause in c ?

else if clause

Instead of using multiple if strands we can also use else if along with if this forming as if an if-elseif-else ladder

if{

//statements;

}

elseif{

}

else{

}

Using if-elseif-else reduces indents

The last “else ” is optional

Also there can be any number of “else if”

Last else is executed only if all conditions fail

What is operator precedence in c?

Operator precedence

Priority Operator

1 !

2 *, / . %

3 +,-

4 <>, <=, >=

5 ==, !=

6 &&

7 ||

8 =



conditional operators

A short hand ‘if-else-” can be written using the conditional or ternary operators

condition ? expression-if-true : expression -if-false

Switch case control Instruction

Switch-case is used when we have to make a choice between number of alternatives for a given variable

Switch (integer=expression)

{

case C1:

code:

Case C2:

code:

Case C3:

code

default:

code:

}

The value of integer expression is matched against c1,c2,c3,…. If it matches any of these cases, that case along with all subsequent “cases” and “default” statement are executed



Important notes:-

  1. We can use switch- case statement even by writing cases in any order of our choice (not necessarily ascending)
  2. char values are allowed as they can be easily evaluated to an integer
  3. A switch can occur within another but in practice this is rarely done.

Comments

Popular posts from this blog

Privacy Policy