0% found this document useful (0 votes)
236 views51 pages

7 - Decision Making and Branching

This document discusses various control statements in C programming including if-else statements, nested if-else statements, else-if ladder, and switch statements. It provides examples of using simple if statements, if-else statements, and nested if-else statements. It also discusses the flow and syntax of else-if ladders and switch statements. The document aims to explain how conditional execution and branching works in C programs through these control statements.

Uploaded by

vishal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
236 views51 pages

7 - Decision Making and Branching

This document discusses various control statements in C programming including if-else statements, nested if-else statements, else-if ladder, and switch statements. It provides examples of using simple if statements, if-else statements, and nested if-else statements. It also discusses the flow and syntax of else-if ladders and switch statements. The document aims to explain how conditional execution and branching works in C programs through these control statements.

Uploaded by

vishal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Decision Making &

Branching
Introduction
▶ Generally C program statement is executed in a order in which they
appear in the program.
▶ But sometimes we use decision making condition for execution
only a part of program, that is called control statement.
▶ Control statement defined how the control is transferred from one
part to the other part of the program.
▶ There are several control statement like if...else, switch, while,
do....while, for loop,break, continue, goto etc.
If statement
➢The if statement may be implemented in different
forms depending on the complexity of conditions to be
tested.
➢The different forms are :
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Using else if statement
Simple if statement
▶ General syntax :

▶ If the expression returns true, then the statement-inside will be


executed, otherwise statement-inside is skipped and only the
statement-outside is executed.
▶ Example :
Simple if statement execution flow/ flowchart
points to note
● If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then
by default if statement will consider the immediate one statement to be
inside its block.
● example :
if(condition)
statement1;
statement2;

// Here if the condition is true, if block


// will consider only statement1 to be inside
// its block.
guess the output
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i =1; int i =15;

if(i>10) if(i>10)
printf("i is greater than 15\n"); printf("i is greater than 15\n");
printf("not inside if\n"); printf("not inside if\n");
} }
Assignment( using if statement)
● Write a C program to check if a given number is even or odd.
● Write a C program to check if a given number is positive,
negative or zero.
● Write a C program to check if entered character is alphabet or
not.
If else statement
➢ If the expression is true, the statement-block1 is executed, else
statement-block1 is skipped and statement-block2 is executed.
➢ General form of if-else statement :
If-else statement execution flow / flowchart
guess the output

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i =1; int i = 5, j = 6, k = 7;
if(i>10) if(i > j == k)
printf("i is greater than 10\n"); printf("%d %d %d", i++, ++j, --k);
else else
printf("i is lesser than 10\n"); printf("%d %d %d", i, j, k);
printf("\noutside if else statement\n"); return 0;
} }
guess the output

#include<stdio.h> #include<stdio.h> #include <stdio.h>


int main() int main() #define TRUE 1
{ { int main()
int i = 2; int i = 5; {
if(i == (1, 2)) if(i = i - 5 > 4) if(TRUE)
printf("Hai"); printf("inside if block"); printf("1");
else else printf("2");
printf("No Hai"); printf("inside else block"); else
return 0; return 0; printf("3");
} } printf("4");
}
guess the output

#include<stdio.h> #include<stdio.h> #include<stdio.h>


int main() int main() int main()
{ { {
int i; if(sizeof(0)) if(sizeof('\0'))
if(scanf("%d",&i)) //if we give printf("Hai"); printf("inside if block");
input as 0 else else
printf("inside if block"); printf("Bye"); printf("inside else
else return 0; block");
printf("inside else block"); } return 0;
return 0; }
}
#include <stdio.h>
void main()
guess the output {
float a=10.5;
if(sizeof(a)==sizeof(10.5))
printf("Matched !!!");
#include <stdio.h> else
printf("Not matched !!!");
void main()
{ if(sizeof(a)==sizeof(10.5f))
printf("Matched !!!");
if(!printf("")) else
printf("Okkk"); printf("Not matched !!!");
else if(sizeof((double)a)==sizeof(10.5))
printf("Hiii"); printf("Matched !!!");
} else
printf("Not matched !!!");

if(a==10.5f)
printf("Matched !!!");
else
printf("Not matched !!!");
guess the output

#define PRINT printf("HAI"); #define PRINT


#include<stdio.h> printf("HAI");printf(“HELLO”);
void main() #include<stdio.h>
{ void main()
int x=1; {
if(x--) int x=1;
PRINT if(x--)
else PRINT
printf("PRINT ELSE"); else
} printf("PRINT ELSE");
}
Guess the output
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i; int i;
if(i=0); if(i=0);
printf("inside if block"); printf("inside if block");
else
} printf("inside else");

}
Assignment( using if else staement)

