Escolar Documentos
Profissional Documentos
Cultura Documentos
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 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);
}
getch();
}
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();
}
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;
}
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();
}
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