PHP Conditional statement
Introduction of Programming
What is Program?
- sequence of instruction written for specific purpose
- Like program to find out factorial of number
- Can be written in higher level programming languages that human can understand
- Those programs are translated to computer understandable languages code gceo like bis
- Most prior programming language was 'C’
- Compiler will convert higher level language code to lower Ian language are called syntax binary Code
- Rules & format that should" be followed while writing program . Consider program to print Hello!" on screen
void main()
{
printf("Hello!");
}
Void main() function an entry point of compilationPart within two curly brackets, is body of function
Printf(), function to print sequence of characters on screen
Any programming language having three part
o Data types
o Keywords
o Operators
- Data types are like int, float, double
- Keyword are like printf, main, if, else
- The words that are pre-defined for compiler are keyword
- Keywords can not be used to define variable
- We can define variable of data type c Like int a; then 'a' will hold value of type int and is called variable
- Programs can have different type of statements like conditional statements
- Conditional statements are for checking some condition
- Conditional Statement
- Conditional statements controls the sequence of statements, depending on the condition
- Relational operators allow comparing two values.
1. == is equal to
2.! = not equal to
3. < less than
4. > greater than
5. <= less than or equal to
6. >= greater than or equal to
Simple "if" statement
It execute if condition is TRUE
Syntax:
if (condition)
{
Statement 1;
}
……..
Statement n;
Code:
main()
{
int a,b;
printf("Enter a,b values:");
scanf("%d %d",&a,&b);
if(a>b)
{
printf" n a is
greater than b");
}
Output
Enter a,b values:20
10
a is greater than b
If-else Statement
It execute IF condition is TRUE IF condition is FALSE it
execute ELSE part
Syntax:
if(condition)
{
Staternent 1; ……
Statement n;
}
Else
{
Statement 1; …..
Statement n;
}
code:
main()
int a,b;
printf("Enter a,b values:");
scanf("%d %d",&a,84b);
if(a>b)
{
printf("\n a is
greater than b");
}
else
{
printf("\nb is greater than b");
}
Output
Enter a,b values:10 20
b is greater than a
If-else if statement : It execute IF condition is
TRUE IF condition is FALSE it checks ELSE IF part .ELSE IF is true then execute
ELSE IF PART. This is also false it goes to ELSE part.
Syntax:
if(condition)
{
Statementn;
}
else if(condition)
{
Statementn;
}
else
{
}
Code :
main()
Statement n;
int a,b;
printf("Enter a,b values:");
scanf("%d %d",&a,&b);
if(a>b)
{
printf("\n a is greater than b");
}
else if(b>a)
{
printf("\nb is greater than b");
}
} else
{
printf("\n a is equal to b");
}
Output
Enter a,b values:10 10
0 comments:
Post a Comment