● Write a c program to input any alphabet and check if it is


vowel or not.
● Write a C program to input a alphabet and check if it is
lowercase or uppercase.
Nested if else statement

▶ when an if else statement is placed


inside the body of “if” or “else”
then it is called nested if else.
▶ General syntax :
#include <stdio.h>
int main() Output:
{ Enter two integers:
int number1, number2; 4
printf("Enter two integers: ");
5
scanf("%d %d", &number1, &number2);
if (number1 >= number2) Result: 4 < 5
{
if (number1 == number2) Output:
printf("Result: %d = %d",number1,number2); Enter two integers:
else 6
printf("Result: %d > %d", number1, number2); 6
}
Result: 6 = 6
else
{
printf("Result: %d < %d",number1, number2);
}
return 0;
}
#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
if (var1 > var2)
printf("var1 is greater than var2\n");
else
printf("var2 is greater than var1\n");
}
else
printf("var1 is equal to var2\n");
return 0;
}
Else-if ladder
➢ The expression is tested from the top(of the ladder) downwards.
As soon as a true condition is found, the statement associated
with it is executed.
➢ Nesting of if-else blocks can be avoided using else..if statement.
Flowchart
points to note(else if ladder)
● else and else..if are optional statements, a program having
only “if” statement would run fine.
● else and else..if cannot be used without the “if”.
● There can be any number of else..if statement in a if else..if
block.
● If none of the conditions are met then the statements in else
block gets executed.
● Just like relational operators, we can also use logical
operators such as AND (&&), OR(||) and NOT(!).
Guess the output
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i=5; int i=7;
if(i==2) if(i==2)
printf("value is 2\n"); printf("value is 2\n");
else if(i==5) else if(i==5)
printf("value is 5\n"); printf("value is 5\n");
else if(i==6) else if(i==6)
printf("value is 6\n"); printf("value is 6\n");
else }
printf("nothing\n");
}
Assignment ( else-if ladder)
● Write a C program to find if two numbers are equal or greatest.
● Write a C program to input marks of five subjects physics, chemistry, biology,
mathematics and computer. Calculate percentage and grade according to the
following.

