Flow of Control (If-else, Switch)

Ad Code

Flow of Control (If-else, Switch)

 

If statement is a conditional branching statement which checks the condition and reflects two logical values(i.e. True or False). After then perform the work accordingly. if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:
If Statement : Syntax.

1.  If(condition or expression)
     { 
                  Work;
      }

2.  If(condition or expression)
     {
           Work1;
     }
   else
   {
      Work2;
   }

First Example of if statement

#include<stdio.h>

#include<conio.h>

int main()

{

int  n;

clrscr();

printf("Enter a number ");

scanf("%d",&n);

if(n<10)

{

printf("Given number is single digited");

}

getch();

return 0;

}

Output

Enter number  7
Given number is single digited 

Second Example of if statement

#include<stdio.h>

#include<conio.h>

int main()

{

int  n;

clrscr();

printf("Enter a number ");

scanf("%d",&n);

if(n<10)

{

printf("Given number is single digited");

}

else

{

printf("Given number is multiple digited");

}

getch();

return 0;

}

Output

Enter number  17
Given number is multiple digited 

Difference between '/' and '%' operator in C
% is a modulous operator , It give remainder of two numbers on division as result and discard quotient and gives an integer
eg. int a=5%2=1
=> 5/2 => 5-(2*2) => 5-4 = 1, remainder is 1.
/ is a division operator , it give quotient of two numbers on division as result and gives a floating point number.
=> 5/2 => 5-(2*2) =>5-4 = 1 => 1/2= 0.5 . so quotient is 2.5
On integer type casting Result will be 2.
Print Even or Odd of given number.

#include<stdio.h>

#include<conio.h>

int main()

{

int n=0;

printf("enter the Number-->");

scanf("%d",&n);

if(n%2==0)

{

printf("%d is Even number",n);

}

else

{

      printf("%d is Odd number",n);

}

getch();

return 0;

}

Output

Enter number  5
5 is odd number 
         or
Enter number  6
6 is even number 


Nested if...else statement (if...elseif....else Statement)
When you use if within if is called nested if. The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
Syntax of nested if...else statement.

if (testExpression1) 
{
    work if testExpression1 is true
}
else if(testExpression2) 
{
   work if testExpression2 is trueis true
}
else if (testExpression 3) 
{
   work if testExpression3 is true
}
.
.
else 
{
   work if all test expressions are false
}

Example: Nested if...else statement


#include <stdio.h>
int main()
{
    int n,m;
    printf("Enter two integers: ");
    scanf("%d %d", &n, &m);

    //checks if two integers are equal.
    if(n == m)
    {
        printf("Result: %d = %d",n,m);
    }
//checks if n is greater than m.
    else if (n > m)
    {
        printf("Result: %d > %d", n,m);
    }

    // if both test expression is false
    else
    {
        printf("Result: %d < %d",n,m);
    }

    return 0;
}


Output

Enter two integers: 22
33
Result: 22 < 33

Switch Statement :
Switch statement is usee for multiple if statement. When the switch statement is executed, the expression in the switch statement is evaluated and the control is transferred directly to the group of statements whose case label value matches the value of the expression. Each group of statement ends with a break statement. The execution of break statement causes immediate exit from the switch statement. If break statement is not present, then the execution will falls trough all the statement in the selected case. If none of the case-label value matches the value of expression, then none of the group within the switch statement will be selected and the control is transferred directly to the statement that follows the switch statement in c.
Syntax: Switch case


switch(expression)
{

case constant1:

statements 1;

break;

case constant2:

statements 2;

break;
................................

default:

statements n;

break;

}



Example: Switch case


#include <stdio.h>
void main()
{
 char o;
 float n1,n2;
 printf("\nSelect Operator");
 scanf("%c",&o);
 printf("\nEnter two numbers:");
 scanf("%f%f",&n1,&n2); 
 switch(o)
 {
 case '+':
 printf("%.1f+%.1f=%.1f",n1,n2,n1+n2);
break;
case '-':
 printf("%.1f-%.1f=%.1f",n1,n2,n1-n2);
break;
case '*':
 printf("%.1f*%.1f=%.1f",n1,n2,n1*n2);
break;
case '/':
 printf("%.1f/%.1f=%.1f",n1,n2,n1/n2);
break;
default:
printf("\nError");
break;
}

}


Conditional or Ternary Operator:
Conditional operators return one value if condition is true and returns another value if condition is false. This operator is also called as ternary operator.
Syntax: (Condition?true_value:false_value); Example : (A > 100 ? 0 : 1);
Example:


#include <stdio.h>
int main()
{
    int a,b,s=0;
   printf("\nEnter two numbers");
   scanf("%d %d",&a,&b);
   s=a>b?a:b;
  printf("\nHighest=%d",s);
  getch();
  return 0;
}


Post a Comment

0 Comments

Ad Code