0% found this document useful (0 votes)
4 views

9-10 decision making if else switch

The document discusses control structures in programming, focusing on sequential, selectional, and iterational approaches. It elaborates on various forms of decision-making statements, including simple if, if-else, and nested if statements, along with examples for clarity. Additionally, it covers the use of switch statements and provides code snippets to illustrate how these structures can be implemented in C programming.

Uploaded by

r.p.singh01857
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

9-10 decision making if else switch

The document discusses control structures in programming, focusing on sequential, selectional, and iterational approaches. It elaborates on various forms of decision-making statements, including simple if, if-else, and nested if statements, along with examples for clarity. Additionally, it covers the use of switch statements and provides code snippets to illustrate how these structures can be implemented in C programming.

Uploaded by

r.p.singh01857
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Decision Making,

Branching
if, if-else
Control Structures
➢A control structure refers to the order of executing the program
statements.
➢The following three approaches can be chosen depending on the problem
statement:

✓Sequential (Serial)
▪ In a Sequential approach, all the statements are executed in the same
order as it is written.

✓Selectional (Decision Making and Branching)


▪ In a Selectional approach, based on some conditions, different set of
statements are executed.

✓Iterational (Repetition)
▪ In an Iterational approach certain statements are executed repeatedly.

CSE 1001 Problem Solving using Computers (PSUC) - 2024 2


18-02-2025
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 3
CSE 1001 Problem Solving using Computers (PSUC) - 2024
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 5
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 6
Different forms of if statement
1. Simple if statement.

2. if…else statement.

3. Nested if…else statement.

4. else if ladder
5. Jump Statements:
break
continue
goto
return

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 7


18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 8
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 9
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 10
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 11
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 12
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 13
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 14
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 15
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 16
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 17
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 18
Simple if Statement
General form of the simplest if statement:

if (test Expression)
{ If expression is true
statement-block; (non-zero), executes
} statement.
next_statement; It gives you the choice
of executing
statement or skipping
it.

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 19


if Statement- explanation
➢ (test Expression) is first evaluated.

➢ If TRUE (non-zero), the ‘if’ statement block is executed.

➢ If FALSE (zero) the next statement following the if statement block is


executed.

➢ So, during the execution, based on some condition, some


code will not be executed (skipped).

For example: bonus = 0;


if (hours > 70)
bonus = 10000;
salary= salary + bonus;

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 20


Flow chart of simple if
Entry

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 21


CSE 1001 Problem Solving using Computers (PSUC) - 2024
Find out whether a number is even or odd.
#include <stdio.h>
int main()
{
int x;
printf(“input an integer\n”);
scanf(“%d”,&x);
if ((x % 2) == 0)
{
printf(“It is an even number\n”);
}
if ((x%2) == 1)
{
printf(“It is an odd number\n”);
}
return 0;
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 23

}
Example - if
// Program to calculate the absolute value of an integer

int main ()
{
int number;
printf(“Type in your number: “);
scanf(“%d”,&number);
if ( number < 0 )
number = -number;
printf(“The absolute value is”);
printf(“%d”,number);
return 0;
}

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 24


The if-else statement

if (test expression )
{
statement _block1 if-else
} statement:
else enables you to
{ choose between
statement _block2 two statements
}
Next_statement

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 25


if-else statement
Explanation:
1.First ,the (test expression) is evaluated.

2.If it evaluates to non-zero (TRUE), statement_1 is executed,


otherwise, if it evaluates to zero (FALSE), statement_2 is executed.

3.They are mutually exclusive, meaning, either statement_1 is


executed or statement_2, but not both.

4.If the statements_ 1 and statements_ 2 take the form of block ,


they must be put in curly braces.

Example: if(job_code == 1)
rate = 7.00;
else
rate = 10.00;
prinf(“%d”,rate);

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 26


