0% found this document useful (0 votes)
29 views

Lecture 3

Uploaded by

BADR ESLAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lecture 3

Uploaded by

BADR ESLAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Assoc. Prof.

Mohamed Moawad Abdelsalam


Head of Computers Engineering and Control Systems dep., Faculty of Engineering,
Mansoura University

E-mail: [email protected]

Lecture 3
Programming statements
1. Flow control statement
2. Branching statement
3. Looping statement
4. Functions
5. Arrays
6. Stings
1. Flow control statement
a. “If” statement
• The if statement is a type of control
statement that falls under the category of
selection statements.
• It allows the program to evaluate a
condition and, based on its result, decide
whether or not to execute a specific block
of code.
Syntax:

if (condition)
{
// statements to execute if condition is true
}

4
Example 1:
Write a program to read an integer number and check if it is a negative number.

#include <stdio.h>
void main()
{
int number;
printf("Enter an integer number: ");
scanf("%d", &number);
if (number < 0)
{
printf("You entered a negative number\n“);
}
}

5
b. “If - else” statement
• In some situations, you may have to
execute statements based on true or
false under certain conditions,
• therefore; you use if-else statements.
If the condition is true, then if block will
be executed otherwise the else block is
executed.

6
Syntax:

if (condition)
{
// run code if condition is true
}
else
{
// run code if condition is false
}

7
Example 2: // Program to display the number type
Write a program to read an #include <stdio.h>
integer number and check if void main()
it is a negative or positive {
number. int number;
printf("Enter an integer number: ");
scanf("%d", &number);
if (number < 0)
{
printf("You entered a negative number\n“);
}
else
{
printf("You entered a positive number\n“);
}
}
5
8
c. “if – else if” statement
The if-else statement executes two different codes depending upon whether the test
expression is true or false. Sometimes, a choice has to be made from more than 2

9
Syntax: if (Condition 1)
{
// statement(s)
}
else if (Condition 2)
{
// statement(s)
}
else if (Condition 3)
{
// statement(s)
}
…….......
else
{
// statement(s)
} 10
Example 3:
Determine the type of a triangle based on the lengths of its three sides: a,b & c. By
evaluating specific conditions:
Validity Check: A triangle is invalid if the sum of the lengths of any two sides is not greater
than the length of the third side.
Equilateral Triangle: The triangle is called equilateral ‫ متساوي االضالع‬if all three sides are
equal in length.
Isosceles Triangle: The triangle is called isosceles ‫ متساوي الساقين‬if two sides have equal
lengths while the third side is different.
Scalene Triangle: The triangle is called scalene if all three sides have distinct ‫مختلفين‬
lengths.

11
#include <stdio.h>
void main()
{
int a = 2, b = 5, c = 5;
// Check for Invalid triangle first
if (a + b <= c || a + c <= b || b + c <= a)
printf("These sides are invalid.\n");

// Equilateral triangle
if (a == b && b == c)
printf("The triangle is equilateral.\n");

// Isosceles triangle
if ((a == b && a != c) || (a == c && a != b) || (b == c && b != a))
printf("The triangle is isosceles.\n");

// Scalene triangle
if (a != b && a != c && b != c)
printf("The triangle is scalene.\n");
12
}
d. Nested “if ” statement
It is possible to include an if...else statement inside the body of
another if...else statement.

13
if (condition 1)
{ //if the second condition holds
if (condition 2)
{ do something
}
//if the second condition does not hold
else
{ do something else
}
}
// if the first condition does not hold
else
{ //if the third condition holds
if (condition 3)
{ do something
}
//if the third condition does not hold
else
{ do something else
}
}
14
Example 4:
Write a C code where we need to analyze if the number is even or odd, and
then if it is even, whether it is divisible by 4 or not, and if it is odd, whether it
is divisible by 3.
#include <stdio.h>
void main()
{
int n;
//take input from the user
scanf("%d", &n);
//if else condition to check whether the number is even or odd
if (n % 2 == 0)
{ //the number is even
printf("Even ");
//nested if else condition to check if n is divisible by 4 or not
if (n % 4 == 0)
{ //the number is divisible by 4
printf("and divisible by 4");
}
else
{ //the number is not divisible by 4
printf("and not divisible by 4");
}
16
}
else
{ //the number is odd
printf("Odd ");
//nested if else condition to check if n is divisible by 3 or not
if (n % 3 == 0)
{ //the number is divisible by 3
printf("and divisible by 3");
}
else
{ //the number is not divisible by 3
printf("and not divisible by 3");
}
}
}
Take home example:
You are working on an e-commerce website. The company has a special
offer where:
• If a customer buys more than 10 items of a single product, they get a 5%
discount.
• If a customer buys more than 50 items of a single product, they get a 10%
discount.
• You can read the number of items and the price of the product. The net
price after discount has to be printed

18

You might also like