0% found this document useful (0 votes)
11 views29 pages

Chap 2

Uploaded by

yaasinecode578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views29 pages

Chap 2

Uploaded by

yaasinecode578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 2: Conditional Statements

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.

• Know and apply the conditional statement switch.

2
Outline
• Introduction

• The if.... else ... statement

• The "nested if"

• The switch statement ...

3
Introduction
• We want to write a program that calculates the total price of printers order.

1. The unit price of a printer is 120dt. false


condition
2. For 5 printers purchased, the unit price becomes 85 d.
true

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).
}

"else " block


5
Optional
Example 1

6
Example 2

Write a program that displays the maximum of two given integers.

7
Example 2 (solution)
First method:

"If"
block

"else"
Block

8
Example 2 (solution)
Second method:

if without
else

9
The if ... else statement

❑ The condition must be between parentheses


❑ It is possible to define several conditions using the logical operators AND and
OR (&& and ||)
❑ For example, the following instruction tests whether both conditions are true:
if ((condition1)&&(condition2))
❑ The following statement will execute the statements if at least one of the two
conditions is true:
if ((condition1)||(condition2))

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

1. Each " Case " must be unique.


2. The " Case " must be a constant / expression of constant.
3. The " Case " must have the integer type (integer, character).
4. Switch case must have at most one default case .
5. " Default " is optional.
6. " Default " can be placed anywhere in the instruction switch.
7. The break statement; ends the execution of the instruction switch.
8. Two or more "cases" can share a single break statement;
9. The switches nested are allowed.
10. Relational operators are not allowed in the Switch statement.
11. The constants are authorized in the instruction switch Case.

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':

Float numbers are not allowed

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;
}

-> Instruction default is optional. It can be omitted.

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 (grade) • output:


{
case ('A'): Good Job!
printf ("Good Job! \ N"); Pretty good.
Better get to work.
case ('B') : You are in trouble.
printf ("Pretty good. \ N"); You are failing!
case ('C'):
printf ("Better get to work. \ N"); Note: The entry ('A') corresponds to the first case but all
the instructions of the switch are executed.
case ('D'):
printf ("You are in trouble. \ N");
default:
printf ("You are failing !! \ n");
} / * End of switch-case statement* /
} / * End of main program 24
Rule 8: Two or more " cases" can share a
single break statement;
switch (alpha)
{
case 'a':
case 'A':
printf("Alphabet A");
break;
case 'b':
case 'B':
printf("Alphabet B");
break;
}
25
Rule 9: Nested switches are allowed
switch (alpha)
{case 'a':
case 'A':
printf("Alphabet A");
break;
case 'b':
case 'B':
switch (beta)
{...
}
break;
}
26
Rule 10: Relational operators are not allowed in the
Switch statement
switch (num)
{
case >15:
printf("Number> 15");
break;
case =15:
printf("Number = 15");
break;
case <15:
printf("Number <15");
break;
} 27
Rule 11: Constants are allowed in the Switch Case
statement.

int const var = 2;

switch (num)
{
case var:
printf("Number = 2");
break;
}

28
Exercises 6 and 7 from the series of exercise

29

You might also like