Unit III
Unit III
Unit III
if Statements
A simple if structure is called a single-selection structure because it either selects or ignores a single action.
It takes an expression in parenthesis and an statement or block of statements. if the expression is true then
the statement or block of statements gets
executed otherwise these statements are
skipped.
Syntax:
if(Expression/Condition)
statement;
if (Expression/Condition)
statement1;
getch();
}
If-else statement
If-else provides a way to instruct the computer to execute a block of code only if certain conditions have
been met. The syntax of an If-else construct is:
The first block of code executes if the condition in parentheses directly after the if evaluates to nonzero
(true); otherwise, the second block executes.
The else and following block of code are completely optional. If there is no need to execute code if a
condition is not true, leave it out.
#include <stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("enter the values of a and b");
scanf("%d %d",&a,&b);
if(a>b)
printf("a is greater");
else
printf("b is greater");
getch();
}
if..else..if Statement
syntax:
if( expression1 )
statement1;
else if( expression2 )
statement2;
else
statement3;
next_statement;
If the first expression, expression1, is true, statement1 is executed before the program continues
with the next_statement.
If the first expression is not true, the second expression, expression2, is checked. If the first
expression is not true, and the second is true, statement2 is executed.
If both expressions are false, statement3 is executed.
Only one of the three statements is executed.
#include <stdio.h>
#include<math.h>
main()
{
int a,b,c,d,x1,x2;
printf("enter the values of a,b and c");
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("Imaginary %d",d);
}
else if(d==0)
{
x1=x2=-b/2*a;
printf("x1=%d, x2=%d, d=%d",x1,x2,d);
}
else
{
x1=(-b+sqrt(d))/2*a;
x2=(-b-sqrt(d))/2*a;
printf("x1=%d, x2=%d, d=%d",x1,x2,d);
}
}
Nested if Statements
It is a conditional statement which is used when we want to check more than 1 conditions at a time in a
same program. The conditions are executed from top to bottom checking each condition whether it meets
the conditional criteria or not. If it found the condition is true then it executes the block of associated
statements of true part else it goes to next condition to execute.
Syntax:
if(condition)
{
if(condition)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the
braces and again checks the next condition. If it is true then it executes the block of statements associated
with it else executes else part.
#include <stdio.h>
main()
{
int a,b,c;
printf("enter the values of a,b and c");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greater");
}
else
{
printf("c is greater");
}
}
else
{
if(b>c)
{
printf("b is greater");
}
else
{
printf("c is greater");
}
}
}
Switch
Switch case statements are a substitute for long if statements that compare a variable to several "integral"
values ("integral" values are simply values that can be expressed as an integer, such as the value of a char).
The basic format for using switch case is outlined below. The value of the variable given into switch is
compared to the value following each of the cases, and when one value matches the value of the variable,
the computer continues executing the program from that point.
switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value
break;
.
.
.
default:
Code to execute if <variable> does not equal the value following any of the cases
break;
}
#include <stdio.h>
main()
{
int code;
printf("Enter the code");
scanf("%d",&code);
switch(code)
{
case 1: printf("Red");
break;
case 2: printf("Green");
break;
case 3: printf("Blue");
break;
default: printf("not defined");
}
}
Conditional Operator
(Exp) ? exp1 : exp2
while (condition){
statement;
}
false
/*Sum of first n numbers */ Continuation
#include<stdio.h> condition?
#include<conio.h>
void main()
{ true
int n,a=0,i=1;
printf("Enter the number\n"); Statement(s)
scanf("%d",&n);
while(i<=n){
a=a+i;
i++;
}
printf("Sum of first %d number is %d",n,a);
getch();
}
do{
statement
}while (condition);
When program execution reaches a do...while statement, the following events occur:
Initial-Action
for ( initial; condition; increment ){
statement;
}
false
/* Table generation for n */ Action-After- Continuation
void main() Each-Iteration condition?
{
int n,a=0,i;
true
printf("Enter the number\n");
scanf("%d",&n); Statement(s)
for(i=1;i<=10;i++) (loop-body)
{
a=n*i;
printf("%d\n",a);
} Next
getch(); Statement
}
false
Continuation
condition?
true
Statement(s)
break
Statement(s)
Next
Statement
The break statement can be placed only in the body of a for loop, while loop, do...while loop or
switch.
break is used inside a loop or switch statement. It causes the control of a program to skip past the
end of the current loop (for, while, or do...while) or switch statement. No further iterations of the
loop execute.
false
Continue
condition?
true
Statement(s)
continue
Statement(s)
Next
Statement
Like the break statement, the continue statement can be placed only in the body of a for loop, a
while loop, or a do...while loop. When a continue statement executes, the next iteration of the
enclosing loop begins immediately. The statements between the continue statement and the end
of the loop aren't executed.
continue is used inside a loop. It causes the control of a program to skip the rest of the current
iteration of a loop and start the next iteration.
Example
printf("Printing only the even numbers from 1 to 10\n");
for( x = 1; x <= 10; x++ ) {
if( x % 2 != 0 ) /* See if the number is NOT even */
continue; /* Get next instance x */
printf( "\n%d", x );
}
Syntax-
goto Label ;
Label:
/*Some Code*/
Syntax-
Label:
/*Some Code*/
goto Label;