I.C.S Part 2 Computer Sciences

Chapter#1          Chapter#10          Chapter#11          Chapter#12
Chapter#11
Decision Constructors

Control Structure:-
It is a group of statements which is used to control flow of execution of the program.
Every program has some kinds of control structures.
It is also used to group multiple statements as single unit.
There are Three kinds of control structure.
i. Sequence
ii. Selection/Conditional
iii. Loop/Repetition/Iteration
Sequence:-
In this control structure, statements are executed in the order in which they are written.
It is the default control structure of every program.
Selection/Conditional:-
In this control structure, statements are executed on the basis of some conditions.
Loop/Repetition/Iteration:-
In this control structure, statements are executed many times upto a fixed number of time or until given condition is satisfied.

Loop/Repetition/Iteration:-
In this control structure, statements are executed up to a fixed number of times or until given condition is satisfied.

Selection Control Structure:-

In this control structure, statements are executed on the basis of some conditions.
Condition:-
It is an expression whose outcome is either true or false.
Following are the Examples of Condition Statements.
i. If-statement
ii. If-else
iii. If-else-if
iv. Nested-if-statement
v. Conditional Operator
vi. Switch Statement
i. If-statement:-
It is simplest conditional statement.
It consists of one condition and one block.
If the condition is true, block will be executed.
If the condition is false, control is transferred to the statement which comes after the block.
Syntax of if-statement:-
if(Condition)
{
....................
....................
} (block)

Program of if-statement:-

#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf("\n\n\tEnter The Marks");
scanf("%d",&marks);
if(marks>=50)
{
printf("\n\t\tStudent is Pass");
}
getch();
}


ii. If-else-statement:-
Ø It is a conditional statement.
Ø Two keywords are used in it.
 i. if
 ii. else
Ø This statement consists of one condition and two blocks.
Ø If the condition is true, First block will be executed and second block will be ignored.
Ø If the condition is false, Second block will be executed and first block will be ignored.
Ø In either case one block is executed and the other is ignored.
Ø This statement is used when we have two possibilities of outcomes.
Syntax of if-else-statement:-
if(Condition)
{
....................
....................
ç (block1)
else
{
...................
...................
}ç (block2)

Program of if-else-statement:-

#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf("\n\n\tEnter The Marks");
scanf("%d",&marks);
if(marks>=50)
{
printf("\n\t\tStudent is Pass");
}
else
{
printf("\n\t\t\tStudent is Fail");
}
getch();
}



iii.If-else-if statement:-

It is a conditional statement.
Two keywords are used in it.
i. if
ii. else
It consists of multiple conditions and multiple blocks.
Each condition has its only one block to execute.
All the conditions are evaluated in sequence.
If the condition is true, Its respective blocks will be executed and all remaining blocks are ignored.
If the condition is false, Its respective block will be ignored and control will be transferred to the next condition.
Variable of any data type can be used in the condition of this statement.
else:-
Use of the else in the if-else-if statement is optional.
It is written at the last of this statement.
Block under the else will be executed when all conditions become false.
          Syntax of if-else-if statement:-
If(condition)
{



} (block 1)

else if(condition 2)
{



} (block 2)

else-if(condition 3)
{



} (block 3)

‘’’’’’’’’up to so on
else
{



} (Last block)


  Program of if-else-if statement:-

#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf("\n\tEnter the Marks...");
scanf("%d",&marks);
if(marks>=90)
{
printf("\n\n\tGrade is A");
}
else if(marks>=80)
{
printf("\n\n\tGrade is B");
}
else if(marks>=70)
{
printf("\n\n\tGrade is C");
}
else
{
printf("\n\n\n\tStudent is Fail");
}
getch();
}

iv.Nested if-statement:-
It is a conditional statement.
If-statement within the body of another if-statement, is called nested if-statement.
Nesting can be perform up to any level.
Nesting increase the complexity of the program.
This statement is used for multiway decision making.
For the successful running of the program, it is necessary to manage true/false outcomes of all conditions.
                Syntax of Nested if statement:-
If(condition)
{
      If(condition)
         {
           If(condition)
        {
         
            }  up to so on as you require
         }
   }

Program of Nested if-statement:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf("\n\n\tEnter Three Numbers....");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
max=a;
else
max=c;
}
else if(b>c)
max=b;
else
max=c;
printf("Maximum Number is %d",max);
getch();
}



v.Conditional Operator:-

It is also a conditional statement.
It is an alternative of if-else statement.
It is also called Ternary Operator.
It consist of Three Expressions.
i. Expression 1
ii. Expression 2
iii. Expression 3
Expression 1:-
It is a logical condition whose outcome is either true or false(0,1).
Expression 2:-
If the condition is true,expression 2 is executed.
Expression 2 is written after the ‘?’
When expression 2 is executed, expression 3 is ignored.
Expression 3:-
If the condition is false, expression 3 is executed.
Expression 3 is written after the colon ‘:’
When expression 3 is executed, expression 2 is ignored.
Syntax of Conditional Operator:-
Expression 1? Expression 2: Expression 3
Program of Conditional Operator:-
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf("\n\n\tEnter The Marks...");
scanf("%d",&marks);
marks>=50?printf("\n\n\t\tStudent is PASS"):printf("\n\nStudent is Fail");
getch();
}

Switch Statement:-
It is a conditional statement.
It is an alternative of if-else-if.
Four Keywords are used in it.
i. switch
ii. case
iii. break
iv. default
In this statement we choose only one option out of few.
In this statement, value of switch expression is compared with values of all cases.
Only integer or character data type can be used in switch expression.
If we use float or double data type in switch expression, compiler will generate an error.
i. switch:-
It is a keyword which represents start of this statement.
It consists of an expression or a value which is compared with all the values of cases.
This value can be of only character or integer data type.
It also contains a body which starts with ‘{‘ and ends with ‘}’ .
ii. case:-
It is another keyword which is written within the body of switch.
One switch contains multiple cases.
Value of case is matched with expression or value of switch.
If value of switch matches the value of case, its statement will be executed.
If the values do not match, control is transferred to the next case in sequence.
Last statement of each case is break statement.
If two cases are written without having expression between them, compiler consider ‘OR’ operation between them.
iii.  break:-
This statement is used to exit from the current body.
If breaks are omitted from the program, all the other statements will also be run even if it matches any case.
iv.  default:-
It is just like else.
It is an optional part of switch statement.
Statements under default are executed when all the cases become mismatched.
Its position is not fixed. It can be written any where in the switch statement.
Syntax of Switch Statement:-
switch (Expression or Value)
{
case-I:


break;

case-II:


break; …….. up to so on

default

}

Program of Conditional Operator:-
#include<stdio.h>
#include<conio.h>
void main()
{
float a;
int ch;
clrscr();
printf("\n\nEnter Any Number....");
scanf("%f",&a);
printf("\nEnter 1 for Square");
printf("\nEnter 2 for Cube");
printf("\nEnter 3 for Square Root");
printf("\nNow Please Press 1 , 2 or 3\n\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\tThe Result = %.2f",a*a);
break;
case 2:
printf("\tThe Result = %.2f",a*a*a);
break;
case 3:
printf("\tThe Result =%.2f",sqrt(a));
break;
default:
printf("\n\t\tChoice is Invalid\n\tPlease Enter Valid Value");
}
getch();
}

0 comments:

Post a Comment