Finance[US] Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
1.5k views
in JECRC University B.Tech(CSE-II Sem) Computer Programming Lab by Goeduhub's Expert (7.6k points)
recategorized by
Program to implement If , If-else and Switch statement using C programming

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Implementation of  If , If-else and Switch Statement 

If statement :  If statement allows the compiler to test the condition first, and then, depending upon the result, it will execute the statements. If the test condition is true, then only statements within the if statement performed by the C compiler.

Syntax :

if (test condition)
{

  Statement 1;
  Statement 2;
  Statement 3;
  ………….
  Statement n;
}

Example : 

#include<stdio.h>
#include<conio.h>
void main(){
int i=10;
if(i<=10){
printf(“value of i is less than or equal to 10”);
}
}

Output : value of i is less than or equal to 10


If-else statement : 

If the test expression is evaluated to true,

  • statements inside the body of if are executed.
  • statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,

  • statements inside the body of else are executed
  • statements inside the body of if are skipped from execution

 Syntax : 

if (condition) {

// execute if the test expression is true

}

else {

// execute if the test expression is false

}

Example : 

#include<stdio.h>

#include<conio.h>

void main() {

    int i;

    printf("Enter value : ");

    scanf("%d", &i);

    if  (i%2 == 0) {

        printf("%d is even .",i);

    }

    else {

        printf("%d is odd .",i);

    }    

}

Output : 

Enter value : 5

5 is odd .


Switch statement :  switch statement is used when you have multiple possibilities for the if statement. Switch case will allow you to choose from multiple options.the switch case allows you to set the necessary statements for the user.At the end of each block it is necessary to insert a break statement because if the programmers do not use the break statement, all consecutive blocks of codes will get executed from every case onwards after matching the case block.

Syntax : 

switch(variable) 

case 1: 

      //execute code 

break; 

case n: 

      //execute code 

break; 

default: 

     //execute  code 

break; 

}

Example : 

 

#include<stdio.h>

#include<conio.h>

void main()

{

    int a;

    printf("Please enter a no numbers from 1 to 7 : ");

    scanf("%d",&a);

    switch(a)

    {

    case 1:

    printf("Its Sunday");

    break; 

    case 2:

    printf("Its Monday");

    break;

    case 3:

    printf("Its Tuesday");

    break;

    case 4:

    printf("its Wednesday");

    break;

    case 5:

    printf("Its thursday");

    break;  

    case 6:

    printf("Its Friday");

    break;

    case 7:

    printf("Its Saturday");

    break;

    default :

    printf("YOU HAVE ENTERED WRONG NUMBER");

    break;

    }

}

Output : 

Please enter a no numbers from 1 to 7 : 8

YOU HAVE ENTERED WRONG NUMBER


For more JECRC University II Sem Computer Programming Lab  Experiments CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...