If CONDITIONAL STATEMENT

What are Case control instructions ?

We need to make decision in every aspect of life , like Whether to buy android or Iphone , watch television or play PUBG , support T-Series or Pewdiepie . Like this we sometime need to take decision in programming world for performing specific operation . For making decision in programming languages we make use of control instruction . In C language these are basically if-else and switch statement.

-> IF-ELSE STATEMENT

This is most common statement for making decision in C . It uses Keywords if and else .
Syntax of if-else statement is
                    if(condition)
                     do this1;
                    else
                     do this2;
Note that condition is enclosed in braces and no semicolon is there to terminate the statement . If condition enclosed is true than do this1 statement execute and if condn is false than statement 2 will execute.
Let’s understand it with an example -:
The relational operator are used to compare the condition enclosed in if statement.

x==y         x is equal to y.
x!=y          x is not equal to y.
x>y           x is greater than y.
x<y           x is smaller than y.
x>=y         x is greater than or equal to y.
x<=y         x is smaller than or equal to y.

We will study these operators in our upcoming examples.

Program -: Program to determine whether no is odd or even -:
Coding-:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf(“Enter the value of number\n”);
scanf(“%d”,&n);
if(n%2==0)
printf(“no is even\n”);
else
printf(“no is odd\n”);
getch();
}
Here conditional statement is if(n%2==0) , this expression tells if number entered is divisible by 2 and its remainder is zero. If this condition is true than statement no is even get print else no is odd.

The condition enclosed itself can be a valid expression . For example-:

a.)    if(5-8*2)

printf(“this is valid\n”);
b.)  if(x==10)
printf(“this is valid\n”);
c.)  if(-80)
printf(“this is valid\n”);

NOTE-: Many students here make mistake in comparing if(n%2==0). Here clear difference between if(n%2==0) and if(n%2=0).
When you write (n%2=0) you are not comparing , you are assigning value 0 remainder that will come after division . So in every case you have remainder 0 which will become wrong . So be clear about this point .

MULTIPLE STATEMENT WITHIN IF-ELSE

It can happen sometime that we want multiple statement to be executed while using if statement . For this we have to use pair of curly braces. Here we also use logical operators like &&,|| and ! which are AND , OR , NOT .
For example-:
a.) if(this1 && this2)  means both condition is true than compiler will enter if                                      block.
b.) if(this1 || this2)    means this1 or this2 anyone condition is true than                                            compiler will enter if block.
c.) if(this1!=this2)     means this1 is not equal to this2 than compiler will enter                                  if block . For example-:
Program -: Let’s take input of numbers scored by two students x and y in a test . If marks scored are above 30 then print x is passed and y is    passed else x failed and y failed.
Coding-:
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int m1,m2;
printf(“Enter marks of x and y\n”);
scanf(“%d%d”,&m1,&m2);
if(m1>30 && m2>30)
 {
    printf(“x is passed\n”);
    printf(“y is passed\n”);
 }
else
 {
    printf(“x failed\n”);
    printf(“y failed\n”);
 }
getch();
}
 
 
If we will not use braces it will show error and by default exactly next statement after if condition go under compilation . Braces tell compiler that you want whole statement to run if condition is true.

Nesting of if-else statement

Here we can use one if condition in another if condition means we nest multiple if conditions in each other . This is called nested if statements . Let’s take an example-:
Program-: WAP to take input of integer number from user and print
                    a.) You entered one , if user enter 1.
                    b.) You entered two , if user enter 2.
                    c.) You entered a different number , if any other number is entered.
 
Coding-:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf(“Enter the number\n”);
scanf(“%d”,&n);
if(n==1)
      printf(“You entered one\n”);
else
  {
       if(n==2)
          printf(“You entered two\n”);
       else
           printf(“You entered a different number\n”);
  }
getch();
}
How it worked-:
Here compiler check line if(n==1) , if this condition is true than compiler enters in if block and print You entered one . If this condition is false than compiler enters in else block and check if n is equal to 2 , if yes than it print You entered two. If (n==2) is false than compiler jump to else statement and print You entered a different number .
These programs just need to be practised and look the flow of compiling means how it works . Look carefully and understand meaning of each line.

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started