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

04-C-Control-Structures

Uploaded by

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

04-C-Control-Structures

Uploaded by

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

C CONTROL

STRUCTURES
CSIT121 – Fundamentals of Programming

Prepared by: Jay Vince D. Serato


C Control Structures

This lesson begins your study of statements that control the


flow of program execution.

You will learn to use if and switch statements to select one


statement group to execute from many alternatives.

First, the chapter discusses conditions and logical expressions


because the if statement relies on them
Prepared by: Jay Vince D. Serato
C Control Structures

Prepared by: Jay Vince D. Serato


C Control Structures

• Control structures control the flow of execution in a


program or function.

• The C control structures enable you to combine individual


instructions into a single logical unit with one entry point and
one exit point.

Prepared by: Jay Vince D. Serato


C Control Structures
• Instructions are organized into three kinds of control
structures to control execution flow: sequence, selection, and
repetition.

• Until now we have been using only sequential flow.

• A compound statement, written as a group of statements


bracketed by { and }, is used to specify sequential flow.

Prepared by: Jay Vince D. Serato


Conditions

Prepared by: Jay Vince D. Serato


Conditions
• A program chooses among alternative statements by testing
the value of key variables.

• Example: A human’s temperature of 37°C is considered


normal. Getting a temperature over 37°C indicates that a
person has fever.

• A program that gets a person’s temperature as data should


compare that value to 37 and display a warning message if
the temperature is over 37.

Prepared by: Jay Vince D. Serato


Conditions
• If temperature is a type int variable, the expression
temperature > 37
performs the necessary comparison and evaluates to 1 (true)
when temperature is over 37;
the expression evaluates to 0 (false) if temperature is not
greater than 37.

• Such an expression is called a condition because it


establishes a criterion for either executing or skipping a
group of statements.
Prepared by: Jay Vince D. Serato
Conditions
Operator Meaning Type
< less than relational
> greater than relational
<= less than or equal to relational
>= greater than or equal to relational
== equal to equality
!= not equal to equality

Prepared by: Jay Vince D. Serato


Conditions
• variable <operator> variable
• total == payment

• variable <operator> constant


• temperature > 37

Prepared by: Jay Vince D. Serato


Conditions
x
-5

• x <= 0

• x less than or equal to 0

• 1 (TRUE)

Prepared by: Jay Vince D. Serato


Conditions
x NUM
19 19

• x < NUM

• x less than NUM

• 0 (FALSE)

Prepared by: Jay Vince D. Serato


Conditions
x y
-5 7

• x >= y

• x greater than or equal to y

• 0 (FALSE)

Prepared by: Jay Vince D. Serato


Conditions
item min
1.5 1

• item > min

• item greater than min

• 1 (TRUE)

Prepared by: Jay Vince D. Serato


Conditions
m
‘M’

• m == ‘M’

• m equal to ‘M’

• 1 (TRUE)

Prepared by: Jay Vince D. Serato


Conditions
num num2
45 45

• num != num2

• num not equal to num2

• 0 (FALSE)

Prepared by: Jay Vince D. Serato


Conditions: Logical Operators

• With the three logical operators

• && (AND)
• || (OR)
• ! (NOT)

we can form more complicated conditions or logical


expressions.
Prepared by: Jay Vince D. Serato
Conditions: Logical Operators

• salary < MIN_SALARY || dependents > 5

• temperature > 31.0 && humidity >= 0.90

• n >= 0 && n <= 100

Prepared by: Jay Vince D. Serato


Conditions: Logical Operators
salary < MIN_SALARY || dependents > 5

• This logical expression determines whether an employee is


eligible for special funds.
• It evaluates 1 (true) if either the condition
salary < MIN_SALARY
or the condition
dependents > 5
is true.
Prepared by: Jay Vince D. Serato
Conditions: Logical Operators
salary < MIN_SALARY || dependents > 5

#define MIN_SALARY 350


• salary = 310, dependents = 8
• salary = 381, dependents = 6
• salary = 300, dependents = 2
• salary = 350, dependents = 5
• salary = 349, dependents = 6
Prepared by: Jay Vince D. Serato
Conditions: Logical Operators
temperature > 31.0 && humidity >= 0.90

• This logical expression describes an unbearable summer day.


• It evaluates 1 (true) if both the condition
temperature > 31.0
and the condition
humidity >= 0.90
are true.
Prepared by: Jay Vince D. Serato
Conditions: Logical Operators
temperature > 31.0 && humidity >= 0.90

• temperature = 30.5, humidity = 0.92


• temperature = 32.7, humidity = 0.90
• temperature = 29.4, humidity = 0.83
• temperature = 31.2, humidity = 0.88

Prepared by: Jay Vince D. Serato


Conditions: Logical Operators
n >= 0 && n <= 100

• This logical expression evaluates n to be between 0-100.


• It evaluates 1 (true) if both the condition
n >= 0
and the condition
n <= 100
are true.
Prepared by: Jay Vince D. Serato
Conditions: Logical Operators
n >= 0 && n <= 100

• n = 15
• n = -89
• n = 100
•n=0

Prepared by: Jay Vince D. Serato


