knowledgediarybd.com

Your Virtual Knowledge Campus

If,else if, Else & Switch Statement in C

J4If,else if, Else & Switch Statement In C are  the most important in c programming. Mainly they are used for checking one or more  conditional operation like grater than, less than or equal e.t.c. logical condition.

These statement provides a easiest way to solve a problem in a very short time & increase the performance of the c program code.

Dear learner, here you can learn how to use if, else, else if, and switch statement in c that will satisfy your

interest to learn c language.

Decision making statements in C:

There are Four types of decision making statements in C. These are:

1. if statement

2. if else statement

3. if else if ..else  statement.

4. switch statement

1. if statement:

you can Use it when  you need to check a specific condition is true.

Syntax of  if statement :

if ( condition)

code to be executed here if condition is true;

For Example:

#include<stdio.h>

int main()
{
int x=7;

if(x<10)
printf("The if condition is true");

return 0;
}

Out put will be:The if condition is true.

2. if else statement:

Use it when  you need to check  one  condition but result may be two Like: yes or No

Syntax of if else statement:

if ( condition )

  {

A Statement to be executed if condition is true;

}

else

  {

  A Statement to be executed if condition is false;

}

For Example:

#include<stdio.h>

int main()
{
int x=13;

if(x<10)
{
printf("The if condition is true");
} else {
printf("The if condition is false");
} 
return 0; }

3. if else if ..else  statement.

Use it when  you will need to check multiple condition statement.

Syntax  of if else if ..else  statement :

if ( condition )

  {

First statement executed if condition is true;

}

elseif ( condition )

  {

Second statement executed if condition is true;

}

else

  {

Final statement executed if all above condition is false;

}

For Example:

#include<iostream>
using namespace std;
int main()
{
int x=10;

if(x<10)
{
printf("The value of x is :%d",x);
}
else if (x>10)
{
printf("The value of x is :%d",x);
} else //it is last else & here no need to check any condition.
printf("The value of x is :%d",x);
return 0; }

4. switch statement

switch statement allows a variable to be tested for equality against a number of values. Each value is called a case, and the variable being switched on is checked for every case.

Syntax  of switch statement :

switch statement
 Example of switch statement:

#include<stdio.h>

int main()
{
int marks;
printf(" Enter your marks:");
scanf("%d",&marks);
switch(marks/5)
{
case 20:
case 19:
case 18:
case 17:

case 16: printf(“Your Gread is: A+”);break;

case 15: printf(“Your Gread is: A”);break;

case 14: printf(“Your Gread is: A-“);break;

case 13: printf(“Your Gread is: B+”);break;

case 12: printf(“Your Gread is: B”);break;

case 11: printf(“Your Gread is: B-“);break;

case 10: printf(“Your Gread is: C+”);break;

case 9: printf(“Your Gread is: C”);break;

case 8: printf(“Your Gread is: D”);break;

default: printf(“Your Gread is: F”);break;

 } return 0; }

#A simple game program : Predict  my number

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void) {
int me = 0;
int you = 0;
srand(time(NULL));
me = 1 + rand() % 10;
printf(“Prediction my number: 1 to 10: “);
scanf(“%d”, &you);
if(you < me)
printf(“Your Prediction was to low\n”);

else if(you > me)
printf(“Your Prediction was to high\n”);

else
printf(“Yesss … you got it!\n”);

printf(“The correct number was: %d\n”, me);

return 0;
}

More C programs with if …else if… and else 

Note:More interested things are waiting to publish..Stay connected to us..

——————-:Thank you:—————–

Related Post or you may felt interest:

knowledgediarybd.com © 2017
shares