conditional statements
conditional statements
the given condition returns true. If the condition returns false then
the statements inside “if” are skipped.
if (condition)
{ //Block of C statements here
//These statements will only execute if the condition is true
}
//statement(s)
#include <stdio.h>
#include <conio.h>
int main()
{
int x = 25; int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
printf(“End of the program”);
getch();
return 0
}
Output:
End of the program
Example of multiple if statements
We can use multiple if statements to check more than one conditions.
#include <stdio.h>
#include <conio.h>
int main()
{ int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:");
scanf("%d", &y);
if (x>y) { printf("x is greater than y\n"); }
if (x<y) { printf("x is less than y\n"); }
if (x==y) { printf("x is equal to y\n"); }
printf("End of Program");
getch();
return 0;
}
If - else statement: If condition returns true then the statements
inside the body of “if” are executed and the statements inside body of
“else” are skipped.
If condition returns false then the statements inside the body of “if”
are skipped and the statements in “else” are executed.
#include <stdio.h>
#include <conio.h>
int main()
{
int age;
clrscr();
printf("Enter your age:");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible for voting");
}
else {
printf("You are not eligible for voting");
}
getch();
return 0;
}
Nested If..else statement: When an if else statement is present
inside the body of another “if” or “else” then this is called nested if
else.
#include<stdio.h>
#include<conio.h>
void main()
{ int n ;
clrscr() ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n < 100 )
//{ printf("Given number is below 100\n") ;
if( n%2 == 0) printf("And it is EVEN") ;
else
printf("And it is ODD") ;
}
else
printf("Given number is not below 100") ; }
if-else-if statement (if-else ladder)
Writing a if statement inside else of an if statement is called if-else-if statement. The
general syntax of the if-else-if statement is as follows...
#include<stdio.h>
#include<conio.h>
Example Program : Find the largest of three numbers.
void main()
{
int a, b, c ;
clrscr() ;
printf("Enter any three integer numbers: ") ;
scanf(“%d %d %d", &a, &b, &c) ;
if( a>=b && a>=c)
printf("%d is the largest number", a) ;
else if (b>=a && b>=c)
printf("%d is the largest number", b) ;
else
printf("%d is the largest number", c) ;
getch();
}
'switch' statement in C
Using the switch statement, one can select only one option from more number of
options very easily.
The switch statement contains one or more cases and each case has a value
associated with it. At first switch statement compares the first case value with the
switchValue, if it gets matched the execution starts from the first case. If it
doesn't match the switch statement compares the second case value with the
switchValue and if it is matched the execution starts from the second case. This
process continues until it finds a match. If no case value matches with the
switchValue specified in the switch statement, then a special case called default is
executed.
When a case value matches with the switchValue, the execution starts from that
particular case. This execution flow continues with the next case statements also.
To avoid this, we use the "break" statement at the end of each case. That means
the break statement is used to terminate the switch statement. However, it is
optional.
The switch statement has the following syntax and execution flow diagram.
#include<stdio.h>
#include<conio.h>
void main()
{ int n ;
clrscr() ;
printf("Enter any digit between 0 to 9: ") ;
scanf("%d", &n) ;
switch( n ) {
case 0:
printf("ZERO") ; break ;
case 1:
printf("ONE") ; break ;
case 2:
printf("TWO") ; break ;
case 3:
printf("THREE") ; break ;
case 4: printf("FOUR") ; break ;
case 5: printf("FIVE") ; break ;
case 6: printf("SIX") ; break ;
case 7: printf("SEVEN") ; break ;
case 8: printf("EIGHT") ; break ;
case 9: printf("NINE") ; break ;
default: printf("Not a valid input. Try again") ;
}
getch() ;
}
Type Casting and Conversion in C
if the expression contains two or more different data type values then they must
be converted to the single data type of destination data type.
Type Conversion:
The type conversion is the process of converting a data value from one data type
to another data type automatically by the compiler. Sometimes type conversion is
also called implicit type conversion.
Int i = 10 ; float x = 15.5 ; char ch = 'A' ;
(TargetDatatype) DataValue
Example
int totalMarks = 450, maxMarks = 600 ;
float average ;