0% found this document useful (0 votes)
42 views7 pages

Condi

The document discusses different types of control structures or conditional statements in C programming that are used for decision making. It explains if, if-else, else-if, nested if, switch-case statements with examples. Some key points covered are: 1. If statement checks a condition and executes the block if true. If-else checks a condition and executes one block if true and another if false. 2. Else-if allows checking multiple conditions. Nested if can use if statements within other if statements. 3. Switch-case allows selecting between multiple blocks of code depending on different case numbers and uses break to terminate each case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Condi

The document discusses different types of control structures or conditional statements in C programming that are used for decision making. It explains if, if-else, else-if, nested if, switch-case statements with examples. Some key points covered are: 1. If statement checks a condition and executes the block if true. If-else checks a condition and executes one block if true and another if false. 2. Else-if allows checking multiple conditions. Nested if can use if statements within other if statements. 3. Switch-case allows selecting between multiple blocks of code depending on different case numbers and uses break to terminate each case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

control structure or conditional statements or decision making statements or

branching statements or flow of control:

It is used to check whether is test condition true or false.

1. if
2. if else
3. else if
4. nested if
5. switch case

Ex:
if(a<b)
{
...
}
//if statement:
#include<stdio.h>
int main()
{
int age;
printf("\nEnter the age:");
scanf("%d",&age);
if(age>=18)
{
printf("\nEligible to vote");
}
if(age<18)
{
printf("\nNot Eligible to vote");
}
return 0;
}
//if statement:
#include<stdio.h>
int main()
{
int num;
printf("\nEnter the number:");
scanf("%d",&num);
if(num%2==0)
{
printf("\nThis is an Even");
}
if(num%2!=0)
{
printf("\nThis is an Odd");
}
return 0;
}

//if else statement:


#include<stdio.h>
int main()
{
int num;
printf("\nEnter the number:");
scanf("%d",&num);
if(num%2==0)
{
printf("\nThis is an Even");
}
else
{
printf("\nThis is an Odd");
}
return 0;
}

//if statement: if it is true means will be executed the statements,if it is false


means will be terminated.
#include<stdio.h>
int main()
{
int age;
printf("\nEnter the age:");
scanf("%d",&age);
if(age>=18)
{
printf("\nWelcome to vote.");
}
return 0;

//if else statement: if it is true means will be executed the true statements,if it
is false means will be
//executed the else statements.
#include<stdio.h>
int main()
{
int age;
printf("\nEnter the age:");
scanf("%d",&age);
if(age>=18)
{
printf("\nWelcome to vote.");
}
else
{
printf("\nNot Eligible to vote.");
}
return 0;

//Leap year:
#include<stdio.h>
int main()
{
int year;
printf("\nEnter the year:");
scanf("%d",&year);
if(year%4==0)
{
printf("\nThis is a Leap year");
}
else
{
printf("\nThis is not a Leap year");
}
return 0;

#include<stdio.h>
int main()
{
int num;
printf("\nEnter the number:");
scanf("%d",&num);
if(num%2==0)
{
printf("\nThis is an Even value");
}
else
{
printf("\nThis is an Odd value");
}
return 0;

//else if statement: we can check more than one condition.


#include<stdio.h>
int main()
{
int year;
printf("\nEnter the year:");
scanf("%d",&year);
if(year%400==0) //1800
{
printf("\nThis is a Leap year.");
}
else if(year%100==0) //2020 =20
{
printf("\nThis is not a Leap year.");
}
else if(year%4==0) //2020 ==0
{
printf("\nThis is a Leap year.");
}
else
{
printf("\nThis is not a Leap year.");
}
return 0;
}

//Nested if: we can used if statement with in a if.


#include<stdio.h>
int main()
{
int pin=9890,num,bal=20000,amt;
printf("\nEnter the pin number:");
scanf("%d",&num);
if(num==pin) //outer if
{
printf("\nEnter the amount:");
scanf("%d",&amt);
if(amt<=bal) //inner if
{
bal-=amt; //bal=bal-amt;
printf("\nTransaction completed...");
printf("\nDebited Rs.%d/-",amt);
printf("\nAcc Bal Rs.%d/-",bal);
}
else
{
printf("\nInsufficient fund");
}
}
else
{
printf("\n Invalid pin number.");
}

return 0;
}

//switch case: more than one option we can used by case number.
// break keyword for termination in case block.
//should used switch keyword.

#include<stdio.h>
int main()
{
int opt,a,b,c,i,n;
start:
printf("\n1.Addition");
printf("\n2.Product");
printf("\n3.Even");
printf("\n4.Odd");
printf("\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
c=a+b;
printf("\nAdd=%d",c);
break;
case 2:
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
c=a*b;
printf("\nProduct=%d",c);
break;
case 3:
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nThe Even values are:");
for(i=0;i<=n;i++)
{
if(i%2==0)
{
printf("\n%d",i);
}
}
break;
case 4:
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nThe Odd values are:");
for(i=0;i<=n;i++)
{
if(i%2!=0)
{
printf("\n%d",i);
}
}
break;
case 5:
exit(0);
break;

default:
printf("Invalid choice.");
goto start;
}
return 0;

//Continue statement:
#include<stdio.h>
int main()
{
int i,sum=0;
for(i=1;i<=10;i++)
{
sum+=10;
if(i==5)
{
continue; //skip and continue the process
}
printf("\n%d",sum);
}
return 0;
}

//switch case- we can be used more than one option by case number.
#include<stdio.h>
int main()
{
int a,b,c,i,n,no,x=8;
do
{
printf("\n----------------------------------------");
printf("\n1.Addition");
printf("\n2.Product");
printf("\n3.Even");
printf("\n4.Odd");
printf("\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\nEnter the a and b values:");
scanf("%d%d",&a,&b);
c=a+b;
printf("\nAdd=%d",c);
break;
case 2:
printf("\nEnter the a and b values:");
scanf("%d%d",&a,&b);
c=a*b;
printf("\nMultiply=%d",c);
break;
case 3:
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nThe Even values are:");
for(i=0;i<n;i++)
{
if(i%2==0)
{
printf("\n%d",i);
}
}

break;
case 4:
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nThe Odd values are:");
for(i=0;i<n;i++)
{
if(i%2!=0)
{
printf("\n%d",i);
}
}
break;
case 5:
exit(0);
default:
printf("\nInvalid choice");
break;

}
}
while(x==8);
return 0;
}

You might also like