0% found this document useful (0 votes)
4 views

conditional statements

The document explains various control flow statements in C programming, including if, if-else, nested if-else, if-else-if ladders, and switch statements. It also covers type conversion and type casting, detailing how data types can be converted either implicitly by the compiler or explicitly by the programmer. Example code snippets illustrate the usage of these statements and concepts in practical scenarios.

Uploaded by

omjidwivedi49
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

conditional statements

The document explains various control flow statements in C programming, including if, if-else, nested if-else, if-else-if ladders, and switch statements. It also covers type conversion and type casting, detailing how data types can be converted either implicitly by the compiler or explicitly by the programmer. Example code snippets illustrate the usage of these statements and concepts in practical scenarios.

Uploaded by

omjidwivedi49
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

If statement: The statements inside the body of “if” only execute if

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.

In a c programming language, the data conversion is performed in two different


methods as follows...
1. Type Conversion 2. Type Casting

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' ;

i = x ; =======> x value 15.5 is converted as 15 and assigned to


variable i

x = i ; =======> Here i value 10 is converted as 10.000000 and


assigned to variable x

i = ch ; =======> Here the ASCII value of A (65) is assigned to i


Example Program
#include<stdio.h>
#include<conio.h>
void main()
{
Int i = 95 ;
float x = 90.99 ;
char ch = 'A' ;
i=x;
printf("i value is %d\n",i);
x=i;
printf("x value is %f\n",x);
i = ch ;
printf("i value is %d\n",i);
}
i=
x=
i=
Typecasting:

It is also called an explicit type conversion.


When compiler converts implicitly, there may be a data loss.
In such a case, we convert the data from one data type to another data type
using explicit type conversion. To perform this we use the unary cast operator. To
convert data from one type to another type we specify the target data type in
parenthesis as a prefix to the data value that has to be converted. The general
syntax of typecasting is as follows.

(TargetDatatype) DataValue

Example
int totalMarks = 450, maxMarks = 600 ;

float average ;

average = (float) totalMarks / maxMarks * 100 ;

You might also like