Anda di halaman 1dari 11

De La Salle University - Manila

Nested Conditional and


Iterative Statements

Preliminary Report

Harriette C. Mojica
LBYEC72 EE2

Marvil V. Graza
June 1, 2016
Introduction
Every procedural programming language makes use of both the conditional statements (if
statements) and the iterative statements (loops). Without these statements, programs would
run at almost the exact same way every single time, always following the same sequence of
function calls. Conditional and iterative statements allows the flow of a program to be changed,
leading to more interesting codes, and thus, creating more functional programs.
Controlling the flow of a program and letting it make decisions on what code to execute are
valuable to the programmer. The conditional statements allow you to control whether or not a
program enters a section of code based on whether a given condition is true or false. An
important function of these conditional statements is that the action is based upon the user's
input.
Iterative statements, on the other hand, are useful when a programmer wants to repeat the
execution of one or more statements or a block of code for a specified number of times. Being
able to have your program repeatedly execute a block of code is one of the most basic but useful
tasks in programming. Many programs or websites that produce extremely complex output are
really only executing a single task many times.
Concept Theory
Conditional Statements
Branching is deciding what actions to take and is called so because the program chooses to
follow one branch or another depending on the required specifications.
In C, programming conditional statements are used for branching.
Conditional statements are used to execute a statement or a group of statements based on
certain conditions. It controls whether or not a program executes a block of codes depending on
whether a given condition is proven either true or false.
 If Statement and If-else Statement
The if and if-else statements in C are used to select one or two alternative course of
actions. The if statement is used in single alternative structures where it executes only
when the condition is true. The if-else statement is used in two alternative structures
where it executes when the condition is either true or false When the condition is proven
true, the statement for ‘for’ is executed. Otherwise, the statement for ‘else’ will run. The
else part is optional which means that it can be included or not.
To have more than one statement execute after an if statement when evaluated true or
after an else statement when evaluated false, use braces. Note: Anything inside braces is
called a compound statement or a block. When using if statements, the code that
depends on the if statement is called the “body” of the if or the else statement.
 Ladderized If-else Statement
Sometimes, there are multiple conditional statements that may all evaluate to true, yet
you want only one if statement’s body to execute. This statement is useful in perfoming a
function this important. It is possible to use numerous if-else statements to ensure that
only one block of code is executed.
Only those conditional statements and expressions that were first proven true are the
ones to be chosen and its associated statements will be executed. When all conditions
are proven false, the statement for ‘else’ will run.
“The sequence of if-else statements is the most general way of writing a multi-way
decision. The expressions are evaluated in order, if any expression is true, the statement
associated with it is executed, and this terminates the whole chain.” – Dennis Ritchie
 Switch/Case Statement
A switch statement is a type of selection control mechanism used to allow the value of a
variable or expression to change the control flow of program execution via a multiway
branch. The switch/case statement is used to simplify some of the tasks of ladderized if-
else statements. Variables declared as integers and characters are the best candidates
that can be tested or evaluated by the switch/case statement.
“The switch/case statement is a multi-way decision that tests whether an expression
matches one of a number of constant integer values, and branches accordingly. If a case
matches the expression value, execution starts at the case.” -Dennis Ritchie
Iterative Statements
Loops are created by using iterative statements. Iterative statements or looping statements are
program instructions that repeat a statement or a sequence of statements in a specified number
of times, subject to the loop-termination criteria and cause statements (or compound
statements) to be executed zero or more times. They are a set of instructions to be performed
all over again until a certain condition is reached, met, or proven, and tested as false. These
statements are executed in order except when a jump statement is encountered.
Iteration may be performed over an arithmetic progression of integers or over any finite
enumerated structure. Early termination of the body of loop may be specified.
 For Loop
For loops are the most useful type. A for loop executes a sequence of statements
multiple times and abbreviates the code that manages the loop variable. The variable
initialization allows you to either declare a variable and give it a value or give a value an
existing variable. The condition tells the program that while the conditional expression is
true, the loop should continue to repeat itself. If the condition is empty, it is evaluated as
true and the loop will repeat until something else stops it. The variable update section is
the easiest way for a for loop to handle the changing of the variable.
A for loop can run a statement or a block of statements repeatedly until evaluated false.
 While Loop
