Thursday, September 26, 2013

SELECTION STRUCTURE (CHOICES AND DECISIONS)


SELECTION STRUCTURE (CHOICES AND DECISIONS)

Comparison of selection and sequential structure in C programming:
Selection Structure allows a choice among various alternatives. Selection structure is different than that of sequential structure. A program that executes the statements one by one until the defined end, this type of program is called sequential structure. The functionality of this type of program is limited since it flows in single direction. Now we will be looking at one of the most powerful programming feature - the ability to change the flow of program execution. It is achieve by establishing the truth or falsity of an expression (condition). This type of program is called selection structure.
Explanation of Selection Structure: We all make different decisions in the face of changing circumstances. For example, if my friend is at home, then I will visit him. If the weather is good tomorrow, I will go for picnic. Computer Languages are also able to perform different tasks depending on the circumstances.
Selection structure is extensively used in programming because it allows the program to decide an action based upon user's input or other processes. The application of selection structure can be seen at systems that have password checking such as ATM Machine, handphone and etc. The system allows user access only if the user input password is the same as the corresponding password stored in the system. 

C uses the following statements to implement choices and decisions in programs.
  1.      The if statement
  2.      The if-else statement
  3.      The else-if statement
  4.      The switch statement
  5.      The conditional operator
Meaning of True and False in Programming:
Before we go further into the selection structure, you need to know the meaning of True and False in programming. An expression that evaluates to non-zero (1) is considered true expression while false expression evaluates to zero (0).


THE if STATEMENT IN C PROGRAMMING LANGUAGE



Here, if is the keyword in C. 


SYNTAX:

if (condition)
{
  block of statements to be executed
}

When this statement is executed, the condition is evaluated first. If the condition is true then the block of statements within the braces will be executed.
Note: The block of statements to be executed must be enclosed in opening and closing braces. If only one statement needs to be executed then braces need not be used. Let us understand if statement using an example.
FLOW CHART:


Example # 1:
Let’s look at an example that implement if selection structure. The program asks the user to input a number. Then it message if the number is a positive number.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num;
printf(“Enter a number:”);
scanf(“%d”,&num);
if(num>0)
printf(“\n %d is a positive number”, num);
getch();
}
Sample output:

           Enter a number: 6
           6 is a positive number


Explanation:

int num;
printf(“Enter a number:”);
scanf(“%d”,&num);

We declare a variable called num and then we prompt the user to enter a number.The scanf statement reads the user input and store it in variable num.

            if(num>0)
printf(“\n %d is a positive number”, num);

The if statement compare the value in num with the value of 0. If num is greater than 0, the printf() will be executed which displays a message and continue with the next part of the program. If it is not greater than 0, the printf() will be skipped. 


Example #2 :
/*Demonstration of if statement*/

int marks;
printf(“Enter the marks:”);
scanf(“%d”, &marks);
if(marks>=35){
printf(“Congrats!!!”);

getch( );
}
In the above program, it will prompt you to enter the marks. If you enter marks greater than or equal to 35, it will display Congrats!!! on the screen else it will do nothing.
***************************************************************************************************************************************************************************************************************************************************

THE if -else STATEMENT IN C PROGRAMMING LANGUAGE

Here, if and else are the keywords in C. 

SYNTAX:

if (condition)
{
  block of statements to be executed
}
else
{
block of statements to be executed
}


When this statement is executed, the condition is evaluated first. The if-else statement allows one action to be executed if the condition is true and another action to be executed if the condition is false. If the condition is true then the if block of statements will be executed, if the condition is false the body of else will be executed.

Note: The block of statements to be executed must be enclosed in opening and closing braces. If only one statement needs to be executed then braces need not be used. Let us understand if statement using an example.
FLOW CHART:


Example #1:
Let’s look at an example that implements if-else selection structure.  The following will find out large number from given input:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
        int x,y;
        printf("Enter value for x :");
        scanf("%d",&x);
        printf("Enter value for y :");
        scanf("%d",&y);
        if ( x > y ){
                printf("X is large number - %d\n",x);
        }
        else{
                printf("Y is large number - %d\n",y);
        }
getch( );
}

Example # 2:
The following program prompts the user to enter a character, if its is vowel a msg "Character is vowel" will be displayed, otherwise the message "character is consonant" will be displayed on the screen.

#include<stdio.h>
#include<conio.h>
void main()
{

clrscr( );
char ch;
printf("enter the character");
scanf("%c",&ch);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("character is vowel");
else
printf("character is consonant");
getch();
}


Example # 3:
The following programs checks that either the input number is even or odd by using if-else statment.

#include<stdio.h>

#include<conio.h>
void main()
{
int n;
clrscr();
printf(“enter the number:”);
scanf(“%d”,&n);
if (n%2==0)
printf(“the number is even”);
else
printf(“the number is odd”);
getch;
}


Example # 4:
The following program read the student name, section and grade. If the grade is greater than 75,  the student ispassed otherwise the student is failed.

#include<stdio.h>
#include<conio.h>
void main()
{
int grade;
char student[25],section[25];
clrscr();
printf(“Enter the name of a student: “);
scanf(“%s”, &student);
printf(“Enter the section of a student: “);
scanf(“%s”, &section);
printf(“Enter the grade of a student: “);
scanf(“%d”, &grade);
if(grade>75)
printf(“PASSED”);
else
print(“FAILED”);

getch();
}

Example # 5:
The following program prompts the user to enter age, if the age is less than 80, it will displayed the message "can't vote" otherwise the message " Can vote" would be displayed.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int age;
printf(“Enter the age:”);
scanf(“%d”, &a);
if(a<18)
printf("can't vote");
else
printf("can vote");
getch();
}

Example #6:
The following program 
#include<stdio.h>
#include<conio.h>
void main()
{
int grade;
clrscr();
printf(“Enter Grade:\n”);
scanf(“%d”, &grade);
if((grade>=80)&&(grade=75)&&(grade<=79))
printf("Nice");
else
printf("Poor");
getch( );
}

**************************************************************************************************************************************************************************************************************************************************

THE else-if STATEMENT IN C PROGRAMMING LANGUAGE

 If we have long decision tree, then the else-if statement is used.
SYNTAX:
The syntax of else-if statement in C programming language is:
if(boolean_expression 1)
{
   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
   /* Executes when the boolean expression 3 is true */
}
else
{
   /* executes when the none of the above condition is true */
}

FLOWCHART:

EXAMPLE:
 Cprogram by using if else statement to print smallest of given four numbers.
#include<stdio.h>
#include<conio.h>
void main()
{

clrscr( );
int a=10,b=20,c=30,d=40;
if (a<b && a<c && a< d)
printf("A is smallest");
else if (b<a &&b<c &&b<d)
printf(" b is smallest );
else if (c <a && c<b && c <d)
printf("c is smallest");
else
printf("D is smallest");
getch()
}

EXAMPLE: 
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr( );
int x;
printf(“enter any number between 1 to 12″);
scanf(“%d”,&x);
if(x==1)
{
printf(“month is jan. %d\n”,x);
}
else if(x==2)
{
printf(“month is feb%d\n”,x);
}
else if(x==3)
{
printf(“month is march .%d\n”,x);
}
else if(x==4)
{
printf(“month is april %d /n”,x);
}
else if……………….
…………………………………….
………………..
………………………
…………………………….
else(x==12)
{
printf(“month is december %d\n”);
}
getch();
}
************************************************************************************************************************************************************************************************************************************************

No comments:

Post a Comment