Professional Documents
Culture Documents
Table of Contents
Decision making & branching statements in C 3
1. if statement 3
1.1 Simple if statement 3
1.2 if..else statement 4
1.3 Nested if..else statement 5
1.4 else if ladder 7
2. switch statement 8
4. Conditional operator( ?: ) 12
5. goto statement (SLE) 13
1. if statement
❑ if is a keyword in C
❑ Gen. format
if(test expression)
❑ Two possible outcomes for test condition(True or False)
if(condition)
{
statements;
}
// Rest of code
Enter a number:10
Absolute value is 10
In the example above, num<0 gives 1 or 0(True or False) based on value of num. if block is executed only if
num<0 is 1(True)
if(condition)
{
statements set 1
}
else
{
statements set 2
}
//Rest of the code
❑ if..else statement
// Sample program: if else statement
#include<stdio.h>
int main( )
{
int experience = 0,income =0;
printf("Enter experience and income");
scanf("%d %d",&experience,&income);
if(experince>5 && income <30000)
{
//if block
printf("Employee is Underpaid\n");
}
else
{
// else block
printf("Employee is NOT Underpaid\n");
}
return 0;
}
Output:
Enter experience and income:7 40000
Employee is NOT Underpaid
if(condition2) // level 2
{
// do something
}
else // else of level 2
{
// do something else
} // else level end
}
else
{ //else of level 1
if(condition3)
{
// do something
}
else
{
// do something else
}
}
❑ Nested if..else statement (prg311)
// Nested if-else statement
#include<stdio.h>
int main()
{
int exp = 0,income =0;
printf("Enter exp and income:");
scanf("%d %d",&exp,&income);
if(exp >= 5)
{
if(income <30000)
{
printf("1.Employee is Underpaid\n");
}
else
{
printf("2.Employee NOT Underpaid\n");
}
else
{
if(income <10000)
{
printf("3.Employee is Underpaid\n");
}
else
{
printf("4.Employee NOT Underpaid\n");
}
}
return 0;
}
Output:
Enter exp and income:6 25000
1.Employee is Underpaid
if(condition 1)
statement-1;
else if(condition 2)
statement-2;
else if(condition 3)
statement-3;
.
.
.
else if(condition-n)
statement-n
else
default-statement;
// Grade of student
#include<stdio.h>
int main()
{
int mark;
printf("Enter mark(<= 100):");
scanf("%d", &mark);
if(mark>=90)
{
printf("S Grade.");
printf("Congrats..!!");
} // // Flower bracket must since more than 1 statement within if part
else if(mark>=75)
{ printf("A Grade"); } // Flower bracket optional since only 1 statement
else if (mark>=60)
printf("B Grade");
else
printf("Grade is low"); // Default case.
Output:
Enter mark (<= 100):91
S Grade. Congrats..!!
Grading Done
General guidelines for if
Flowchart of switch-case
The switch expression should be an integral type variable (int or char) or an expression that evaluates to and
integer value.
switch(choice)
{
case 1:
printf("Addition selected");
break;
case 2:
printf("Subtraction selected");
break;
case 3:
printf("Multiplication selected");
break;
case 4:
printf("Division selected");
break;
default:
printf("Invalid choice");
} // end switch
return 0;
} // end main
Output:
Enter your choice:
1: Add 2:subtract 3: Multiply 4:Divide ::1
Addition selected
switch(choice)
{
case '+':
printf("Addition selected\n");
break;
case '-':
{ // Optional { }
printf("Subtraction selected\n");
break;
}
case '*':
printf("Multiplication selected\n");
break;
case '/':
printf("Division selected");
break;
default:
printf("Invalid choice");
}
return 0;
}
Output:
Enter your choice:
+: Add
-:subtract
*: Multiply
/:Divide ::*
Multiplication selected
switch(choice) {
case '+':
printf("%d + %d = %d",num1, num2, num1+num2);
break;
case '-':
printf("%d - %d = %d",num1, num2, num1-num2);
break;
case '*':
printf("%d * %d = %d",num1, num2, num1*num2);
break;
case '/':
if(num2 == 0)
{
{
int x,salary;
printf("Enter no. of products sold:");
scanf("%d",&x);
salary = (x==40)?300:(x<40)?(4*x+100):(4.5*x+300);
printf("Salary: %d",salary);
return 0;
}
Output:
Enter no. of products sold:40
Salary: 300
printf("China\n");
printf("England\n");
printf("Germany\n");
label1: // Destination
printf("Italy\n");
}
Output:
India
USA
JAPAN
Italy