Decision making in C
Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements,- if statement
- switch statement
- goto statement
Decision making with if statement
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,- Simple if statement
- If....else statement
- Nested if....else statement
- else if statement
Simple if statement
The general form of a simple if statement is,if( expression ) { statement inside; } statement outside;If the expression is true, then 'statement-inside' it will be executed, otherwise 'statement-inside' is skipped and only 'statement-outside' is executed.
Example :
#include <stdio.h> void main( ) { int x,y; x=15; y=13; if (x > y ) { printf("x is greater than y"); } }Output
x is greater than y
if...else statement
The general form of a simple if...else statement is,if( expression ) { statement block1; } else { statement block2; }If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped and 'statement-block2' is executed.
Example :
#include <stdio.h> void main( ) { int x,y; x=15; y=18; if (x > y ) { printf("x is greater than y"); } else { printf("y is greater than x"); } }Output
y is greater than x
Nested if....else statement
The general form of a nested if...else statement is,if( expression ) { if( expression1 ) { statement block1; } else { statement block2; } } else { statement block3; }if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression 1' . If the 'expression 1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.
Example :
#include <stdio.h> #include <conio.h> void main( ) { int a,b,c; clrscr(); printf("enter 3 number"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if( a > c) { printf("a is greatest"); } else { printf("c is greatest"); } } else { if( b> c) { printf("b is greatest"); } else { printf("c is greatest"); } } getch(); }
else-if ladder
The general form of else-if ladder is,if(expression1) { statement block1; } else if(expression2) { statement block2; } else if(expression3 ) { statement block3; } else default statement;The expression is tested from the top(of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.
Example :
#include <stdio.h> #include <conio.h> void main( ) { int a; printf("enter a number"); scanf("%d",&a); if( a%5==0 && a%8==0) { printf("divisible by both 5 and 8"); } else if( a%8==0 ) { printf("divisible by 8"); } else if(a%5==0) { printf("divisible by 5"); } else { printf("divisible by none"); } getch(); }
Points to Remember
- In if statement, a single statement can be included without enclosing it into curly braces { }
int a = 5; if(a > 4) printf("success");
No curly braces are required in the above case, but if we have more than one statement inside if condition, then we must enclose them inside curly braces.
- == must be used for comparison in the expression of if condition, if you use = the expression will always return true, because it performs assignment not comparison.
- Other than 0(zero), all other values are considered as true.
if(27) printf("hello");
In above example, hello will be printed.
Switch statement is used to solve multiple option type problems for menu like program, where one value is associated with each option. The expression in switch case evaluates to return an integral value, which is then compared to the values in different cases, where it matches that block of code is executed, if there is no match, then default block is executed. The general form of switch statement is,
switch(expression) { case value-1: block-1; break; case value-2: block-2; break; case value-3: block-3; break; case value-4: block-4; break; default: default-block; break; }
Points to Remember
- We don't use those expressions to evaluate switch case, which may return floating point values or strings.
- It isn't necessary to use break after each block, but if you do not use it, all the consecutive block of codes will get executed after the matching block.
int i = 1; switch(i) { case 1: printf("A"); // No break case 2: printf("B"); // No break case 3: printf("C"); break; }
Output : A B C
The output was supposed to be only A because only the first case matches, but as there is no break statement after the block, the next blocks are executed, until the cursor encounters a break.
- default case can be placed anywhere in the switch case. Even if we don't include the default case switch statement works.
Valid Switch | Invalid Switch | Valid Case | Invalid Case |
---|---|---|---|
switch(x) | switch(f) | case 3; | case 2.5; |
switch(x>y) | switch(x+2.5) | case 'a'; | case x; |
switch(a+b-2) | case 1+2; | case x+2; | |
switch(func(x,y)) | case 'x'>'y'; | case 1,2,3; |
Example of Switch Statement
#include<stdio.h> #include<conio.h> void main( ) { int a,b,c,choice; clrscr( ); while(choice!=3) { printf("\n 1. Press 1 for addition"); printf("\n 2. Press 2 for subtraction"); printf("\n Enter your choice"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter 2 numbers"); scanf("%d%d",&a,&b); c=a+b; printf("%d",c); break; case 2: printf("Enter 2 numbers"); scanf("%d%d",&a,&b); c=a-b; printf("%d",c); break; default: printf("you have passed a wrong key"); printf("\n press any key to continue"); } } getch(); }
C goto statement
The goto statement is known as jump statement in C language. It is used to unconditionally jump to other label. It transfers control to other parts of the program.It is rarely used today because it makes program less readable and complex.
Syntax:
- goto label;
goto example
Let's see a simple example to use goto statement in C language.- #include <stdio.h>
- #include <conio.h>
- void main() {
- int age;
- clrscr();
- ineligible:
- printf("You are not eligible to vote!\n");
- printf("Enter you age:\n");
- scanf("%d", &age);
- if(age<18)
- goto ineligible;
- else
- printf("You are eligible to vote!\n");
- getch();
- }
You are not eligible to vote! Enter you age: 11 You are not eligible to vote! Enter you age: 44 You are eligible to vote!
No comments:
Post a Comment
Please write your view and suggestion....