Lecture 3-11327
Lecture 3-11327
Zagazig University
Fall 2023
Lecture #3
Dr. Ahmed Amer Shahin
Dept. of Computer & Systems Engineering
These slides are adapted from the slides accompanying the text “C How to Program, 8/e,” https://deitel.com/c-how-to-program-8-e/
Copyright Deitel 2016
Announcements
• A New Team is created in Microsoft Teams:
– For communication, discussions, and
announcements.
• Faculty Platform:
– Continuously updated with Lectures, Tutorials,
and Labs
• Tutorials/Practical Sessions:
– Follow the schedule announced on Teams
Outline
• A C Program to compare two numbers
• Relational and Equality Operators
• Logical Operators
• Structured Program Development in C
– Algorithm, Pseudocode, and Flowchart
– Control Structures
• The selection statements (if, if…else, switch)
• Common Errors
3
Lecture Map
4
A C PROGRAM TO COMPARE TWO
NUMBERS
A C Program to Compare Two Numbers
These operators are binary operators, and the result from any
expression that contains a relational or equality operators is
either 0 (false) or 1 (true)
© 2016 Pearson Education, Ltd. All rights reserved. 11
Relational and Equality Operators
#include <stdio.h>
int main()
{
int a = 4, b = 9;
return 0;
}
if ( num1 == num2 ) {
printf( "%d is equal to %d\n", num1, num2 );
}
16
Logical Operators
if (!(grade == undefinedValue))
printf("The next grade is %f\n", grade);
if (grade != undefinedValue)
printf("The next grade is %f\n", grade);
26
Algorithm, Pseudocode, and Flowchart
34
The if Selection Statement
• The if statement is called a single-selection
statement because it selects or ignores a single
action.
• For example, suppose the passing grade on an exam
is 60.
– The pseudocode statement
If student’s grade is greater than or equal to 60
Print “Passed”
– The corresponding C Code could be
if (grade >= 60)
{
printf("Passed\n");
}
A flowchart for the statement
© 2016 Pearson Education, Ltd. All rights reserved. 35
The if…else Selection Statement
• The if…else statement is called a double-selection statement because
it allows you to specify that different actions are to be performed when
the condition is true and when it’s false.
• For example.
– The pseudocode statement
If student’s grade is greater than or equal to 60
Print “Passed”
else
Print “Failed”
– The corresponding C Code could be
if (grade >= 60)
{
printf("Passed\n");
}
else
{
printf("Failed\n");
}
A flowchart for the statement
40
switch Multiple-Selection Statement
int main(void)
int main(void) {
int count = 50;
{
switch (count)
int count = 50; {
case 90:
if (count == 90)
puts("****");
puts("****"); break;
else if (count == 80) case 80:
puts("***");
puts("***"); break;
else default:
puts("**");
puts("**");
break;
} }
}
44
Common Errors
51