Conditions: Logical Operators
!(n >= 0 && n <= 100)

• This logical expression evaluates n to not be between 0-100.


• It evaluates 0 (false) if both the condition
n >= 0
and the condition
n <= 100
are true.
Prepared by: Jay Vince D. Serato
Conditions: Logical Operators
!(n >= 0 && n <= 100)

• n = 15
• n = -89
• n = 100
•n=0

Prepared by: Jay Vince D. Serato


Conditions: The && Operator
operand1 &&
operand1 operand2
operand2
nonzero (true) nonzero(true) 1 (true)
nonzero (true) 0 (false) 0 (false)
0 (false) nonzero (true) 0 (false)
0 (false) 0 (false) 0 (false)

Prepared by: Jay Vince D. Serato


Conditions: The || Operator
operand1 ||
operand1 operand2
operand2
nonzero (true) nonzero(true) 1 (true)
nonzero (true) 0 (false) 1 (true)
0 (false) nonzero (true) 1 (true)
0 (false) 0 (false) 0 (false)

Prepared by: Jay Vince D. Serato


Conditions: The ! Operator

operand !operand
nonzero (true) 0 (false)
0 (false) 1 (true)

Prepared by: Jay Vince D. Serato


Conditions: Operator Precedence
Operator Precedence
! HIGHEST
*/%
+-
< <= > >=
== !=
&&
||
Prepared by: Jay Vince D. Serato LOWEST
Conditions:
x Operator
y Precedence
z flag
3 4 2 0

!flag || (y + z >= x - z)

Prepared by: Jay Vince D. Serato


The if Statement

Prepared by: Jay Vince D. Serato


If Statement

You now know how to write a C expression that is the


equivalent of a question such as “Is your temperature greater
than 37 degrees Celcius?”

Next, we need to investigate a way to use the value of the


expression to select a course of action. In C, the if statement is
the primary selection control structure.

Prepared by: Jay Vince D. Serato


If Statement
if (temperature > 37) {
printf(“You have a fever!\n”);
else {
printf(“You are healthy.\n”);
}

• This statement selects one of the two calls to printf.

Prepared by: Jay Vince D. Serato


If Statement
if (temperature > 37) {
printf(“You have a fever!\n”);
} else {
printf(“You are healthy.\n”);
}
• It selects the statement following the parenthesized condition
if the condition evaluates to 1 (true) (that is, if temperature is
greater than 37), or it selects the statement following else if
the condition evaluates to 0 (false) (if temperature is not
greater than 37).
Prepared by: Jay Vince D. Serato
If Statement
if (score >= 51) {
printf(“You passed!\n”);
} else {
printf(“You failed.\n”);
}

Prepared by: Jay Vince D. Serato


If Statement
char ans;
if (letter == ‘P’) {
printf(“Passed\n”);
printf(“Congrats!\n”);
} else {
printf(“Failed\n”);
printf(“Do you want to take this subject next semester? [Y/N]”);
scanf(“%c”, &ans);
}

Prepared by: Jay Vince D. Serato


Multiple-Alternative Decision
if (letter == ‘P’) {
printf(“Passed\n”);
} else if (letter == ‘C’) {
printf(“Conditional\n”);
printf(“Please take the removals.”);
} else {
printf(“Failed\n”);
}
Prepared by: Jay Vince D. Serato
Multiple-Alternative Decision
if (num > 0) {
printf(“Positive\n”);
} else if (num < 0) {
printf(“Negative\n”);
} else {
printf(“Zero\n”);
}

Prepared by: Jay Vince D. Serato


Nested If Statements
• We use nested if statements (one if statement inside
another) to code decisions with multiple alternatives.

Prepared by: Jay Vince D. Serato


Nested If Statement
if (road_status == 'S’) {
if (temp > 0) {
printf("Wet roads ahead\n");
printf("Stopping time doubled\n");
} else {
printf("Icy roads ahead\n");
printf("Stopping time quadrupled\n"); }
} else {
printf("Drive carefully!\n");
}
Prepared by: Jay Vince D. Serato
Switch Statements
• The switch statement may also be used in C to select one of
several alternatives.

• The switch statement is especially useful when the selection


is based on the value of a single variable or of a simple
expression (called the controlling expression) .

Prepared by: Jay Vince D. Serato


Multiple-Alternative Decision
if (letter == ‘P’) {
printf(“Passed\n”);
} else if (letter == ‘C’) {
printf(“Conditional\n”);
printf(“Please take the removals.”);
} else {
printf(“Failed\n”);
}
Prepared by: Jay Vince D. Serato
Switch Statement
switch(letter) {
case ‘P’:
printf(“Passed\n”);
break;
case ‘C’:
printf(“Conditional\n”);
break;
default:
printf(“Failed\n”);
}

Prepared by: Jay Vince D. Serato


Switch Statement
switch(letter) {
case ‘P’:
case ‘p’:
printf(“Passed\n”);
break;
case ‘C’:
case ‘c’:
printf(“Conditional\n”);
break;
default:
printf(“Failed\n”);
}
Prepared by: Jay Vince D. Serato

You might also like