Lecture 6 - Fall 2023
Lecture 6 - Fall 2023
to Computer Science
Computer Science Dept.
Helwan University
Lecture 6
Control Structures: Selection
the If .. Else & Switch .. Case
6.1 Control Structures & Single-Selection Statements (If .. Then)
Control Structures – Sequence Control Structure – Selection Control
Structure (Decisions) – Types of Selection Statements – Single-
Selection Statements – If .. Then Statements
6.2 Double-Selection Statements (If .. Else) & the Conditional Operator
Double-Selection Statements – If .. Else Statements – Compound
Faculty of
Statement (Block) – Conditional Operator ( ? : )
Computers &
6.3 Multiple Selection Statements (Nested if & Switch Case) Artificial Intelligence
Multiple Selection / Cases Statements – Nested if Statements –
Switch–Case Statements FALL 2023
Where are we now .. ?!
0: Course Introduction & Plan
1: Computational Thinking – Part 1 (Computers, Computer Science, & Binary Representation)
2: Computational Thinking – Part 2 (Types of Hardware, Algorithms, & Programming
Languages)
3: Computational Thinking – Part 3 (Types of Software, Basics of Programs, & Compilation)
4: Variables, Constants, & Data Types in C
5: Arithmetic & Bitwise Operations, Logical & Relational Operators, and Precedence
6: Control Structures – Selection (Decisions)
7: Control Structures – Repetition (Loops)
8: C Built-In Functions & Solved Examples (1st Set)
9: User–Defined Functions & Scope of Variables
10: Number Systems, Binary Arithmetic, & the Complements Representation
11: Recursion, Function Overloading, & Call-by-Reference versus Call-by-Value
12: Solved Examples (2nd Set)
Control
Structures
Selection Repetition
Sequence
(Decisions) (Loops)
Pseudocode:
Power ← xy
Output Power
End 6
Example 6.1: Calculate the Power of two
numbers ..
So, .. your program should behave like this:
7
Example 6.1: Calculate the Power of two
numbers ..
#include <stdio.h> // The header file including the printf & scanf
#include <math.h> // The header file including the pow function
int main() // The main function, execution starts here
{
float x, y; // Variables Declarations
// Prompt message to read the 2 numbers
printf( "Please enter 2 integers x & y to calculate the power x^y\n" );
scanf( "%f %f", &x, &y ); // Reads the 2 numbers
printf( "%f to the power of %f equals %f\n", x, y, pow(x, y) );
return 0; // Program ended successfully
}
8
Control Structures ..
Control
Structures
Selection Repetition
Sequence
(Decisions) (Loops)
YES Statement(s)
Condition
NO Statement(s)
NO YES
A < B?
Types of Selection Statements
o Single-Selection Statement:
o if statements, when the condition is:
o true performs an action;
o false the action is skipped.
o Double-Selection Statement:
o if .. else statements, when the condition is:
o true performs an action;
o false performs another action.
o Multiple-Selection Statement:
o Nested if .. else statements
o switch .. case statements
11
Types of Selection Statements ..
Single-Selection Statements ..
if (condition)
- a null statement, or
Statement(s); - a single statement, or
- a block of statements:
{
statement1;
statement2;…
}
true
condition
12
false
12
Example 6.2: Determine Passed Students
Write a program that reads a student’s grade and displays
“Passed” if his/her grade is greater than or equal to 60. The
program should prompt the user through a message to input
a grade, then displays Passed if that grade was greater than
or equal to 60.
Pseudocode:
Start
Read Grade
Print Grade
“Passed” Yes >= 60
No
End
14
Example 6.2: Determine Passed Students
15
Example 6.2: Determine Passed Students
#include <stdio.h> // The header file including the printf & scanf
int main() // The main function, execution starts here
{
int grade; // Variable Declaration
// Prompt message to read a grade
printf("Please enter a student's grade (from 0 to 100)\n");
scanf("%d", &grade); // Reads a grade
if (grade >= 60) // Decision (Selection) Indentation is a good
printf("Passed\n"); programming practice.
return 0; // Program ended successfully
}
16
Up Next ..
Exercises Section 6.2
What have we learned?
Write down an algorithm using pseudocode,
represent it graphically using a flowchart, and
finally write the corresponding C program that:
1) Reads a positive integer N and determines
whether N is even or odd.
2) Reads two positive integers, determines which
has a greater value, and then prints this value
only if it is even.
Write a C program that calculates the cube of an
integer number. The program should be
implemented by using two methods:
1) without using the “pow()” function, and ..
2) using the “pow()” function.
Lecture 6: Control Structures: Selection
the If .. Else & Switch .. Case Statements
6.1 Control Structures & Single-Selection Statements (If .. Then)
Control Structures & the Sequence Control Structure
Selection Control Structure (Decisions) & the Types of Selection Statements
Single-Selection Statements: the If .. Then Statements
Pseudocode:
Step 1: Input a set of 4 marks (M1, M2, M3, & M4)
Step 2: Calculate their average by summing & dividing by 4
Step 3: If average is below 50
Step 4: Print “Fail”
Step 5: Else
Step 6: Print “Pass” 20
Example 6.3: Determine Final Grade
Flowchart:
START
Input M1, M2, M3, M4
GRADE ( M1 + M2 + M3 + M4 ) / 4
is
GRADE < 50
No Yes
PRINT “PASS” PRINT “FAIL”
END 21
Example 6.3: Determine Final Grade
22
Example 6.3: Determine Final Grade
#include <stdio.h> // The header file including the printf & scanf
int main() // The main function, execution starts here
{
// Variables Declaration
int mark_1, mark_2, mark_3, mark_4; float average;
// Prompt message to read the 4 marks
printf("Please enter the student’s 4 marks (each from 0 to 100)\n");
// Read the marks
scanf("%d %d %d %d", &mark_1, &mark_2, &mark_3, &mark_4);
average = (mark_1 + mark_2 + mark_3 + mark_4) / 4.0;
if (average < 50) // Decision (Selection)
printf("Fail\n");
else
printf("Pass\n");
return 0; // Program ended successfully
23
}
Compound Statement (Block) ..
if ( grade >= 60 ) {
printf("Passed.\n");
} // end if
else {
printf ("Failed.\n");
printf ("You must take this course again.\n");
} // end else
Yes Grade No
>=85
Print “A” Yes Grade No
>=75
Print “B” Yes Grade No
>=65
Print “C” Yes Grade No
>=50
Print “D” Print “F”
End
Example 6.4: A Multiple Cases Example
if ( grade >= 85 ) { Nested if Statements ..
printf("A.\n");
} // end if
else {
if ( grade >= 75 ) {
printf("B.\n");
} // end if
else {
if ( grade >= 65 ) { Only a single statement
printf("C.\n");
} // end if will be executed ..
else {
if ( grade >= 50) {
printf("D.\n");
} // end if
else { printf("F.\n");
} // end else
} // end else
} //end else
} // end else
Multiple Cases Statements ..
Switch – Case Statements ..
Yes menuNo No
== 1
result =
num1 + num2 Yes No
menuNo
== 2
result =
break; num1 - num2 display
illegal choice
break;
a third Solution ..
Exercises
What have we learned?
C - switch statement:
https://www.tutorialspoint.com/cprogramming/switch_statement
_in_c.htm
Thank you!