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

CSC209 Lecture Slide 3

Computer

Uploaded by

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

CSC209 Lecture Slide 3

Computer

Uploaded by

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

UNIVERSITY OF DELTA, AGBOR.

FACULTY OF COMPUTING

COMPUTER SCIENCE DEPARTMENT


CSC 209 (SYSTEMS PROGRAMMING WITH C) 1
COURSE CODE: CSC209

COURSE TITLE: SYSTEMS PROGRAMMING WITH C

CSC 209 (SYSTEMS PROGRAMMING WITH C) 2


DECISION MAKING
Programmers uses decision-making
structures to specify one or more conditions
that will be examined or tested by the
program, as well as one or more statements
that will be carried out if the condition is
true and, optionally, additional statements
that will be carried out if it is false.
The broad outline of a common decision-
making structure that may be found in most
programming languages is displayed in this
slide
CSC 209 (SYSTEMS PROGRAMMING WITH C) 3
Any non-zero and non-null values are taken as true in the C programming language,
while zero or null values are taken as false values.
The following categories of decision-making statements are offered by the C
programming language.
Statemnt Description
if statement followed by one or more An if statement consists of a boolean expression
statements.
if...else statement An if statement can be followed by an optional else
statement, which executes when the Boolean
expression is false.
nested if statements You can use one if or else if statement inside
another if or else if statement(s).
switch statement A switch statement allows a variable to be tested
for equality against a list of values.
nested switch statements You can use one switch statement inside another
switch statement(s).

CSC 209 (SYSTEMS PROGRAMMING WITH C) 4


 If Statement
An if statement consists of a Boolean expression followed by one or
more statements.
Syntax
The syntax of an ‘if’ statement in C programming language is:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}

CSC 209 (SYSTEMS PROGRAMMING WITH C) 5


If the Boolean expression evaluates to true, then the block of code
inside the ‘if’ statement will be executed. If the Boolean expression
evaluates to false, then the first set of code after the end of the ‘if’
statement (after the closing curly brace) will be executed.

CSC 209 (SYSTEMS PROGRAMMING WITH C) 6


Example If statement #include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
CSC 209 (SYSTEMS PROGRAMMING WITH C) 7
When the above code is compiled and executed, it produces the
following result:
a is less than 20;

value of a is : 10
If … Else Statement
An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false. It’s syntax is given
below:

CSC 209 (SYSTEMS PROGRAMMING WITH C) 8


Syntax of If … Else statement

if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}

CSC 209 (SYSTEMS PROGRAMMING WITH C) 9


Syntax of If … Else statement
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
If the Boolean expression evaluates to true, then the if block will be
executed, otherwise, the else block will be executed.
CSC 209 (SYSTEMS PROGRAMMING WITH C) 10
#include <stdio.h>
Example of If … else statement int main ()
{
int a = 100; /* declaration of variable*/
/* check the boolean condition */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}

CSC 209 (SYSTEMS PROGRAMMING WITH C) 11


Exercise
1. Write a C program to compute the roots (x1 and x2) of a given
quadratic equation: ax2 + bx + c = 0
The program should handle all cases of
(a) Real and distinct roots
(b) Equal and real roots
(c) Display a message for imaginary or complex roots

CSC 209 (SYSTEMS PROGRAMMING WITH C) 12


LOOPS
A piece of code may need to be executed more
than once in certain circumstances. The first
statement in a function is executed first, then
the second, and so on. In general, statements
are executed in order. Different control
structures offered by programming languages
enable more intricate execution paths.
We can run a statement or set of statements
repeatedly by using a loop statement. The
general format of a loop statement in the
majority of programming languages is
provided here
CSC 209 (SYSTEMS PROGRAMMING WITH C) 13
C programming language provides the following types of loops to
handle looping requirements. For the sake of this course, we are
considering the While and For loops.

CSC 209 (SYSTEMS PROGRAMMING WITH C) 14


WHILE LOOP
A while loop in C programming repeatedly executes a target statement
as long as a given condition is true. The syntax is given below
while(condition)
{
statement(s);
}

statement(s) may be a single statement or a block of statements. The


condition may be any expression. The loop iterates while the condition
is true. When the condition becomes false, the program passes control
to the line immediately following the loop.
CSC 209 (SYSTEMS PROGRAMMING WITH C) 15
#include <stdio.h>
Example int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}
CSC 209 (SYSTEMS PROGRAMMING WITH C) 16
When the above code is compiled and executed, it produces the
following result:

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

CSC 209 (SYSTEMS PROGRAMMING WITH C) 17


FOR LOOP
Using a for loop, which is a repetition control structure, you can
quickly create a loop that needs to run a certain number of times.
In C programming language, a for loop has the following syntax:

for ( init; condition; increment )


{
statement(s);
}

CSC 209 (SYSTEMS PROGRAMMING WITH C) 18


NOTE
1. First, the init step is carried out. You can declare and set any loop
control variables in this stage. As long as there is a semicolon after
this, a statement is not necessary.
2. Next, the condition is then assessed. The body of the loop is
executed if the condition is true. If it is false, the body of the loop is
skipped, and the statement immediately following the "for" loop
takes control.
3. The flow of control hops back up to the increment statement when
the for loop's body has finished running. Any loop control variables
may be updated with this statement. As long as there is a semicolon
after the condition, this statement may be left empty.
CSC 209 (SYSTEMS PROGRAMMING WITH C) 19
4. The situation is now re-evaluated. The process (body of the loop,
increment step, and again condition) repeats itself if the condition
is true. The 'for' loop ends when the condition is no longer true.

CSC 209 (SYSTEMS PROGRAMMING WITH C) 20


#include <stdio.h>
Example int main ()
{
/* for loop execution */
for( int a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d\n", a);
}
return 0;
}

CSC 209 (SYSTEMS PROGRAMMING WITH C) 21


#include <stdio.h>
Example int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}
CSC 209 (SYSTEMS PROGRAMMING WITH C) 22
When the above code is compiled and executed, it produces the
following result:

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

CSC 209 (SYSTEMS PROGRAMMING WITH C) 23


LOOP Control statements
Statements used to control loops divert execution from the usual path.
All automatic objects created within a scope are destroyed when
execution exits it.
The following control statements are supported by C.
Control statements Description
break statement Terminates the loop or switch statement and transfers execution to
the statement immediately following the loop or switch.
continue statement Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.
goto statement Transfers control to the labeled statement.

CSC 209 (SYSTEMS PROGRAMMING WITH C) 24


Exercise
1. Using a FOR LOOP and WHILE LOOP, write a C program that
print out all even and odd numbers between 1 and 100

CSC 209 (SYSTEMS PROGRAMMING WITH C) 25

You might also like