C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Enter num1:3

Enter num2:0

Enter num3:-5

Enter num4:2

product=-30

C Programming switch....case Statement

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. If a programmar has to choose one among many alternatives if...else can be used but, this makes programming logic complex. This type of problem can be handled in C programming using switch...case statement.

Syntax of switch...case

switch (expression)

{

case constant1:

codes to be executed if expression equals to constant1;

break;

case constant2:

codes to be executed if expression equals to constant3;

break;

.

.

.

default:

codes to be executed if expression doesn't match to any cases;

}

In switch...case, expression is either an integer or a character. If the value of switch expression matches any of the constant in case, the relevant codes are executed and control moves out of the switch...case statement. If the expression doesn't matches any of the constant in case, then the default statement is executed.

index-33_1.jpg

Example of switch...case statement

Write a program that asks user an arithmetic operator('+','-','*' or '/') and two operands and perform the