2. Introduction
• Till now, The instructions were executed in the
same order in which they appeared within the
program. (Sequence)
• Branching: one of several possible actions will be
carried out, depending on the outcome of the
logical test.
• Selection: One group of statements is selected
from several available groups.
3. Flow Chart of if Statement
if
condition
Body of if
True
False
Statement just below if
20. Flow Chart of
if else
Statement
if
condition
Body of if
True
False
Statement just below if else
Body of else
21. Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
22. Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
true
23. Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
true
24. Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
true
25. Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
truefalse
26. Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
truefalse
27. Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
truefalse
36. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
37. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
true
38. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
true
39. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
true
40. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
true
41. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
true
42. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
true
43. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
true
44. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
true
45. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
true
46. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
truefalse
47. Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
truefalse
48. Problem
• A program to compare two integers and show the
corresponding result (<,=,>).
51. Solution
#include <stdio.h>
int main()
{
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
if(num1>num2){
printf("first number is biggern");
}
return 0;
}
52. Solution
#include <stdio.h>
int main()
{
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
if(num1>num2){
printf("first number is biggern");
}
else if(num1==num2){
printf("Both numbers are equaln");
}
return 0;
}
53. Solution
#include <stdio.h>
int main()
{
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
if(num1>num2){
printf("first number is biggern");
}
else if(num1==num2){
printf("Both numbers are equaln");
}
else{
printf("num2 is biggern");
}
return 0;
}
54. Syntax of Nested if
if(condition)
{
if(condition)
{
statement;
}
else
{
statement;
}
}
else
{
statement;
}
Nested if else statement is
same like if else
statement, where new
block of if else statement
is defined in existing if or
else block statement.
55. Problem
• A program to check for a valid username and
password. If the username and password is correct
then show “Login Successful” message in the output.
62. Syntax of switch
switch(expression)
{
case constant-expression :
statement(s);
break; ///optional
case constant-expression :
statement(s);
break; ///optional
… …
/// you can have any number of case statements.
default : ///Optional
statement(s);
}
When the variable being
switched on is equal to a case,
the statements following that
case will execute until a break
statement is reached.
63. Syntax of switch
switch(expression)
{
case constant-expression :
statement(s);
break; ///optional
case constant-expression :
statement(s);
break; ///optional
… …
/// you can have any number of case statements.
default : ///Optional
statement(s);
}
When a break statement is
reached, the switch terminates,
and the flow of control jumps to
the next line following the
switch statement.
64. Syntax of switch
switch(expression)
{
case constant-expression :
statement(s);
break; ///optional
case constant-expression :
statement(s);
break; ///optional
… …
/// you can have any number of case statements.
default : ///Optional
statement(s);
}
Not every case needs to contain
a break. If no break appears, the
flow of control will fall
through to subsequent cases
until a break is reached.
65. Problem
• A program that takes input a grade(char) from user
and categorize it as “Excellent”, ”Well done”, ” You
passed”, “Better try again”, “Invalid grade”.
66. Solution
#include <stdio.h>
int main()
{
char ch;
scanf(" %c",&ch);
switch(ch){
case 'A':
printf("Excellentn");
break;
case 'B':
case 'C':
printf("Well donen");
break;
case 'D':
printf("You passedn");
break;
case 'F':
printf("Try againn");
break;
default:
printf("Invalid graden");
}
printf("Exitn");
return 0;
}