9-10 decision making if else switch
9-10 decision making if else switch
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.
✓Iterational (Repetition)
▪ In an Iterational approach certain statements are executed repeatedly.
2. if…else statement.
4. else if ladder
5. Jump Statements:
break
continue
goto
return
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.
}
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;
}
if (test expression )
{
statement _block1 if-else
} statement:
else enables you to
{ choose between
statement _block2 two statements
}
Next_statement
Example: if(job_code == 1)
rate = 7.00;
else
rate = 10.00;
prinf(“%d”,rate);
#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;
}
return 0;
}
range“);
printf(“in
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 !
• int main() {
• int callUnits;
• float totalBill = 200; // Starting with rental charge
• switch (callUnits) {
• case 0 ... 200:
• totalBill += 0; // First 200 calls are free
• break;
return 0;
}
• int main() {
• int phy, chem, bio, math, comp;
• float per;
• /* Calculate percentage */
• per = (phy + chem + bio + math + comp) / 5.0;
return 0;
}
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;
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);
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
• //goto Statement
• printf("Jawaharlal \t");
• goto y;
• x:
• printf("Technological \t");
• goto z;
• y:
• printf("Nehru \t");
• goto x;
• z:
• printf("University \t");
• }