Você está na página 1de 11

THE DECISION MAKING STATEMENTS

if statement
This is the most simple form of the branching statements.
It takes an expression in parenthesis and an statement or block of statements. if the expression is
true then the statement or block of statements gets executed otherwise these statements are
skipped.
NOTE: Expression will be assumed to be true if its evaulated values is non-zero.
if statements take the following form:
if (expression)
statement;
or
if (expression)
{
Block of statements;
}
or
if (expression)
{
Block of statements;
}
else
{
Block of statements;
}
or
if (expression)
{
Block of statements;
}
else if(expression)
{
Block of statements;
}
else
{
Block of statements;
}

EXAMPLE FOR IF ELSE:

EXAMPLE 1:
TO CHECK WHETHER A NUMBER IS EVEN OR ODD
#inclue<stdio.h>
#include<conio.h>
void main()
{
int a,res;
printf("enter value of a ");
scanf("%d",&a);
res=a%2;
if(res==0)
{
printf("\nthe number is even");
}
else
{
printf("\nthe number is odd");
}
getch();
}

EXAMPLE 2:
//To check if year is leap year or not
#include<stdio.h>

#include<conio.h>
main()
{
int a,res;
printf("enter the year ");
scanf("%d",&a);
res=a%4;
if(res==0)else
{
printf("\nthe year is not leap year");
}
getch();

{
printf("\nthe year is leap year");
}

EXAMPLE 3:
//This program finds greatest of three nos
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
printf("enter value of a ");
scanf("%d",&a);

printf("enter value of b ");


scanf("%d",&b);
printf("enter value of c ");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("\ngreatest number is a %d",a);
}
else
{
printf("\ngreatest number is c %d",c);
}
}
else
{
if(b>c)
{
printf("\ngreatest number is b %d",b);
}
else
{
printf("\ngreatest number is c %d",c);
}

}
getch();
}

Nested if...else statement (if...elseif....else Statement)


The nested if...else statement is used when program requires more than one test expression.

Syntax of nested if...else statement.


if (test expression1){
statement/s to be executed if test expression1 is true;
}
else if(test expression2) {
statement/s to be executed if test expression1 is false and 2 is true;
}
else if (test expression 3) {
statement/s to be executed if text expression1 and 2 are false and 3 is
true;
}
.
.
.
else {
statements to be executed if all test expressions are false;
}

The nested if...else statement has more than one test expression. If the first test expression is true,
it executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the
second test expression. If the second test expression is true, it executes the statement/s inside the
braces{ } just below it. This process continues. If all the test expression are false, code/s inside else is
executed and the control of program jumps below the nested if...else

EXAMPLE 4:
//To compute electricity bill
#include<stdio.h>
#include<conio.h>
main()
{
int u;

float a;
printf("enter units consumed ");
scanf("%d",&u);
if(u<100)
{
a=u*1.5;
printf("\nthe electricity charge is %f",a);
}
else if(u>=100&&u<200)
{
a=100*1.5+(u-100)*2.5;
printf("\nthe electricity charge is %f",a);
}
else if(u>=200&&u<500)
{
a=100*1.5+100*2.5+(u-200)*3.5;
printf("\nthe electricity charge is %f",a);
}
else if(u>=500)
{
a=100*1.5+100*2.5+300*3.5+(u-500)*5.0;
printf("\nthe electricity charge is %f",a);
}
getch();
}

EXAMPLE 5:
// To Compute grades of the student.
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,d,e,f;
printf("enter the marks in each subject in any order\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
f=(a+b+c+d+e)/5;
if((f>=90))
printf("the grade is S");
else if(f>=80)
printf("the grade is A");
else if(f>=70)
printf("the grade is B");
else if(f>=60)
printf("the grade is C");
else if(f>=50)
printf("the grade is D");
else
printf("the grade is f");
getch();
}

Conditional Operator (? : Operator):


Conditional operator ? : can be used to replace if...else statements. It has the following general
form:
Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is
evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is
evaluated and its value becomes the value of the expression.
Example: Program to check Even or Odd using Ternary operator
#include<stdio.h>
int main()
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Even"):printf("Odd");
}

SWITCH CASE:
The switch statement is much like a nested if .. else statement. It is mostly a matter of preference
which you use, switch statement can be slightly more efficient and easier to read.
switch( expression )
{
case constant-expression 1:
statements 1;
break;
case constant-expression 2:
statements 2;
break;
case constant-expression 3:
statements 3;
break;
default :
statements 4;
}

Using break keyword:


If a condition is met in switch case then execution continues on into the next case clause also if it
is not explicitly specified that the execution should exit the switch statement. This is achieved by
using break keyword.
What is default condition:
If none of the listed conditions is met then default condition executed.
EXAMPLE :
//Menu driven program for arithmetic operations
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,ch;
printf("\n1.addition \n2.subtraction \n3.multiplication \n4.division ");
printf("\nenter your choice ");
scanf("%d",&ch);
printf("\nenter value of a & b ");
scanf("%d%d",&a,&b);
switch(ch)
{
case 1:
c=a+b;
printf("\naddition of two nos is %d",c);
break;
case 2:
c=a-b;
printf("\nsubtraction of two nos is %d",c);

break;
case 3:
c=a*b;
printf("\nmultiplication of two nos is %d",c);
break;
case 4:
c=a/b;
printf("\ndivision of two nos is %d",c);
break;
default:
printf("enter no between 1-4");
}
getch();
}

Break and Continue Statements


C provides two commands to control how we loop:
break -- exit form loop or switch.
continue -- skip 1 iteration of loop.
You already have seen example of using break statement. Here is an example showing usage of
continue statement.
#include <stdio.h>
main()
{
int i;
int j = 10;
for( i = 0; i <= j; i ++ )
{
if( i == 5 )
{

continue;
}
printf("Hello %d\n", i );
}
}
This will produce following output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Você também pode gostar