conditional statements in C.

Hello coders! Welcome back!

Today will look into conditional statements in C language along with examples. 

1) IF-ELSE- 

If a condition satisfies then execute the following sets of code else execute the other set of instructions. 

In the above flow graph, If the condition is true then execute statement-1 else i.e, if it is false then execute statement-2. 

Let's take an example- 

Here, 'a' is declared as 32. 
We are checking whether the number is 30. 
The number is not equal to 30! hence 'else' set of statements is executed.

2) Nested-if 

If one condition satisfies. 
In that satisfied condition if you want to check another condition then we use 'nested if ' statements. 
first will look into the flow graph- 

Here, the first condition is checked.
if it is false, then statement 1 is executed.
If it is true, then the second condition is checked.
If it is true then statement 2 is executed.
Else statement 3 is executed.

Let's look at an example-

Here the first is condition satisfied hence the second condition is checked, even it satisfied, hence the following set of code is executed and produced "number is 32 and is positive".

Let's check for a non-positive number-

In the previous blog, we have seen that 'unsigned' is used to indicate a negative number. Click here to view.
The first condition failed and the statements are not executed.
The condition turns out to be false and the 'else' block is executed.

3)else-if 

If the first condition fails to satisfy then we check for another condition. 
The difference between else-if and nested if, will get to know if you look at the code and the flow graph- 

Observe the flow graph carefully! 
In nested if statement, if the condition was false we use to print some statement.
But in an else-if statement, if the condition is false we check for another condition.
This is the difference.

Let's look into code for still more clarity-

We all know the ASCII value of the alphabet 'A' is 65.
Var holds the ASCII value of 'A'
We are trying to compare that value with 66 first. Since it's not equal it won't print statement 1
Next, we try to compare it with 65. It's true hence at command prompt it prints the statement 2.

Sometimes, in the end, we put the default else statement. It up to the programmer if he/she wants to inform the user then use the default else statement.
 Let me explain the above two sentences with the help of code-

Here it's up to the programmer whether he/she wants to display the statement 3. If the default else statement is missed, the program will work fine!

4) switch- 

Switch statements are replacement for long else if statements. 
Let's look into the flow graph- 

Here, a few points are important to note- 
  • The default statement is optional. 
  •  It's important to mention break instruction. Otherwise subsequent cases are evaluated and are executed. 
Let's look into code-

Here, the code tries to explain about an ATM machine.
The choice you make get stored in 'n'.
Switch statement compares the 'n' value with the case label.
Once the case label matches, it executes the following set of instruction.
Here comes the important point to note- If break statement is not mentioned it goes on executing the subsequent instructions.

Facts about switch statement- 

  1. It won't allow adding duplicate case labels. That is you can't repeat the case label.
  2. In the switch statement condition, only those expressions are allowed which results in an integral constant value. 
  3. Float numbers are not allowed as case labels only integer constant/ constant expressions are allowed in case labels. Look at the example below it shoots an error. 
  4. You cant use variable names as a case labels. But you can use macro names as a case label 
  5. The default statement can be placed anywhere it will be evaluated if it won't find any matching case label.

Hope so, the conditional statements in C are cleared. If any doubts drop a comment.

Happy learning!












Comments

Popular post