Percentage >= 90% : Grade A


Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
Switch statement in C
➢ The switch case statement is used when
we have multiple options and we need to
perform a different task for each option.
➢ general syntax :
● Switch statement is used to solve multiple option type problems
for menu like program, where one value is associated with each
option.
● The expression in switch case evaluates to return an integral
value, which is then compared to the values in different cases,
where it matches that block of code is executed, if there is no
match, then default block is executed.
Flowchart
Flowchart
points to note (switch case)
● We don't use those expressions to evaluate switch case, which may return
floating point values or strings.
● The break statement is optional. If omitted, execution will continue on into the next
case. The flow of control will fall through to subsequent cases until a break is
reached.
● default case can be placed anywhere in the switch case. Even if we don't
include the default case switch statement works.
● Case doesn’t always need to have order 1, 2, 3 and so on. They can have any
integer value after case keyword. Also, case doesn’t need to be in an ascending
order always, you can specify them in any order as per the need of the
program.
Points to note
● You can also use characters in switch case.
● duplicate case values are not allowed.
● The expression provided in the switch should result in a constant
value otherwise it would not be valid.
● Nesting of switch statements are allowed, which means you can
have switch statements inside another switch.
● The break statement is used inside the switch to terminate a statement
sequence. When a break statement is reached, the switch terminates,
and the flow of control jumps to the next line following the switch
statement.
Guess the output
#include<stdio.h>
int main()
{
switch(3/2)
{
case 1:printf("case 1 executed ");

case 2:printf("case 2 execcuted ");


break;
Default : printf("Default block
executed");
}
}
int main() int main()
{ {
int i = 0; int i = 0;
switch(i) switch(i);
{ {
i = 2; i = 2;
case 0: printf("Hai. This is case 0"); case 0: printf("Hai. This is case 0");
i++; i++;
printf("\ni = %d",i); printf("\ni = %d",i);
case 1: printf("\nHai. This is case 1"); case 1: printf("\nHai. This is case 1");
i++; i++;
printf("\ni = %d",i); printf("\ni = %d",i);
break; break;
case 2: printf("\nHai. This is case 2"); case 2: printf("\nHai. This is case 2");
i++; i++;
printf("\ni = %d",i); printf("\ni = %d",i);
break; break;
default: printf("\nHai. This is default"); default: printf("\nHai. This is default");
} }
return 0; return 0;
} }
int main() int main()
{ {
int i = 0; int i = 3;
switch(i) switch(i)
{ {
i = 2; default: printf("\nHai. This is default");
default: printf("\nHai. This is default"); case 0: printf("Hai. This is case 0");
case 0: printf("Hai. This is case 0"); i++;
i++; printf("\ni = %d",i);
printf("\ni = %d",i); case 1: printf("\nHai. This is case 1");
case 1: printf("\nHai. This is case 1"); i++;
i++; printf("\ni = %d",i);
printf("\ni = %d",i); break;
break; case 2: printf("\nHai. This is case 2");
case 2: printf("\nHai. This is case 2"); i++;
i++; printf("\ni = %d",i);
printf("\ni = %d",i); break;
break; }
} return 0;
return 0; } }
int main() #include<stdio.h>
{ void main()
int i = 3; {
switch(i) int movie = 1;
{
switch (movie << (2 + movie))
defaul : printf("\nHai. This is default");
case 0: printf("Hai. This is case 0"); {
i++; default:
printf("\ni = %d",i); printf(" Traffic");
case 1: printf("\nHai. This is case 1"); case 4:
i++; printf(" Sultan");
printf("\ni = %d",i); case 5:
break; printf(" Dangal");
case 2: printf("\nHai. This is case 2");
case 8:
i++;
printf("\ni = %d",i); printf(" Bahubali");
break; }
} }
return 0;
}
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int movie = 1; int movie = 1;
switch (movie << (2 + movie)) switch (movie << (2 + movie))
{ {
default: printf(" Traffic"); case 4: printf(" Sultan");
case 4: printf(" Sultan"); default: printf(" Traffic");
case 5: printf(" Dangal"); case 5: printf(" Dangal");
case 8: int a; case 8: printf(" Bahubali");
printf(" Bahubali"); }
} }
}
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int movie = 1; int movie = 1;
switch (movie << (1 + movie)) switch (movie << (2 + movie))
{ {
printf("movies list"); default: printf(" Traffic");
default: printf(" Traffic"); case 4: printf(" Sultan");
case 4: printf(" Sultan"); case 5: printf(" Dangal");
case 5: printf(" Dangal"); case 8: printf(" Bahubali");
case 8: printf(" Bahubali"); }
} }
}
goto statement (unconditional jump statement)
➢ C supports a unique form of a statement that is the goto Statement which is used to
branch unconditionally within a program from one point to another.
➢ The goto statement is used by programmers to change the sequence of execution of a C
program by shifting the control to a different part of the same program.
➢ The general form of the goto statement is:

➢ A label is an identifier required for goto statement to a place where the branch is to be
made.
➢ A label is a valid variable name which is followed by a colon and is put immediately
before the statement where the control needs to be jumped/transferred
unconditionally.
Forward jump statement
example
#include<stdio.h>
void main()
{
printf("\nbefore label");
goto label;
printf("\nstatement 1");
printf("\nstatement 2");
label:
printf("\nskipped previous statements\n");
}
Backward jump statement
Example
#include <stdio.h> #include<stdio.h>
int main() int main()
{ {
int num = 1234, rev = 0; int num;
loop: Read:
if(num > 0) printf ("Enter a number:");
{ scanf("%d",&num);
rev = rev*10 + num%10; if(num>0)
num = num/10; {
goto loop; printf("Entered number is %dn",num);
} goto Read;
printf("\nreverse num = %d",rev); }
} }
#include <stdio.h>
int main()
{
int num;
printf("Enter a positive or negative integer number:\n ");
scanf("%d",&num);
if(num >= 0)
goto pos;
else
goto neg;
pos:
{
printf("%d is a positive number",num);
return 0;
}
neg:
{
printf("%d is a negative number",num);
return 0;
}
Guess the output??
int main() #include <stdio.h>
{ void main()
printf("%d ", 1); {
goto l1; int i = 0, k;
printf("%d ", 2); label: printf("%d", i);
l1:goto l2;
printf("%d ", 3); if (i == 0)
l2:printf("%d ", 4); goto label;
} }

You might also like