Find out whether a number is even or odd.
#include <stdio.h>
int main()
{
int x;
printf(“Input an integer\n”);
scanf(“%d”,&x);
if ((x % 2) == 0)
{
printf(“It is an even number\n”);
}
else
{
printf(“It is an odd number\n”);
return 0;
18-02-2025} CSE 1001 Problem Solving using Computers (PSUC) - 2024 27
WAP to find largest of 2 numbers

#include<stdio.h>
int main()
{
int a, b;
printf(“Enter 2 numbers\n”);
scanf(“%d %d”,&a,&b);
if(a > b)
a;
printf(“Large is %d“,a);
else
else
cout<<"large is "<<fb;
printf(“Large is %d“,b);
return 0;
}

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 28


18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 29
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 30
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 31


// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 32


Example: determine if a year is a leap
year
#include<stdio.h> A leap year is
int main() exactly divisible
{ by 4 except for
int year;
century years
printf(“Enter the year“);
(years ending
scanf(“%d”,&year);
if(year%4 == 0) with 00). The
{ century year is
if( year%100 == 0) a leap year only
{ if it is perfectly
if ( year%400 == 0) divisible by 400.
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
} else printf("%d is a leap year",year);
} else printf("%d is not a leap year",year);
return 0;
} 18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 33
Testing for character ranges
Output:
enter a
#include<stdio.h> character:
int main() C
{ uppercase char
char ch; special char
printf(“enter a character\n”);
scanf(“%c”,&ch); enter a
if (ch >= 'a' && ch <= 'z') character:
printf(“lowercase char\n“); j
if (ch >= ‘A' && ch <= ‘Z') lowercase char
special char
printf(“uppercase char\n“);
if (ch >= ‘0' && ch <= ‘9')
enter a
printf(“digit char\n“); character:
else 5
printf(“ special char\n”); digit char
return 0;
}
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 34
Testing for ranges

? if (x >= 5 && x <= 10)

range“);
printf(“in

? if (5 <= x <= 10)


printf(“in range“);

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 35


Testing for ranges

YES if (x >= 5 && x <= 10)


printf(“in range“);

NO ! if (5 <= x <= 10)


printf(“in range“);

Syntactically correct, but semantically an error !!!

Because the order of evaluation for the <= operator is left-to-right, the
test expression is interpreted as follows:
(5<= x) <= 10
The subexpression 5 <= x either has the value 1 (for true) or 0 (for
false). Either value is less than 10, so the whole expression is always
true, regardless of x !

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 36


18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 37
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 38
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 39
CSE 1001 Problem Solving using Computers (PSUC) - 2024
• #include <stdio.h>

• int main() {
• int callUnits;
• float totalBill = 200; // Starting with rental charge

• // Input number of calls


• printf("Enter the number of calls made: ");
• scanf("%d", &callUnits);

• switch (callUnits) {
• case 0 ... 200:
• totalBill += 0; // First 200 calls are free
• break;

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 41


case 201 ... 500:
totalBill += (callUnits - 200) * 1; // Calls between 201 and 500
charged 1 per call
break;

default: // For calls above 500


totalBill += 300 * 1 + (callUnits - 500) * 2; // 300 calls from
201-500 charged at 1/call, above 500 at 2/call
break;
}

// Display final bill


printf("Your total telephone bill is Rs. %.2f\n", totalBill);

return 0;
}

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 42


• #include <stdio.h>

• int main() {
• int phy, chem, bio, math, comp;
• float per;

• /* Input marks of five subjects from user */


• printf("Enter five subjects marks: ");
• scanf("%d %d %d %d %d", &phy, &chem, &bio, &math, &comp);

• /* Calculate percentage */
• per = (phy + chem + bio + math + comp) / 5.0;

• printf("Percentage = %.2f\n", per);

• /* Convert percentage to an integer scale for switch */


• int percentageInt = (int)per; // Cast to int for switch purposes

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 43


/* Find grade according to the percentage using switch case */
switch (percentageInt) { // Using integer division by 10
case 91 ... 100:
// This handles percentage of 100
// This handles 90-99
printf("Grade A\n");
break;
case 81 ... 90: // Handles 80-89
printf("Grade B\n");
break;
case 71 ... 80: // Handles 70-79
printf("Grade C\n");
break;
case 61 ... 70: // Handles 60-69
printf("Grade D\n");
break;
case 51 ... 60: // Handles 50-59
printf("Grade E\n");
break;

18-02-2025 CSE 1002 Problem Solving using Computers (PSUC) - 2025 44


case 41 ... 50: // Handles 40-49
printf("Grade F\n");
break;
default: // Handles < 40
printf("No Grade\n");
}

return 0;
}

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 45


18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 46
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 47
CSE 1001 Problem Solving using Computers (PSUC) - 2024
#include <stdio.h>

int main() {

int number;
printf("Enter a number between 1 to 7: ");
scanf("%d", &number);

switch(number) {
case 1:
printf("Sunday");
break;

case 2:
printf("Monday");
break;

case 3:
printf("Tuesday");
break;

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 49


case 4:
printf("Wednesday");
break;

case 5:
printf("Thursday");
break;

case 6:
printf("Friday");
break;

case 7:
printf("Saturday");
break;

default:
printf("Invalid Number");
}

return 0;
}
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 50
#include <stdio.h>

int main() {

char operator;
printf("Choose an operator ['+', '-',
'*', '/']: ");
scanf("%c", &operator);

double num1, num2;

printf("Enter first number: ");


scanf("%lf", &num1);

printf("Enter second number: ");


scanf("%lf", &num2);

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 51


double result;

switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '/':
result = num1 / num2;
break;
case '*':
result = num1 * num2;
break;

default:
printf("Invalid Operator");
}

printf("%.2lf", result);

return 0;
18-02-2025 } CSE 1001 Problem Solving using Computers (PSUC) - 2024 52
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 53
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 54
CSE 1001 Problem Solving using Computers (PSUC) - 2024
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 56
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 57
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 58
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 59
CSE 1001 Problem Solving using Computers (PSUC) - 2024
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 61
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 62
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 63
Jump Statement

• Break

• Continue

• Goto

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 64


• #include<stdio.h>
• int main()
•{
• /* //break statement

int i;

for(i=1;i<=5;i++)
• {
• if(i==4)
• break; //Exit the loop
• printf("\n%d",i);

}*/

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 65


• #include<stdio.h>
• int main()
•{
• /* //break statement

int i;

for(i=1;i<=5;i++)
• {
• if(i==4)
• continue;// Take to next Incrementation
• printf("\n%d",i);

}*/

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 66


Goto

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 67


• #include<stdio.h>
• int main()
• {

• //goto Statement
• printf("Jawaharlal \t");
• goto y;
• x:
• printf("Technological \t");
• goto z;
• y:
• printf("Nehru \t");
• goto x;
• z:
• printf("University \t");
• }

18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 68


18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 69
CSE 1001 Problem Solving using Computers (PSUC) - 2024
18-02-2025 CSE 1001 Problem Solving using Computers (PSUC) - 2024 71

You might also like