2. Introduction
• The statements that change the flow of a program to
change the order of execution of statements based on
certain conditions, or repeat a group of statements until
certain specified conditions are met are called as
Decision Making statements or Control statements. The
Control statements are categorized into three major
conditional types they are Decision making, Iteration
statements, Jump Statements
2
3. If Statement
The general Syntax for the simplest if statement:
if (expression) /* no semi-colon */
Statement;
Syntax for the simplest if statement:
if (expression) /* no semi-colon */
{
Statement 1;
Statement 2;
----------------
}
The if keyword is followed by an expression in parentheses. The
expression is evaluated. If the expression is true, it returns 1,
otherwise 0. The value 1 or any non-zero value is considered as
true and 0 as false. If the given expression in the if statement is
true, the following statement or block of statements are
executed; otherwise, the statement that appears immediately
after the if block (true block) is executed. 3
5. Multiple If Statement
The general Syntax for the multiple ifs:
if (expression) /* no semi-colon */
Statement 1;
if (expression) /* no semi-colon */
Statement 2;
if (expression) /* no semi-colon */
Statement 3;
5
6. If-Else Statement
If the expression/condition is true, the body of the if statement is
executed; otherwise, the body of the else statement is executed.
The else keyword is used when the expression is not true.
The general Syntax of if–else statement can be given as follows.
if (expression is true) // if block
{
statement1;
statement 2;
}
else // else block
{
statement 3;
statement 4;
} 6
8. Nested If-Else Statement
In this kind of statement, a number of logical conditions are
tested for taking decisions. Here, the if keyword followed
by an expression is evaluated. If it is true, the compiler
executes the block following the if condition; otherwise, it
skips this block and executes the else block. It uses
the if statement nested inside an if-else statement, which
is nested inside another if-else statement.
8
9. The general Syntax of nested if–else statement is
if (expression1)
{
if(expression2)
statement1;
else
statement2;
}
else
{
if(expression3)
statement3;
else
statement4;
}
next statement5;
9
11. Else – If Ladder
A common programming construct is the else-if ladder,
sometimes called the if-else-ifstaircase because of its
appearance. In the program one can write a ladder of else-
if. The program goes down the ladder of else-if, in
anticipation of one of the expressions being true.
11
12. The general Syntax of nested if–else statement is
if(condition)
{
statement 1; /* if block*/
statement 2;
}
else if(condition)
{
statement 3; /* else if block*/
statement 4;
}
else
{
statement 5; /* last else block */
statement 6;
}
12
13. The flowchart for nesting an else-if statement is shown below.
The conditions are evaluated from top to bottom. As soon as a true
condition is met, the associated statement block gets executed and the
rest of the ladder is bypassed. If none of the conditions are met, then
the final else block is executed. If this else is not present and none of
the if statements evaluate to true, then the entire ladder is bypassed. 13
14. Switch Statement
The switch statement is a multi-way branch statement and an
alternative to if-else-if ladder in many situations. The expression
of switch contains only one argument, which is then checked with a
number of switch cases. The switch statement evaluates the
expression and then looks for its value among the case constants. If
the value is matched with a particular case constant, then those
case statements are executed until a break statement is found or
until the end of switch block is reached. If not, then simply
the default (if present) is executed (if a default is not present, then
the control flows out of the switch block). The default is normally
present at the bottom of theswitch case structure. But we can also
define default statement anywhere in the switchstructure.
The default block must not be empty. Every case statement
terminates with a ‘:’ (colon). The break statement is used to stop the
execution of succeeding cases and pass the control to the end of
the switch block. 14
15. The general Syntax of switch statement is
switch(variable or expression)
{
case constant A: statement;
break;
case constant B: statement;
break;
default: statement;
}
15
16. The flowchart for switch statement is shown below.
The conditions are evaluated from top to bottom. As soon as a true
condition is met, the associated statement block gets executed and the
rest of the ladder is bypassed. If none of the conditions are met, then
the final else block is executed. If this else is not present and none of
the if statements evaluate to true, then the entire ladder is bypassed. 16
17. Note the following for switch case.
1. The switch expression: In the block the variable or expression can be a character
or an integer. The integer expression following the keyword switch will yield an
integer value only. The integer may be any value 1, 2, 3, etc. In case of character
constant, the values may be with alphabets such as ‘x’, ‘y’, ‘z’, etc.
2. The switch organization: The switch expression should neither be terminated
with a semicolon (;) nor with any other symbol. The entire case structure following
the switch should be enclosed within curly braces. The keyword case is followed by a
constant. Every constant terminates with a colon (:). Each case statement must
contain different constant values. Any number of case statements can be provided. If
the case structure contains multiple statements, they need not be enclosed within
curly braces. Here, the keyword case & break performs, respectively, the job of
opening and closing curly braces.
3. The switch execution: When one of the cases is satisfied, the statements
following it are executed. In case there is no match, the default case is executed.
4.The break statement used in switch passes control outside the switch block. By
mistake if no break statements are given, all the cases following it are executed.
17
18. #include <stdio.h>
int main()
{
char ch;
printf("Input a charactern");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.n", ch);
break;
default:
printf("%c is not a vowel.n", ch);
}
return 0;
} 18
19. Switch – Statement : Example
#include <stdio.h>
main()
{
char grade;
printf("Enter a character");
scanf(" %c",&grade);
/* grade=getchar() */
switch( grade )
{
case 'A' : printf( "Excellentn" );
break;
case 'B' : printf( "Goodn" );
break;
case 'C' : printf( "OKn" );
break;
case 'D' : printf( "Can do bettern" );
break;
case 'F' : printf( "You must do better than thisn" );
break;
default : printf( "What is your grade anyway?n" );
break;
}
return 0;
}
19
20. 20
#include <stdio.h> /* Roots of a quadratic equation */
#include <math.h>
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
21. Code 1 : Biggest of three numbers 3 given numbers
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
} 21
22. Code 2 : Biggest of three numbers 3 given numbers
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
return 0; 22