Monday, 28 December 2020

Relational and Logical Operators, Decision or Selection Statements.

Objectives:

To familiarize students with Relational and logical operators and with usage of decision control statements such as if, else and switch statements. 

Tools:

Turbo C IDE 



logical operator

Procedure:

Write C programs which will perform the following
       Take integer number from keyboard as input and prints “I am greater than ten” if number is greater than ten
       Take two numbers from keyboard as input and subtract smaller number from larger
       Ask user an arithmetic operator (+, -, /, or *) and two operands and perform corresponding calculations on the operands
Relational Operators used in decision or selection statements. It compares status of one variable with another variable value as greater than, less than or equal to. Following is a list of relational operators:

Operator Name
Syntax
Less than or equal to
<=
Greater than
>
Less than
<
Greater than or equal to
>=
Equal to
==
Not equal to
!=

Logical Operators used in decision or selection statements and are three in number such as &&, ||, and ! read as AND, OR and NOTrespectively. The first two logical operators allow two or more conditions to be combined in if-statement. The following table shows logical operators:

Logical operators
Syntax
Logical AND
a && b
Logical OR
a || b
Logical Negation (NOT)
!a
 
if statement  is used to take decision about specific condition to execute. The body of if-statement is executed if value of the expression or condition specified in the if-statement is true. For example if there is raining outside, I will take umbrella so here condition is rain for which umbrella must be taken in case to go outside. 




Syntax of if-statement is as follows:

if (condition)
           {
                      Block of statements;
           }
if-else statement is similar to if-statement with an addition of else statement. If the condition is false in if-statement, then if-statement body will be skipped and else body will be executed. For example if there is raining outside, I will take an umbrella else I will go outside without an umbrella. So when first condition doesn’t satisfy then second condition will be followed. Syntax of if-else statement is as follow:

if (condition)
           {
                      Block of statements;
           }
         Else
           {
Block of statements;
            }
           
switch statement allows us to make a decision from a number of choices. The syntax for switch statement is as follows:

switch (integer expression)
{
     Case constant 1:
       Do this;
     Break;
                     Case constant 2:
       Do this;
      Break;
     Default;
       Do this;
     Break;
}
The integer expression can be any C expression that yields to integer value and could be an integer constant like 1, 2, 3 or an expression that evaluates to an integer value. case is keyword and must be followed by integer or character constant and constant in case must be different from all other constants.


Step 1: Create New C Program File and save it as lab4.c
Step 2: Write the following code as shown in figure below

program 1
 

Figure 13 (a): if-statement





Step 3: Save file again, compile and run it for required output as shown in figure below

output 1
 
Figure 13 (b): if-statement
Step 4:Over-write lab4.c with the following code as shown in figure below

program 2

 
Figure 14 (a): if-else statement
Step 5: Save the file again, compile and run to view the required output as shown in figure below

output 2
 

Figure 14 (b): if-else statement
output 3
 
Figure 14 (c): if-else Statement


Step 6: Over-write lab4.c with the following code as shown in figure below

2nd last code
 

Figure 15 (a): switch statement


Step 7: Save the file again, compile and run to view the required output as shown in figure below

  
last program code

Figure 15 (b): switch statement
second last output
 
Figure 15 (c): switch Statement
last output
 
Figure 15 (d): switch Statement

No comments:

Post a Comment