The while loop repeats a statement or a group of statements while a given condition is
true. It tests the condition before executing the loop body. While loops are very simple. A
while loop is like a stripped-down version of a for loop -- it has no initialization or update
section. However, an empty condition is not legal for a while loop as it is with a for loop.
 Do-While Loop
Do-while loops are used for a set of statements that are wanted to loop at least once. It is
quite similar to the while loop, except that the loop body is guaranteed to execute at
least once. A while loop says "Loop while the condition is true and execute this block of
code.” A do-while loop, on the other hand, says "Execute this block of code, and then
continue to loop while the condition is true.”
Concept Examples
Ladderized If-Else Statement
Syntax:
if (condition)
statement1;
else if (condition)
statement2;
else if (condition)
statement3;
else
statement4;

Example:
// A program that identifies if input grade is passing or failing
// 0-69: FAIL, 70-100: PASS, Otherwise, OUT OF RANGE.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int grade;
printf(“Enter grade: “);
scanf(“%d”, &grade);
if ((grade>=0)&&(grade<=69))
printf(“FAIL”);
else if ((grade>=70)&&(grade<=100))
printf(“PASS”);
else
printf(“OUT OF RANGE”);
getch();
return 0;
}

Switch/Case Statement
Syntax:
switch (var_expression)
{
case const_value: statement1; break;
case const_value: statement2; break;
case const_value: statement3; break;
default: statement x; break;
}

Example:
// A program that gives a short horoscope
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf(“Enter a digit from 1 -5 to know your horoscope
today!\n\nYour Digit: “);
scanf(“%d”, &n);
printf(“\nYour Horoscope:”);
switch (n)
{
case 1: printf(“You might be meeting an old friend or you
might be reminiscing an old memory today! Take all the time you need to
reflect on your past.”); break;
case 2: printf(“Take all distractions away from your desk
and focus on your work. It’s better missing your friend for a day rather than
missing a deadline!”); break;
case 3: printf(“Now is a great time for a break! Take a day
off and reward yourself for all the work you have done this week.”); break;
case 4: printf(“Someone close to you might be giving you
some advice today. Be open-minded and appreciate their willingness to
help.”); break;
case 5: printf(“Today may be an upsetting day for you. But
give yourself a pat in the back for giving it all your best. Now move on to
the next challenge!”); break;
default: printf(“INVALID !!! Pick a digit from 1-5 ONLY.”);
break;
}
getch();
return 0;
}

For Loop
Syntax:
for (initialization; condition; loop control)
{
… action;
}

Example:
// A program that displays the following sequence: 13 12 11 10 9 8 7 6
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
for (a=13; a>=6; a--)
printf(“%d “, a);
getch();
return 0;
}

While Loop
Syntax:
while (condition)
{
statements;
inc/dec/loop control;
}

Example:
// A program that displays the following sequence: 13 12 11 10 9 8 7 6
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a=13;
while (a>=6)
{
printf(“%d “, a);
a--;
}
getch();
return 0;
}

Do-While Loop
Syntax:
initialization;
do { statements; inc/dec; }
while (condition);

Example:
// A program that displays the following sequence: 13 12 11 10 9 8 7 6
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a=13;
do {
printf(“%d “, a);
a--;
}
while (a>=6);
getch();
return 0;
}
Engineering Equations
(Formulas taken from Mechanics of Deformable Bodies)
Formula for Bearing Stress
𝑃𝑏
𝜎𝑏 =
𝑡 ×𝑑
where 𝜎𝑏 = bearing stress (in MPa)
𝑃𝑏 = compressive force (in N)
t = thickness of the body (in mm)
d = bolt diameter (in mm)

Bearing stress is caused by the contact between two separate bodies. It is the internal
compressive stress, P applied over a contact area Ab. In bolts, Ab is the projected area of
the rivet or bolt hole, denoted by the thickness of the body, t, and the rivet diameter, d.
Formula for Deformation or Change in Length
𝑃𝐿
𝛿=
𝐴𝐸
where 𝛿 = deformation or change in length (in mm)
P = normal force (in N)
L = length of the member (in mm)
A = area of the cross-section (in mm2)
E = elastic modulus (in MPA or N/mm2)

