Chap 2
Chap 2
Level: 1A
Algorithmic and programming team
AY: 2023/2024
Goals
By the end of this chapter, the student will be able to:
• Know and apply conditional statements if ... else and nested if.
2
Outline
• Introduction
3
Introduction
• We want to write a program that calculates the total price of printers order.
We must calculate the amount to pay: quantity * unit price 85 * Number 120 * Number
the unit price is variable and depends on the quantity
the treatment must therefore carry out a test on the number of printers
using
conditional statements
4
The if ... else statement
Condition = Logical expression
if (Condition) single or combined.
Examples: (A <B), (M> 10.0), (Delta <= 0),
{ ((Age> 18) && (Avg> = 14.0))
...
"If" ...
block Treatment to be performed if the condition is true(any
} value ≠ 0). At the end of the processing, there is a jump
else after the else part.
{
...
... Treatment to be performed if the condition is false
(value = 0).
}
6
Example 2
7
Example 2 (solution)
First method:
"If"
block
"else"
Block
8
Example 2 (solution)
Second method:
if without
else
9
The if ... else statement
10
Exercises 1 and 2 from the series of exercise
11
"Nested if"
if (condition1)
list of instructions 1
else
if ( condition2)
list of instructions 2
else
if (condition3)
list of instructions 3
else
if (condition N)
list of instructions N
else
list of instructions N + 1
12
Example 3
13
Exercise 4 from the series of exercise
14
15
The switch statement
Only integer expressions are allowed: int, short, long
switch (Expression) or char.
{
case val1:… Val1, val2,… must be constants of the same type as
... Expression.
break;
case val2:… Mandatory in order to do not enter in the
Next « case »
...
break;
case val3:
...
break; Executed when none of the preceding « cases » is
default:… true
}
16
Rules for using the statement 'Switch Case' in C programming
17
Rrule 1: The 'case' label must be unique
int id = 3;
switch (id)
{
case 1:
printf(« C Programming Language");
break;
case 2:
printf("C ++ Programming Language");
break;
case 2:
printf("Web Technology");
break;
default:
printf("No student found");
break;
}
18
Rule 2: 'Case' must be a constant /
expression of constants
case 1 + 1:
case 'A':
case 67:
⇒These examples are allowed. However, variables are not allowed in label cases.
case var :
case num1 :
case n1 + n2 :
19
Rule 3: 'Case' must have the integer type (integer or
Character)
case 10:
case 20 + 20:
case 'A':
case 'a':
case 10.12:
case 7.5:
20
Rule 4: 'Switch case' must have at most one default case
switch (roll)
{
case 1:
printf("C Programming Language");
break;
case 2:
printf("C ++ Programming Language");
break;
case 3:
printf("Web Technology");
break;
default:
printf("Default Version 1");
break;
default:
printf("Default Version 2");
break;
}
21
Rule 5: "Default" is optional
switch (roll)
{
case 1:
printf("C Programming Language");
break;
case 2:
printf("C ++ Programming Language");
break;
case 3:
printf("Web Technology");
break;
}
22
Rule 6: "Default" can be placed anywhere in
the switch statement.
switch (roll)
{
case 1:
printf("C Programming Language");
break;
default:
printf("No Student Found");
break;
case 2:
printf("C ++ Programming Language");
break;
case 3:
printf("Web Technology");
break;
}
23
Rule 7: The break statement; ends the execution of
the switch statement.
#include <stdio.h>
main ()
{char grade;
printf ("Enter your current letter grade \ n");
grade = getchar (); • If the entered value of grade = 'A'
switch (num)
{
case var:
printf("Number = 2");
break;
}
28
Exercises 6 and 7 from the series of exercise
29