0% found this document useful (0 votes)
22 views51 pages

GEC225 Module 1

This document provides an outline for the course Applied Computer Programming II at Covenant University for the 2022/2023 academic year, including the module on decision making in C programming. It covers revision of data types, program structures, and decision making statements like if, if-else, nested if, and case control statements including switch, break, continue, and goto. The lectures will be taught by Prof. E. Adetiba, Mrs Temitope Takpor, Engr. (Mrs) A.H. Ifijeh, and Miss Omolola Ademola.

Uploaded by

Daniel Joseph
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)
22 views51 pages

GEC225 Module 1

This document provides an outline for the course Applied Computer Programming II at Covenant University for the 2022/2023 academic year, including the module on decision making in C programming. It covers revision of data types, program structures, and decision making statements like if, if-else, nested if, and case control statements including switch, break, continue, and goto. The lectures will be taught by Prof. E. Adetiba, Mrs Temitope Takpor, Engr. (Mrs) A.H. Ifijeh, and Miss Omolola Ademola.

Uploaded by

Daniel Joseph
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/ 51

COVENANT UNIVERSITY

Course Title: Applied Computer Programming II


Course Code: GEC 225
Omega Semester
2022/2023

Lecturers: Prof. E. Adetiba, Mrs Temitope Takpor,


Engr. (Mrs) A.H. Ifijeh, Miss Omolola Ademola
MODULE 1:
Revision (Data Types
and Program
Structures) And
Decision Making in C
Revision (Data
Types and
Program
Structures)
Revision of Programming Steps

The following steps are involved when a computer program is used to solve a problem.
i) Analyze the problem
ii) Identify the variables involved
iii) Design the solution
iv) Write the program
v) Enter it into a computer
vi) Compile the program and correct errors
vii) Correct the logical errors if any
viii) Test the program with data
ix) Document the program
4
Declaring A Variable

◦ Declaration
◦ The name of a variable may be any valid identifier.
◦ An identifier is the formal programming term for a word that can be used to name
something.
◦ In C, identifiers may be any length, and can contain letters, numbers, and
underscores (_).
◦ They may only start with a letter or an underscore (not a number)
◦ They are case-sensitive (meaning that abc is different from Abc and ABC is different
from both of them).
◦ The variable declaration ends with a semicolon—which is used to end many
statements in C.
5
Declaring A Variable

This figure shows a variable declaration

6
Assigning a Variable

7
Expressions with Common Operators

◦ Mathematical operators, such as +, –, *, and / are used to carry out arithmetic


operations.
◦ For example, 7 + 3 evaluates to 10 and 4 * 6 + 9 * 3 evaluates to 51.
◦ These operators have the standard rules of precedence—multiplication and
division occur before addition and subtraction

◦ Another common operator which you may not be as familiar with is the modulus
operator, %. The modulus operator evaluates to the remainder when dividing the
first operand by the second.
◦ That is a % b (read “a modulus b”, or “a mod b” for short) is the remainder when a
is divided by b. For example, 19 % 5 = 4 because 19 / 5 = 3 with a remainder of 4
8
Expressions with Common Operators

◦ A statement such as x = x + 1;
◦ The statement above is quite common in programming.
◦ The statement means to take current value of x, add 1 to it, and update x’s value
to whatever that result is.

Example:
int a;
int b;
a = 5;
b = a % 2 - (a + 1) / 2;

9
1.0 Decision
Making in C
OUTLINE

1.0 Decision Making in C

1.1 Decision Making in C: If Statement

1.2 Decision Making in C: If…Else Statement

1.3 Decision Making in C: Nested If Statement

1.4 Decision Making in C – Case Control Statement

1.4.1 Decision Making in C – Case Control Statement: Switch Case Statement in C

1.4.2 Decision Making in C – Case Control Statement: Break Statement in C

1.4.3 Decision Making in C – Case Control Statement: Continue Statement in C

1.4.4Decision Making in C – Case Control Statement: GOTO Statement in C


Decision Making in C

Decision making is about deciding the order of execution of


statements based on certain conditions or repetition of a
group of statements until certain specified conditions are met.

C programming language assumes:


any non-zero and non-null either zero or null, then is
values as true, assumed as false value.

12
Decision Making in C

Decision making structures requires that:

• one or more conditions is to be evaluated or tested by the


program, while

• A statement(s) is/are to be executed if the condition is


determined to be true or false.

13
Decision Making in C

C language handles decision-making by supporting the following


statements,
• if statement
• if...else statement
• nested if statements
• switch statement
• nested switch statements

14
Decision Making in C

15
Friday, March 31, 2023
1.1 If Statement
Decision Making in C: If Statement

If the expression is true, then


'statement-inside' will be
executed,

Otherwise
'statement-inside' is skipped and
only 'statement-outside' is
executed.

17
Friday, March 31, 2023
Decision Making in C: If Statement

Example Program for If Statement in C:


1#include<stdio.h>
2 int main()
2 {
3 int m=40,n=40;
4 if (m == n)
5 {
6 printf("m and n are equal");
7 }
8}
OUTPUT: m and n are equal 18
Friday, March 31, 2023
1.2 If…Else
Statement
Decision Making in C: If…Else Statement

In if else control statement,


• group of statements are executed when
condition is true.
• However, If condition is false, then else part of
the statements are executed.

20
Friday, March 31, 2023
Example Program for If…Else Statement in C

OUTPUT: m and n are not equal

