All Tech at one spot
The Control-flow statments of a language specify the order in which computations are performed.
Decision Making Structures require that the programmer specify one or more condtions to be evaluated or tested by the program.
If the first condition is determined to be true else other statment to be excuted if the conditions is determined to be false.
for example :-
#include<stdio.h> // This is the header file for input and Output
#include<conio.h> // This is the header file is used for clear_screen and for getch
void main() // To run the program/ main means :- Start the program
{
clrscr(); // It will clear the screen everytime you run the program.
int a=10 , b=20; // Here we are define the variable for your program.
if (a>b) // Here we start with the if Condition . If this Condtion is true then the below Statment is printed.
{
printf(“A is Big”);
}
else // If the above if statement is false then else Statement is printed.
{
printf(“B is Big”);
}
getch();
}
An if Statment consists of a boolean expression followed by one or more Statement. if” statement. It starts with the keyword “if” followed by an expression in
parentheses. If the expression is evaluated and found to be true, the single statement following
the “if” is executed. If false, the following statement is skipped.
Syntax :-
The Syntax of an if statment in C programming language is :
if (boolean_expression)
{
/* Statement (s) will be excuted if the boolean expression is true.
}
If the boolean expression evaluates to true, then the block of code inside the if statment will be excuted. If the boolean expression evaluates to False, then the first set of code after the end of the if statment.
An if statement can be followed by an optional else statement, which executes when the boolean expression is false. , if the expression in the parentheses evaluates as true, the first expression is executed, otherwise the expression following the “else” is executed.
The if-else statment is used to express decisions. Formally, the syntax is :-
if(expression)
statment1
else
statment2
Example
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=5;
if (a>0)
{
printf(“No. is positive”);
}
else
{
printf(“No is Negative”);
}
getch();
}
An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.
When using if , else if , else statements there are few points to keep in mind:
An if can have zero or one else’s and it must come after any else if’s.
An if can have zero to many else if’s and they must come before the else.
Once an else if succeeds, none of the remaining else if’s or else’s will be tested.
Syntax:-
if(expression)
Statment
else if(expression)
statement
else
Statement
Example :-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=10,b=20,c=14;
if ((a>b) && (a>c))
{
printf(“The First is greater %d”,a);
}
else if(b>c)
{
printf(“The Second number is greater %d”,b);
}
else
{
printf(“The third number is greater %d”,c);
}
getch();
}
Ladder if Statment
In this statment we have ladder of if statment with the if-else Statment.
Syntax :-
if (condition)
{
Statment
}
else if (condition)
{
Statment
}
else if (condition)
{
Statment
}
else if (condition)
{
Statment
}
else
{
Statment
}
Example
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int rno =1,eng=67,maths=78,Social=78,Sci=58,comp=78,tot,perc;
char name[50]=”Hardik_kabra”;
tot = eng+maths+Social+Sci+comp;
perc=tot/5;
printf(“Your Roll no :- %d”,rno);
printf(“Your Name is :-%s “,name);
printf(“Total Marks you Score :- %d”,tot);
printf(“Your Percentage is :- %d “,perc);
if(perc>90)
{
printf(“Your Grade is A”);
}
else if(perc>80)
{
printf(“Your Grade is B”);
}
else if(perc>70)
{
printf(“Your Grade is C”);
}
else if(perc>50)
{
printf(“Your Grade is D”);
}
else if(perc>40)
{
printf(“Your Grade is E”);
}
else if(perc<40)
{
printf(“Your Grade is F”);
}
getch();
}