03.05.2016
Procedural Programming
Exercise 2 (SS 2016) ■ 3.05.2016
What will I learn in the 2. /Exercise
• relational operators and logical operators
• if () else
• switch
• loops
– do while
– while
– for
• Exercise(s)
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 1
03.05.2016
Home exercise 1 (3 points)
Write a program which converts a decimal
number (0-9) into a nibble (half byte) .
The output should be printed in the form:
nibble = 0101
Don‘t use if statements for the solution.
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming Relational Operators
Operator
Meaning
>
Greater than
<
Less than
>=
Greater than or equal to
<=
q
to
Less than or equal
==
Equal to
!=
Not equal to
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 2
03.05.2016
Relational Expression
• Consists of operands and a relational operator
between them.
• The result is a boolean value, represented as
either true (1) or false (0).
• The result of a relational expression can be
assigned to a variable.
Example:
logResult=(x < y)
//logResult is set to 1(true) if x smaller y
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming Logical Operators
• The logical operators allow logical combination
of relational expressions
• The && Operator realizes Logical AND
• The || Operator realizes Logical OR
• The ! Operator realizes Logical NOT
p
Example:
logResult=((x && y) || (!z))
// logResult is set to 1 (true)
// if x AND y are non-zero OR if z is equal 0
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 3
03.05.2016
if - else statement
The if statement executes a code only when the
condition is true. The else part is optional. If-else
can be nested!
if (condition)
{
statement(s)_1;//executed when condition true (1)
}
else
l
{
statement(s)_2;//executed when condition false (0)
}
Joachim Zumbrägel
ti.uni‐due.de
Procedural Programming if vs. if-else
if
if-else
condition
?
false
condition
?
true
true
statement(s)
ti.uni‐due.de
false
statement(s)_1
Joachim Zumbrägel
statement(s)_2
Procedural Programming 4
03.05.2016
The conditional expression
A conditional expression is a „short“ if else!
(expression)?(execute if true):(execute if false)
Example:
conResult=(x>y)? x : y;
//is equal to
if (x>y)
conResult=x;
else
conResult=y;
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming Switch (1)
The switch statement allows the selection of
statements from several available choices.
switch (expression of type integer)
{
case (choice 1):
// executed if expression == choice 1
case (choice 2):
// executed if expression == choice 2
:
case (choice n):
// executed if expression == choice n
default:
// default execution
}
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 5
03.05.2016
Switch (2)
Take care:
When a case is true, all following statements are
executed regardless of the case choices.
=> Use break to leave the switch.
switch (expression of type integer)
{
case (choice 1):
statement(s);
break; //leave the switch block
case (choice 2):
:
}
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming Exercise 2
Write a program which simulates the behaviour
of an AND Gate and an OR Gate. The gates have
two inputs. The program works as follows:
By entering a number the user can choose which
gate he wants to simulate. 1 for the AND Gate
and 2 if he wants two simulate an OR Gate. Then
th user has
the
h to
t enter
t the
th two
t
input
i
t values.
l
After
Aft
that the program shows the result of the
simulation.
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 6
03.05.2016
while loop
while (condition)
{
statement(s)
t t
t( ) // Code
C d for
f
the
th while’s
hil ’ body
b d
//typically here you influence the condition
}
A WHILE loop checks the condition before each
run. The
Th loop’s
l
’ body
b d code
d is
i executed
t d as long
l
as
the condition is true (1).
Joachim Zumbrägel
ti.uni‐due.de
Procedural Programming for loop
for (initialization; condition; increment)
{
statement(s)// Code for the loop's body
}
The Init part initializes any variables required.
The condition part checks a condition, and quits
the loop if false (0). The increment part is
executed before the next repetition is performed.
Hint: A for loop can be exchanged through a
while
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 7
03.05.2016
do while loop
do
{
statement(s)//
t t
t( )// C
Code
d f
for th
the f
for l
loop's
' b
body
d
//typically here you influence the condition
} while (condition);
A DO-WHILE loop is checks the condition after
each
h run. Therefore
Th
f
it will
ill execute
t the
th loop’s
l
’
body code at least once, even if the condition is
false (0).
Joachim Zumbrägel
ti.uni‐due.de
Procedural Programming Flowcharts for loop variants
while-loop
do while-loop
for-loop
initialize
statement(s)
false
false
condition
?
condition
?
true
statement(s)
false
condition
?
true
statement(s)
true
increment
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 8
03.05.2016
break and continue
• break
The break statement leads immediately to an
abort of the loop. The next executed statement
is the one after the loop.
• continue
the continue statement leads immediately to
the next loop cycle.
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming Exercise 3
Extend now the program of exercise 2. The
program should run in a loop, so the user is able
to simulate the GATES again and again til the
user enters a 3 in order to terminate the program.
Implement the loop twice. By use of a “do-while”
and a “while” loop.
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 9
03.05.2016
Home Exercise 2
Write a program which allows the calculation of
the mean of given integer values.
The user is asked to input the numbers one after
the other. When the user inputs a zero, the
progam prints the mean of the values entered
and terminates.
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming Programming style
• Use spaces in source code (easier to read)
• Use BlockCodes
• Descriptive names for variables and use
CamelCase. Example: (int nrOfAdders)
• Initialize your variables
• Use constant or defines for fixed values
• Use comments
• Avoid long lines and complex statements
ti.uni‐due.de
Joachim Zumbrägel
Procedural Programming 10
#include <stdio.h> //Exercise 2
#define SIM_AND 1
#define SIM_OR 2
int main()
{
int select,a,b;
printf("Select (1=AND), (2=OR)");
scanf("%d",&select);
switch (select)
{
case SIM_AND:
printf("Enter a (0 or 1):");
fflush(stdin);
scanf("%d",&a);
printf("Enter b (0 or 1):");
fflush(stdin);
scanf("%d",&b);
printf("result (AND):%d\n", (a && b));
break;
case SIM_OR:
printf("Enter a (0 or 1):");
fflush(stdin);
scanf("%d",&a);
printf("Enter b (0 or 1):");
fflush(stdin);
scanf("%d",&b);
printf("result (OR):%d\n", (a || b));
break;
default:
printf("Wrong input\n");
}
return 0;
}
#include <stdio.h> //Exercise 3
#define SIM_AND 1
#define SIM_OR 2
#define PROG_END 3
int main()
{
int select=0,a,b;
while (select != PROG_END)
{
printf("Select (1=AND), (2=OR), (3=Quit):");
scanf("%d",&select);
switch (select)
{
case SIM_AND:
printf("Enter a (0 or 1):");
fflush(stdin);scanf("%d",&a);
printf("Enter b (0 or 1):");
fflush(stdin);scanf("%d",&b);
printf("result (AND):%d\n", (a && b));
break;
case SIM_OR:
printf("Enter a (0 or 1):");
fflush(stdin);scanf("%d",&a);
printf("Enter b (0 or 1):");
fflush(stdin);scanf("%d",&b);
printf("result (OR):%d\n", (a || b));
break;
case PROG_END:
printf("Program termination\n");
break;
default:
printf("Wrong input\n");
}
}
return 0;
}
© Copyright 2026 Paperzz