Day 3 - Decision Making and Branching
Day 3 - Decision Making and Branching
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:
#include <stdio.h>
int main( )
{
int x, y;
x = 15;
y = 13;
if (x > y )
{
printf("x is greater than y");
}
return 0;
}
Output:
x is greater than y
If the expression is true, the statement-block1 is executed, else statement-block1 is skipped and
statement-block2 is executed.
Example:
#include <stdio.h>
int main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
return 0;
}
Output:
y is greater than x
d) else if ladder
The general form of else-if ladder is,
if(expression1)
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
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.
Example:
#include <stdio.h>
int main( )
{
int a;
printf("Enter a number...");
* Points to Remember
➢ In if statement, a single statement can be included without enclosing it into curly braces
{ ... }
int a = 5;
if(a > 4)
printf("success");
No curly braces are required in the above case, but if we have more than one statement
inside if condition, then we must enclose them inside curly braces.
➢ == must be used for comparison in the expression of if condition, if you use = the
expression will always return true, because it performs assignment not comparison.
➢ Other than 0(zero), all other values are considered as true.
if(27)
printf("hello");
In above example, hello will be printed.
Example:
#include <stdio.h>
int main()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
b) continue statement:
This loop control statement is just like the break statement. The continue statement is opposite
to that of break statement, instead of terminating the loop, it forces to execute the next iteration
of the loop.
As the name suggest the continue statement forces the loop to continue or execute the next
iteration. When the continue statement is executed in the loop, the code inside the loop
following the continue statement will be skipped and next iteration of the loop will begin.
Syntax:
continue;
Example:
#include <stdio.h>
int main()
{
int j;
for (j=0; j<=8; j++)
{
if (j==4)
{
continue;
c) goto statement:
The goto statement in C/C++ also referred to as unconditional jump statement can be used to
jump from one point to another within a function.
Syntax:
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
In the above syntax, the first line tells the compiler to go to or jump to the statement marked
as a label. Here label is a user-defined identifier which indicates the target statement. The
statement immediately followed after ‘label:’ is the destination statement. The ‘label:’ can
also appear before the ‘goto label;’ statement in the above syntax.
d) return statement:
The return in C or C++ returns the flow of the execution to the function from where it is called.
This statement does not mandatorily need any conditional statements. As soon as the statement
is executed, the flow of the program stops immediately and return the control from where it
was called. The return statement may or may not return anything for a void function, but for a
non-void function, a return value is must be returned.
Syntax:
return[expression];
Example:
#include <stdio.h>
int Print()
{
int a,b;
scanf("%d%d",&a,&b);
return(a+b);
return 0;
}
Output:
Compile Time error
65-A
66-B
67-C
68-D
Program:
#include <stdio.h>
int main()
{
int n1,n2,n3,n4;
scanf("%d%d%d%d",&n1,&n2,&n3,&n4);
printf("%d-%c\n",n1,n1);
printf("%d-%c\n",n2,n2);
printf("%d-%c\n",n3,n3);
printf("%d-%c\n",n4,n4);
return 0;
2. Write a program to calculate the fuel consumption of your truck. The program should
ask the user to enter the quantity of diesel to fill up the tank and the distance covered
till the tank goes dry. Calculate the fuel consumption and display it in the format (liters
per 100 kilometers).
Convert the same result to the U.S. style of miles per gallon and display the result. If
the quantity or distance is zero or negative display” is an Invalid Input”.
[Note: The US approach of fuel consumption calculation (distance / f uel) is the inverse
of the European approach (fuel / distance). Also note that 1 kilometer is 0.6214 miles,
and 1 liter is 0.2642 gallons.]
The result should be with two decimal places.
Sample Input 1:
Enter the no of liters to fill the tank
20
Enter the distance covered
150
Sample Output 1:
(Liters/100KM)
13.33
(Miles/gallons)
17.64
Program:
#include <stdio.h>
int main()
3. Vohra went to a movie with his friends in a Wave theatre and during break time he
bought pizzas, puffs and cool drinks. Consider the following prices:
Rs.100/pizza
Rs.20/puffs
Rs.10/cooldrink
Generate a bill for What Vohra has bought.
Sample Input 1:
Enter the no of pizzas bought:10
Enter the no of puffs bought:12
Enter the no of cool drinks bought:5
Sample Output 1: