Professional Documents
Culture Documents
PPC - Unit 2
PPC - Unit 2
PPC - Unit 2
Unit – II
Decision Making and Branching: Decision making with If, simple IF, IF ELSE, nested IF ELSE,
ELSE IF ladder, switch, and GOTO statement.
Decision Making and Looping: While, Do-While, For, Jumps in loops.
Branching is deciding what actions to take and looping is deciding how many times to take a
certain action.
The if statement controls whether a program enters a section of code or not based on the
condition. One of the important functions of the if statement is that it allows the program to
select an action based upon the user's input. The program chooses to follow one branch or
another.
1. Simple if Statement
2. if-else Statement
3. Nested if-else Statement
4. else-if Ladder
27/03/2023 Lecture Notes - Dr. Persis 2
1. Simple if statement
Definition: If the test expression is evaluated to true, the statement block will get
executed, or it will get skipped. Flow chart
The basic format of the if Statement is:
Syntax
if(test_expression)
{
statement 1;
statement 2;
...
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 15,
b = 20;
if (b > a)
{
printf("b is greater");
}
getch();
}
27/03/2023 Lecture Notes - Dr. Persis 4
2. If…Else statement
Definition: "If else statements" executes some statement code block if the expression is true;
otherwise, it executes the else statement code block.
• The basic format of if else Statement is: Flow chart
• Syntax:
if(test_expression)
{
//execute your code
}
Else
{
//execute your code
}
27/03/2023 Lecture Notes - Dr. Persis 5
Example – If…Else Statement
#include<stdio.h>
if (a > b)
#include<conio.h>
{
printf("\n a is greater");
void main()
}
{ int a, b;
else
printf("Please enter the value for a:");
{
scanf("%d", &a);
printf("\n b is greater");
printf("\nPlease the value for b:");
}
scanf("%d", &b);
getch();
}
Definition: Nested "if else statements" means the use of conditional statements inside
another conditional statement.
• The basic format of the Nested if-else statement is:
• Syntax:
if(test_expression one)
{
if(test_expression two)
{
//Statement block executes when the test expression two
is true.
}
}
else
{
//else statement block
27/03/2023 } Lecture Notes - Dr. Persis 7
Flow chart
#include<stdio.h>
else if (marks >= 50 && marks < 70)
#include<conio.h>
{
printf("YOUR GRADE : C\n");
int main()
}
{
else
int marks;
{
printf("Enter marks between 0-100\n");
printf("YOUR GRADE : Failed\n");
scanf("%d", &marks);
}
getch();
if(marks >= 90)
}
{
printf("YOUR GRADE : A\n");
}
else if (marks >= 70 && marks < 90)
{
printf("YOUR GRADE : B\n");
}
27/03/2023 Lecture Notes - Dr. Persis 11
Switch statement
Flow Chart
Definition: A switch statement allows a variable to be tested for
equality against a list of values. Each value is called a case, and
the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch is
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
...
default:
// default statements
} 27/03/2023 Lecture Notes - Dr. Persis 12
program to read weekday number and print weekday name using switch.
#include <stdio.h>
#include <conio.h>
case 4: printf("Thursday");
break;
Void main()
case 5: printf("Friday");
{
break;
int wDay;
case 6: printf("Saturday");
printf("Enter weekday number (0-6):");
break;
scanf("%d",&wDay);
default: printf("Invalid weekday number");
switch(wDay)
printf("\n");
{
case 0: printf("Sunday");
getch();
break;
}
case 1: printf("Monday");
break; Output
case 2: printf("Tuesday"); Enter weekday number (0-6): 3
break; Wednesday
case 3: printf("Wednesday"); Enter weekday number (0-6): 9
break; Invalid weekday number.
27/03/2023 Lecture Notes - Dr. Persis 13
goto statement
Definition: The goto is used to transfer the program control to a predefined label. It is a jump statement which is
sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere
to anywhere
#include <stdio.h>
#include <conio.h>
Enter the number table you want to print?10
10 x 1 = 10
int main() 10 x 2 = 20
{ 10 x 3 = 30
int num, i=1; 10 x 4 = 40
printf("Enter the number table you want to print?"); 10 x 5 = 50
scanf("%d",&num); 10 x 6 = 60
table: printf("%d x %d = %d\n",num, i, num*i); 10 x 7 = 70
10 x 8 = 80
i++;
10 x 9 = 90
if(i<=10) 10 x 10 = 100
goto table;
}
Syntax
The syntax of a while loop is −
while(condition)
{
statement(s);
}
27/03/2023 Lecture Notes - Dr. Persis 17
Example Program to print numbers from 10 to 19 using while loop
syntax
•The init step is executed first, and only once. This step allows you to declare and initialize any loop control
variables.
•Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop
does not execute and the flow of control jumps to the next statement just after the 'for' loop.
•After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This
statement allows you to update any loop control variables
•The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop,
then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.
#include<stdio.h>
#include<conio.h>
Output
void main() ABCDEFGHIJKLMNOPQRSTUVWXYZ
{
char c;
for (c = 'A'; c <= 'Z’; c++)
printf("%c ", c);
getch();
}