Question

In: Computer Science

Analysis and Discussion about if, if else, else if statement and Switch C programming write on...

Analysis and Discussion about if, if else, else if statement and Switch C programming write on your computer font

Solutions

Expert Solution

IF Statement

If statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of If. The syntax for if statement is as follows:

if (condition) 
     statements;
Example Program
#include<stdio.h>
int main()
{       int n=1;
        int m=2;
        if(n<m)              //test-condition
        {
                printf("n is smaller than m");
        }
        return 0;
}

Output

n is smaller than m

IF Else Statement

Syntax is as follows.

if (test-expression)
{
    True block of statements
}
Else
{
    False block of statements
}
Statements;

If test-expression is true, if block is executed. Otherwise else block is executed.

Example Program

#include<stdio.h>

int main()

{ int n=1;

int m=2;

if(n<m) //test-condition

{

printf("n is smaller than m");

}

else

{

printf("m is smaller than n");

}

return 0;

}

Output

n is smaller than m

IF ELSE IF Statement

The syntax is as follows.

if (test - expression 1) {
    statement1;
} else if (test - expression 2) {
    Statement2;
} else if (test - expression 3) {
    Statement3;
} else if (test - expression n) {
    Statement n;
} else {
    default;
}

The test-expressions are evaluated from top to bottom. Whenever a true test-expression if found, statement associated with it is executed. When all the n test-expressions becomes false, then the default else statement is executed.

Example Program

#include<stdio.h>
int main()
{
        int marks=40;
        if(marks>75){
                printf("First class");
        }
        else if(marks>65){
                printf("Second class");
        }
        else if(marks>55){
                printf("Third class");
        }
        else{
                printf("Failed");
        }
        return 0;
}

Output

Failed

Switch Statement

The switch statement in C is an alternate to if-else-if statement which allows us to execute multiple operations for the different possible values of a single variable called switch variable.

Syntax is as follows.

switch(expression){    

case value1:    

//code to be executed;    

break;  //optional  

case value2:    

//code to be executed;    

break;  //optional  

......    

default:     

code to be executed if all cases are not matched;    

}    

Example Program

#include<stdio.h>  

int main(){    

int number=40;     

switch(number){    

case 10:    

printf("number is equals to 10");    

break;    

case 50:    

printf("number is equal to 50");    

break;    

case 100:    

printf("number is equal to 100");    

break;    

default:    

printf("number is not equal to 10, 50 or 100");    

}    

return 0;  

}    

Output

number is not equal to 10, 50 or 100

Since number is 40 and does not match with any case of the switch statement, the default value of the case will be printed.


Related Solutions

write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
As it relates to C#: Though the function of switch case and else if ladder is...
As it relates to C#: Though the function of switch case and else if ladder is same, there are a number of  difference between switch case and else if ladder, Explain the differences in two areas such (memory consumption, speed of processing, variable requirement) etc.
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); 2. Write a Constructor for the TrafficLight class that sets stopLight value to “red”, waitLight to “yellow” and goLight to “green”?
WRITTEN IN C PLEASE. Write a switch statement that tests the value of the char variable...
WRITTEN IN C PLEASE. Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y, the message Your request is being processed is printed if response is n, the message Thank you anyway for your consideration is printed if response is h, the message Sorry, no help is currently available is printed for any other value of response, the message Invalid entry; please try again is printed
c++ class Topics Branches if statement if/else statement Description Write a program that will determine whether...
c++ class Topics Branches if statement if/else statement Description Write a program that will determine whether the user passed or failed a test. The program will ask the user to input the following: Maximum Test Score - the maximum total scores in the test. Percent Pass Score - the minimum percent scores required to pass the test. User Test Score - the actual scores obtained by the user in the test. From the above input values, the program will compute...
With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message based on the logic below. A buyer can pay immediately or be billed. If paid immediately, then display "a 5% discount is applied." If billed, then display "a 2% discount is applied" if it is paid in 30 days. If between 30 and 60 days, display "there is no discount." If over 60 days, then display "a 3% surcharge is added to the bill."...
C language <stdio.h> (Decisions only) (else if and Switch statements) A bank wants an application to...
C language <stdio.h> (Decisions only) (else if and Switch statements) A bank wants an application to help in the loan approval process and has asked us to create it for them. Write a program to ask the user for the loan amount, the annual interest rate (as a percentage, not as a decimal), the length of the loan (in years), and the gross monthly income of the applicant. Validate the inputs per below, and if invalid, inform the user and...
Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT