Decision making

1. Control statement:
• All the instructions are executed sequentially by default, when no repetitions of some calculations are necessary.
• In some situation, we may have to change the execution order of statements based on condition or to repeat set of the statement until certain conditions are met.

Tyes:
1. If statement
2. If – else statement
3. Nested if....else statement
5. Switch case
1. If statement:
It is used to check the condition and if true the condition then it executes.
Syntax:

if (condition) {           True statement; }

Example program for if:

#include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter a value:"); scanf("%d",&a); if (a>=5)     {           printf("The entered number %d is greater than 5",a);      } getch(); }

Output:
if condition:
False case:
2. If – else statement:
It is used to check both conditions i.e. true and false.
It executes only one condition at a time.
Syntax:

if (condition)     {           True statement      } else     {           false statement;      }

Example program for if-else:

#include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter a value:"); scanf("%d",&a); if (a>5)       {    printf("The entered number %d is greater than 5",a);        } else       {   printf("The entered number %d is less than 5",a);       } getch(); }

Output:
if condition:
else condition:
3. Nested if....else statement:
Multiple if-else conditions are validating i.e. within condition check first after that execute.
Syntax:

if(condition 1)    {           if(condition 2)               {                    True statement2;                }           else               {                    False statement2;                }    } else     {           False statement1;      }

Example program for nested if...else:

#include<stdio.h> #include<conio.h> void main() { int  a,b; clrscr(); printf("Enter a result:\n 1.pass\n 2.fail\n press 1 or 2\n"); scanf("%d",&a); if (a==1)      { printf("Enter total mark:\n"); scanf("%d",&b); if(b>450)    { printf("You can get first group i.e. maths group");     } else    { printf("You can get all group except first group");    }       } else     { printf("Passed studens only can join the 11th standard");      } getch(); }

Output:
if-if condition
if-if-else case:
else condition:
It is similar to nested if but it checks continually.
Syntax:

#include<stdio.h> #include<conio.h> void main() { int  a,b,c; clrscr(); printf("Enter 3 values: \n"); scanf("%d %d %d",&a,&b,&c); if( (a>b) &&(a>c))   { printf("Biggest value %d is a",a);   } else if(b>c)   { printf("Biggest value %d is b",b);    } else   { printf("Biggest value %d is c",c);    }      getch(); }

Output:
if condition:
else if  condition:
else condition:
5. Switch case:
It is used to execute a particular group of statement .
Syntax:

switch(expression)   {           case condition 1:           statement1;           break;           case condition 2:           statement2;           break;           .........           .........           default           statements           break;   }

Example program for switch case:

#include<stdio.h> #include<conio.h> void main() { int  choice; clrscr(); printf("Enter a choice 1 or 2\n"); scanf("%d",&choice); switch(choice)   { case 1: printf("You are chooses option 1 "); break; case 2: printf("You are chooses option 2 "); break; default: printf("You can only entered 1 or 2 choice "); break;   } getch(); }

Output:
Case 1:
Case 2:
Default case:
2. Branching and looping:
It is used to execute continually until the condition becomes false.
Types:
• While
• Do while
• For
1. While:
It first checks condition then execute result until become false.
Syntax:

while (condition) {           Statements; }

Example program for while condition:

#include<stdio.h> #include<conio.h> void main() { int  i=1,sum=0; clrscr(); while(i<=5)       { sum=sum+i; i++;       } printf("The sum of numbers upto 5 is %d",sum); getch(); }

Output:
2. do while:
It is first to perform some operation after that check the conditions.
It's opposite to the while condition.
Syntax:

do {           Statements; } while (condition);

Example program for do while condition:

#include<stdio.h> #include<conio.h> void main() { int  i=1,sum=0; clrscr(); do   { sum=sum+i; i++;   } while(i<=5); printf("The sum of numbers upto 5 is %d",sum); getch(); }

Output:
3. for loop:
It is used to execute a set of instructions repeatedly until the condition becomes false.
Syntax:

for(initialization ;condition ;increment or decrement) {           Statement; }

Example program for for loop:

Output: