0% found this document useful (0 votes)
122 views

Lab 4

This document discusses different types of decision making structures in C programming including if/else statements, switch statements, and goto statements. It provides examples of each type of statement and explains their usage and functionality. The objectives are to understand and use if/else constructs, switch statements, and goto statements. The document also includes lab tasks for students to practice writing programs using these conditional statements.

Uploaded by

radians042
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Lab 4

This document discusses different types of decision making structures in C programming including if/else statements, switch statements, and goto statements. It provides examples of each type of statement and explains their usage and functionality. The objectives are to understand and use if/else constructs, switch statements, and goto statements. The document also includes lab tasks for students to practice writing programs using these conditional statements.

Uploaded by

radians042
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 4 I f-else, Switch statements (6-3-2013)

1. Objectives
To have strong understanding of if/else, switch statement and goto keyword.

2. Outcome
After performing this lab, the students should be able to understand and use
if/else constructs
Switch statement
goto statement

3. Introduction
It is sometimes required to execute a particular portion of code only if certain condition is
true; or false. For said purpose three major decision making structures are used
if statement
if-else statement
switch statement.

3.1 If statement

The general form of if statement looks like this:

if ( this condition is true )
execute this statement ;

Condition expression can be written using relational and logical operators as shown below


Figure 1

Example:
main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ; }

Conditional expression can be any valid expression including a relational expression. We can
even use arithmetic expressions in the if statement. For example all the following if statements
are valid.

if ( 3 + 2 % 5 )
printf ( "This works" ) ;
if ( a = 10 )
printf ( "Even this works" ) ;
if ( -5 )
printf ( "Surprisingly even this works" ) ;


In C a non-zero value is considered to be true, whereas a 0 is considered to be false. In
the first if, the expression evaluates to 5 and since 5 is non-zero it is considered to be true.
Hence the printf( ) gets executed.
In the second if, 10 gets assigned to a so the if is now reduced to if ( a ) or if ( 10 ). Since
10 is non-zero, it is true hence again printf( ) goes to work.
In the third if, -5 is a non-zero number, hence true. So again printf( ) goes to work. In
place of -5 even if a float like 3.14 were used it would be considered to be true.

3.2 The if-else Statement

Often our programs will want to take one branch if your condition is true, another if it
is false. The keyword else can be used to perform this functionality.

if (expression)
{ statement/s; }
else
{ statement/s; }

Example: If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA =
90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and
DA = 98% of basic salary. If the employee's salary is input through the keyboard write
a program to find his gross salary.

/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}

With the if statement, the default scope of else is also the statement immediately after the
else. To override this default scope a pair of braces as shown in the above example must be
used.

3.3 Nested if-else

It is perfectly all right if we write an entire if-else construct within either the body of
the if statement or the body of an else statement. This is called nestingof ifs. This is
shown in the following program.

main( )
{
int i ;
printf ( "Enter either 1 or 2 " ) ;
scanf ( "%d", &i ) ;
if ( i == 1 )
printf ( "You would go to heaven !" ) ;
else
{
if ( i == 2 )
printf ( "Hell was created with you in
mind" ) ;
else
printf ( "How about mother earth !" ) ;
}
}

3.4 Switch Statement
The general form of the switch statement is:
switch (variable){
case constant1: statement sequence_1;
break;
case constant2: statement sequence_2;
break;
default: statement_sequence_default;
}

In a switch statement, a variable is successively tested against a list of integer or
character constants.
If a match is found, a statement or block of statements is executed.
The default part of the switch is executed if no matches are found.
The switch differs from if statements in such a way that switch can only test for
equality whereas if can evaluate a relational or logical expression.

Figure 2

No two case constants in the same switch can have identical values. Of course, a
switch statement enclosed by an outer switch may have case constants that are the
same.
If character constants are used in the switch, they are automatically converted to
their integer values. Shown below

Figure 3

Output = I am in case 2
I am in case x
The break statement is used to terminate the statement sequence associated with
each case constant. It is a C keyword which means that at that point of execution,
you should jump to the end of the switch statement terminated by the symbol }.
Even if there are multiple statements to be executed in each case there is no need
to enclose them within a pair of braces
Every statement in a switch must belong to some case or the other. If a statement
doesnt belong to any case the compiler wont report an error.
We can check the value of any expression in a switch. Thus the following switch
statements are legal.

switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )
You can in fact put the cases in any order you please. In principle, a switch may
occur within another, but in practice it is rarely done. Such statements would be
called nested switch statements.

Example: Write a program that will ask the user to input 2 integers and a character (A,S, M or
D). If the user inputs A for the character, add the 2 numbers, if Ssubtract the 2 numbers, if
M multiply, and if D divide the numbers. Output the c computed value.

#include<stdio.h>
main( )
{
int num1, num2, ans;
char operation;
printf(Enter number 1:);
scanf(%d, &num1);
printf(Enter number 2:);
scanf(%d, &num2);
printf(A Addition \n);
printf(S Subtraction \n);
printf(M Multiplication \n);
printf(D Division \n);
printf(Enter a character for the operation:);
scanf(%c, &operation);

switch (operation){
case A: ans = num1 + num2;
break;
case S : ans = num1 - num2;
break;
case M: ans = num1 * num2;
break;
case D: ans = num1 / num2;
break;}
printf(The answer is: %d, ans); getch( );
}


4. Lab Tasks
Execute the following programs to view the output.

i.
main( )
{
int i = 4, j = -1, k = 0, w, x, y, z ;
w = i || j || k ;
x = i && j && k ;
y = i || j && k ;
z = i && j || k ;
printf ( "\nw = %d x = %d y = %d z = %d", w, x, y,
z ) ;
}
ii.
main( )
{
int i = -3, j = 3 ;
if ( !i + !j * 1 )
printf ( "\nMassaro" ) ;
else
printf ( "\nBennarivo" ) ;
}
iii.
main( )
{
int x = 20 , y = 40 , z = 45 ;
if ( x > y && x > z )
printf( "x is big" ) ;
else if ( y > x && y > z )
printf( "y is big" ) ;
else if ( z > x && z > y )
printf( "z is big" ) ;
}


1. Write a program which takes a character input and checks whether it is vowel or
consonant
2. Write a program which takes three sides a, b and c of a triangle input and calculates its
area if these conditions are satisfied a+b>c, b+c>a, a+c>b (help a= vs(s-a)(s-b)(s-c),
where s=(a+b+c)/2
3. Write a program that will ask the user to input an integer then output the
equivalent day of the week. 1 is Sunday, 2 is Monday and so on. If the inputted
number is not within 1-7, output Day is not available!
4. Write a nested if statement to print the appropriate activity depending on the
value of a variable temperature and humidity as in the table below: Assume that
the temperature can only be warm and cold, and the humidity can only be dry
and humid.
if temperature
is
if humidity is print this
activity
Warm >= 35 Dry <50 "play tennis"
warm Humid >=50 "swim"
Cold <35 dry "play
basketball"
cold humid "watch TV"

You might also like