The amount of deformation a material can handle is a basic criteria for engineering
design. Deformation is measured using the quantity “strain,” which is defined as the
change in length over original length (𝜀 = 𝛿/𝐿). Substituting Hooke’s Law (𝜀 = 𝑃/𝐴𝐸)
into this equation gives us the formula for the deformation or change in length of a
member.
Algorithm / Pseudocode
Print “Select formula.”
Scan choice.
Clear screen.
Switch {
Case 0: Exit program.

Case 1: // Formula 1 Variable Selection Screen

Print “Select variable.”


Scan choice.
Clear screen.

Switch {
Case 0: Exit program.
Case 9: Go back to formula selection screen.
Case 1: // Solve for Variable 1.
Print “Input Variable 2.” Scan Variable 2.
Print “Input Variable 3.” Scan Variable 3.
Print “Input Variable 4.” Scan Variable 4.
Solve for Variable 1.
Print “Variable 1=X.”
Case 2: (Follow format for Case 1. Replace variables.)
Case 3: (Follow format for Case 1. Replace variables.)
Case 4: (Follow format for Case 1. Replace variables.)
Default: Print “INVALID.” Go back to variable selection screen.
}
Clear screen.
Go back to formula selection screen.

Case 2: // Formula 2 Variable Selection Screen

Print “Select variable.”


Scan choice.
Clear screen.

Switch {
Case 0: Exit program.
Case 9: Go back to formula selection screen.
Case 1: // Solve for Variable 1.
Print “Input Variable 2.” Scan Variable 2.
Print “Input Variable 3.” Scan Variable 3.
Print “Input Variable 4.” Scan Variable 4.
Print “Input Variable 5.” Scan Variable 5.
Solve for Variable 1.
Print “Variable 1=X.”
Case 2: (Follow format for Case 1. Replace variables.)
Case 3: (Follow format for Case 1. Replace variables.)
Case 4: (Follow format for Case 1. Replace variables.)
Case 5: (Follow format for Case 1. Replace variables.)
Default: Print “INVALID.” Go back to variable selection screen.
}

Clear screen.
Go back to formula selection screen.

Default: Print “INVALID.” Go back to formula selection screen.


}

Test Data
Formula for Bearing Stress

Thickness of
Bearing Stress Compressive Force, P Bolt Diameter, d
the Body, t
60 MPa 12 000 N 20 mm 10 mm
11 MPa 69 300 N 100 mm 63 mm
99 MPa 13 860 N 35 mm 4 mm
63.589 MPa 20 564. 343 N 32.333 mm 10.00 mm
20.816 MPa 52 040 N 100 mm 25 mm
65 MPa 59 995 N 71 mm 13 mm
33.56 MPa 46 000 N 25.38 mm 54 mm
24 MPa 25 050 N 82 mm 12.73 mm

Formula for Deformation / Change in Length

Length of Area of Cross- Elastic


Deformation Normal Force, P
Member, L Section, A Modulus, E
1.44 mm 6 000 N 360 mm 6 mm2 250 000 MPa
1.4 mm 7 339.81 N 412 mm 7.2 mm2 300 000 MPa
4.2 mm 8 655 N 300.87 mm 4 mm2 155 000 MPa
2.56 mm 7 645 N 602 mm 4 mm2 450 000 MPa
10.35 mm 3 478.93 N 864 mm 1.18 mm2 246 114.42 MPa
13.50 mm 9 560 N 567 mm 2.23 mm2 180 000 MPa
6.2 mm 16 326.67 N 120 mm 3.16 mm2 100 000 MPa
2.3 mm 5 455 N 676.72 mm 6.42 mm2 250 000 MPa
8.57 mm 4 025 N 238 mm 0.18 mm2 628 000 MPa
1.21 mm 9 678 N 980 mm 34.68 mm2 226 020.19 MPa
References
LBYEC71 Manual
MEDEFOR Lecture 1 – Axial, Shear, and Bearing Stress by Kirk Kennedy Uy
MEDEFOR Lecture 2 – Stress and Strain by Kirk Kennedy Uy
http://www.cs.cornell.edu/courses/cs1130/2012sp/1130selfpaced/module2.html
http://www.cprogramming.com/tutorial/c/lesson2.html
http://www.cprogramming.com/tutorial/c/lesson3.html
http://www.tutorialspoint.com/ansi_c/c_control_statements.htm
http://www.tutorialspoint.com/cprogramming/c_loops.htm
http://plsql-tutorial.com/plsql-iterative-statements.htm

Anda mungkin juga menyukai