C Basics
04 - control structures
Part 1
(if, else, else if statement & switch case, /AhmedHatemS
default case statement) /c/AhmedHatemS
What is the content?
1. Sequenced control structures.
2. Selection control structures.
➢if, else if, else statements.
➢Switch, case, default statements.
memory
Control structures - sequence
x 2
Program to calculate the power of two numbers.
y 3
result 8
Relational
expressions
• Single-Selection Statement:
if statements, when the condition is:
true performs an action;
false the action is skipped.
Practice:
Program to determine students who passed in a subject.
If grade is more than or equals 60 , the student passed.
• Double-Selection Statement:
if .. else statements, when the condition is:
true performs an action;
false performs another action.
Determine students who passed in a year.
There are 4 subjects.
If the total grade is more than or
equals 50, print “passed”, otherwise
print “failed”.
• Multiple-Selection Statement:
Nested if .. else statements.
switch statements.
Conditional operator (?:)
...
if (grade >= 60)
printf(“passed\n“);
else
printf(“failed\n“);
Practice
A program to check the number is even or odd.
Even number giving integer when dividing it by 2,
that means the mod is 0 in this case.
A program to check the number is positive or negative or zero.
A program to know the student’s grade.
If >= 90 ➔ A+
If >= 85 ➔ A
If >= 80 ➔ B+
If >= 75 ➔ B
If >= 70 ➔ C+
If >= 65 ➔ C
If >= 60 ➔ D+
If >= 50 ➔ D
If <50 ➔ F
Switch .. Case .. Default statements
Only used with integer and character
constants.
Switch works faster than if – else.
program should read two integer numbers from
the user, then display a menu to the user with 2
options:
- If (s)he typed 1 the two integers will be added
and their sum will be displayed, and ..
- If (s)he typed 2 the two integers will be
subtracted and the result will also be displayed.
A program for basic calculator.
Summation ( + )
Subtraction ( - )
Multiplication ( * )
Division ( / )
Next video