C programming conditional branching
Conditional branching.
1. Decision making:-
The decision making is use for the weather condition is true or false .
The decision making have a following types:-
*if:-
It is used for testing one condition. if condition is true then back will be executed.
Syntax:-
if(condition test)
{
statememt:
}
For e.g:-
Write a program to check the number is zero or non zero.
#include<stdio.h>
#include<stdio.h>
void main()
{
int n;
printf("enter no");
scanf("%d",&n);
if(n==0)
{
printf("number is non-zero");
}
getch();
}
Output
enter no 4
number is non zero
* if else:-
If else is based for two condition either it will be true or false.
Syntax:-
if(condition test)
{
statement;
}
else
{
statement;
}
For e.g:-
Write a prgram to chech weather no is zero or non zero
#include<stdio,h>
#include<conio.h>
void main()
{
int n;
printf("enetr no");
scanf("%d",&n);
if(n==0)
{
printf("number is non zero");
}
else
{
printf("number is zero");
}
getch();
}
Output
enter no 6
number is no zero
* Ladder if:-
The ladder if is use for checking the multiple condition of the program.it also check the condition of the program.
Syntax:-
if(condition test)
{
statement;
}
else if(condition test)
{
statement;
}
else if(condition test)
{
statement;
}
else
{
statement;
}
getch();
}
For e.g:-
write a program to find a greater three digit no...
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter the value of a,b and c");
scanf("%d%d%d",&a,&b,&c);
if(a<b && b<c)
{
printf("a is greater");
}
else if(b<a && b<c)
{
printf("b is greater");
}
else if(c<a && c<b)
{
printf("c is greater");
}
else
{
printf("wrong choice");
}
getch();
}
Output
enter a value of a,b,c 2
3
4
C is greater
* Nested if:-
A nested if is an if statement that is target of another of another if statement.
Nested if statement means an if statement inside another if statement.
Synatx:-
if(condition test)
{
{
else if(condition test)
{
else if9condition test)
{
statement;
}
else
{
statement;
}
}
}
* Switch case:-
Switch case statement are a substitute for long if statement that compare a variable to several integrals value. it is use for to select M.C.Q as for given situation.
Syntax:-
switch()
{
case 1:
statement:
break;
case 2:
statement;
break;
case 3:
statement;
break;
default;
break;
}
Break:-
Break is a keyword which is use for to exit from current location,current block,current loop.
2. Iteration/repetition
In iteration, we talk about the loop. so,
There are mainly three types of loops following are:-
While loop: -
A while loop is a control flow of statement that allow code to be executed again and again based on a given boolean expansion.
it is also a entry control loop.
Syntax:-
initial value;
while(condition test)
{
expression;
statement;
updation;
}
For e.g:-write a program to print a no series from 1 to 5.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
while(i<=5)
{
printf("%d",i);
i++;
}
getch();
}
Output
1
2
3
4
5
Do while:-
Do while loop is also known as a exit control loop. Do while loop execute and check and test condition at last so firstly, it not check the condition and executed the statement.
Syntax:-
initiial value
do
{
expression;
statement;
updation;
}
while(condition test);
For e.g:-
#include<,stdio.h>
#include<conio.h>
void main()
{
int i;
do
{
printf("%d",i);
i++;
}
while(i<=4);
getch();
}
Output
1
2
3
4
For loop:-
A for loop is a control flow statement for specifying iteration which allow code to be executed again and again.
Syntax:-
for(initial value;condition test;updation)
{
expression;
statement;
}
For e.g:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=4;i++)
{
printf("%d",i);
}
getch();
}
Output
1
2
3
4
Nested loop:-
You can create code that embeds one loop inside another loop. This is known as nested loop.
Syntax:-
ROW----------for(initial value;condition test;updation)
{
column-----for(initial value;condition test;updation)
{
statement 1;
}
statement 2;
}
For e.g:-
To print this pattern:-
***
***
***
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;j=1;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output
***
***
***
c programming language
c programming for prime number
c programming basics
c programming structure
c programming tutorial
c programming array
c programming questions for interview
c programming pdf
for c programming example
c programming examples
c programming questions
c programming interview questions
c programming hello world
what c programming
c programming online compiler
c programming functions
c programming string
c programming online
c programming com…
c programming language
c programming for prime number
c programming basics
c programming structure
c programming tutorial
c programming questions for interview
c programming pdf
for c programming example
c programming examples
c programming questions
c programming interview questions
c programming hello world
what c programming