21
Friday, March 31, 2023
Decision Making in C: If…Else Statement
The Ternary Operator ?
Exp1 ? Exp2 : Exp3;
Where: Exp1, Exp2, and Exp3 are expressions.
It can be read as – If Exp1 then Exp2 otherwise Exp3
The outcome of the the expression is determined by:
• Exp1 is evaluated. If it is true, then Exp2 becomes the
result.
• If Exp1 is false, then Exp3 becomes the result.
E.g.
6 == 4? 1:0 // Evaluate 0 since 6 is not equal to 5
Interpret the following?
6>=4?1:0 //

22
Friday, March 31, 2023
1.3 Nested If
Statement
Decision Making in C: Nested If Statement

In “nested if” control statement:

• if condition 1 is false, then condition 2 is checked and


statements are executed if it is true.
• If condition 2 is also false, then else part is executed.

24
#include<stdio.h>
#include<conio.h>
void main() Example Program for Nested If Statement
{
int a;
in C:
printf (“enter a number”);
scanf(“%d”,&a); Note:
if(a%5==0 && a%8==0)
The scanf() function in C is a powerful tool for reading
{
printf(“divisible by both 5 and 8”); input from the user or from a file and storing it in
} variables.
else if(a%8==00 && a%5!=0)
{ When you use the printf family of functions in C
printf(“divisible by 8”); programming to print a formatted string, these functions
} use a percent sign ( % ) to indicate the beginning of a
else if(a%5==00 && a%8!=0) format specifier.
{ For example, %d means to print an int , and %u means to
printf(“divisible by 5”);
print an unsigned int .
}
else
{ The & is a unary operator in C which returns the
printf(“divisible by none”); memory address of the passed operand. This is also
} known as address of operator, and the value of operator.
25
getch();
}
Class Exercise 1
Questions

Write a simple C program to calculate the area of a circle using

26
Friday, March 31, 2023
Area of a Circle

#include <stdio.h>
int main() {
int radius;
float area;
radius = 6;

area = 3.14*radius*radius;
printf("Area of the Circle = %f square inches\n", area);

return(0);
}

27
Questions

What is the output of


this program?

28
Friday, March 31, 2023
Questions

What is the output of


this program?

29
Friday, March 31, 2023
Questions

What is the output of this


program?

30
Friday, March 31, 2023
1.4 Case Control
Statement
Decision Making in C – Case Control Statement

The statements which are used to execute only specific


block of statements in a series of blocks are called case
control statements.

There are 4 types of case control statements in C


language.
i) switch
ii) break
iii) continue
iv) goto
32
Friday, March 31, 2023
Decision Making in C – Case Control Statements

33
Friday, March 31, 2023
1.4.1 Switch Case Statement in C:

Switch case statements are used to execute only specific case statements based on the
switch expression.

Syntax for switch case statement.


switch (expression)
{
case label1: statements;
break;
case label2: statements;
break;
case label3: statements;
break;
default: statements;
break;
} Friday, March 31, 2023
34
Switch Case Statement in C:

35
Friday, March 31, 2023
Switch Case Statement in C:

Rules for applying switch


• With switch statement use only byte, short, int, char data types.
• You can use any number of case statements within a switch.
• Value for a case must be same as the variable in switch.

Limitations of switch
• Relational operators cannot be used with switch statement. For
instance
case k>=20: //is not allowed
• Switch case variables can have only int and char data types, so float data
type is not allowed.
36
Friday, March 31, 2023
Example Program for Switch…Case Statement in C:

37

37
Friday, March 31, 2023
Example Program for Switch…Case Statement in C:

38
Friday, March 31, 2023
1.4.2. Break Statement in C

Break statement is used to terminate the while loops,


switch case and for loops from the subsequent
execution.
• Syntax: break;

39
Friday, March 31, 2023
Break Statement in C:

40
Friday, March 31, 2023
Break Statement in C:
Class Exercise 2
Give the output of this C program

41
Friday, March 31, 2023
Break Statement in C:

Result of Exercise 2

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

42
Friday, March 31, 2023
1.4.3. Continue Statement in C

Continue statement is used to continue the next iteration of for loop, while
loop and do-while loops.
Hence, the remaining statements are skipped within the loop for that particular
iteration.
• Syntax : continue;

43
Friday, March 31, 2023
Continue Statement in C: EXAMPLE
44

44
Friday, March 31, 2023
Continue Statement in C:

Class Exercise 3
Give the output of the following
program

45
Friday, March 31, 2023
Continue Statement in C:

Output Values for Exercise 3


value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
46
Friday, March 31, 2023
1.4.4. GOTO Statement in C

goto statements is used to transfer the normal flow of a


program to the specified label in the program.
Syntax for goto statement
{
…….
go to label;
…….
…….
LABEL:
statements;
}

47
Friday, March 31, 2023
GOTO Statement in C: EXAMPLE

48
Friday, March 31, 2023
GOTO Statement in C:
Class Exercise 4
Give the output of the following program

Output Value for Exercise 3


value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

49
Friday, March 31, 2023
Module-1 Assignment
A) Write C program for the following tasks:
1) Check Whether an Integer is Prime or Not
2) Check Whether a Number is Even or Odd
3) Check Whether a Character is Vowel or consonant
4) Check Whether the Entered Year is Leap Year or Not
5) Check Whether a Number is Positive or Negative or Zero.

B) Develop a Simple Calculator to Add, Subtract, Multiply or Divide any given


number using switch...case in C language.

Note: Module-1 Assignment codes and output are to be submitted on the


Moodle Platform latest before next class.
50
Friday, March 31, 2023
Thank You All for Listening

You might also like