Chapter 4 Denso

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

Flow of Control

Chapter 4

Jheanel Estrada Technological Institute of the Philippines- Manila


Outline

◼ Relational, Equality and Logical Operators


◼ Compound Statement
◼ Expression and Empty Statements
◼ If and If else Statements
◼ While Statement
◼ For and if else statements
◼ Boolean variables
◼ do
◼ goto
◼ switch
Jheanel Estrada Technological Institute of the Philippines- Manila
Introduction

◼ Statements in a program are normally


executed one after another.
- this is called sequential flow of control.

Jheanel Estrada Technological Institute of the Philippines- Manila


Relational, Equality and Logical
Operators
◼ They also have rules of precedence and
associativity that determine precisely how
expressions involving these operators are
evaluated.

Jheanel Estrada Technological Institute of the Philippines- Manila


Relational, Equality and Logical
Operators
◼ Relational Operators
<, >, <= , >=
◼ Equality Operator
==, !=
◼ Logical Operator
! (unary), &&, ||

Jheanel Estrada Technological Institute of the Philippines- Manila


Note:

◼ a<b+c is equivalent to a<(b+c)


◼ a<b<c is not equivalent a<(b<c)
◼ 1+5*d <e is equivalent to (1+(5*d)) <e
◼ a= 9; so 1<a<12 and 1<12 is equivalent
◼ a== b is not the same as a= b
◼ x! = y is not the same as !(x= y)
◼ a==b is not the same as a= = b
◼ n==m is the same as m==n
◼ a!= b is the same as b!=a
Jheanel Estrada Technological Institute of the Philippines- Manila
Note:

◼ a&&b is not the same as a&b


◼ a!=b is the same as b!=a
◼ a-b ==0 is the same as (a-b) == 0
◼ a+1 != 2*b is not the same as ((a+1) != 2) *b
◼ (a&&b) &&c is not the same as a&&(b&&c)

Jheanel Estrada Technological Institute of the Philippines- Manila


Logical Operators and Expressions

Values of:
expr1 expr2 expr1&&expr2 expr1|| expr2

zero zero 0 0
zero nonzero 0 1
nonzero zero 0 1
nonzero nonzero 1 1

Note:
zero = false
nonzero = true
Jheanel Estrada Technological Institute of the Philippines- Manila
Note:

◼ The precedence of && is higher than || but


both operators are of lower precedence than
all unary, arithmetic and relational operators.

Jheanel Estrada Technological Institute of the Philippines- Manila


Examples:

char c = ‘B’;
int i = 3, j= 3, k= 3;
double x= 0.0, y= 2.3;

Expression Eq. Expr Value


i && j && k (i && j) && k 1
x|| i && j- 3 x || (i &&(j-3)) 0

Jheanel Estrada Technological Institute of the Philippines- Manila


Exercise:

Write the equivalent expression and its value.


int a = 1, b= 2, c= 3;
double x = 1.0;
1. a>b && c<d
2. a<!b || !!a
3. a- x || b*c && b/a

Jheanel Estrada Technological Institute of the Philippines- Manila


Answers in the Exercise:

Give the equivalent expression and value of the


ff.
int a = 1, b= 2, c= 3;
double x = 1.0;
Expression:
1. a>b && c<d
answer:
((a>b) && (c<d))
value: 0
Jheanel Estrada Technological Institute of the Philippines- Manila
Examples:

◼ a<!b || !!a
answer:
(a<(!b)) || (!(!a))
value: 1

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:

◼ a- x || b*c && b/a


answer:
((a-x) || ((b*c) && (b/a)))
value: 1

Jheanel Estrada Technological Institute of the Philippines- Manila


The if and the is- else statements

◼ The general form of an if statement is


if (expr)
statement

If expr is nonzero (true), then statement is


executed; otherwise, statement is skipped
and control passes to the next statement.

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:

if ( 5 < 10 )
printf( "Five is now less than ten, that's a big
surprise" );

Jheanel Estrada Technological Institute of the Philippines- Manila


Examples:

Explain briefly the problem with the following


code:

1. if(b=a)
area = A*a;

answer: b==a should be used

Jheanel Estrada Technological Institute of the Philippines- Manila


if- else statement

◼ the general form is:


if (expr)
statement1
else
statement2

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:

if (x<y)
min = x;
else
min = y;

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:

◼Explain briefly the problem with the following


code:
if (i!=j) {
i+= 1;
j+=2;
};
else
i-=j;
Answer: there should be no semicolon after }

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:

◼Explain briefly the problem with the following


code:
…..
if (a == 1)
if (b==2)
printf(“ “);
else
print(“\n”);
Answer: it should be printf
Jheanel Estrada Technological Institute of the Philippines- Manila
Example:

◼What is the value of x, if a= 0?


…..
x = 0;
if (a == 1)
if (b ==2)
x=1;
else
x= 2;

Answer: x = 0
Jheanel Estrada Technological Institute of the Philippines- Manila
The While statement

It is used for repetitive action.


The general form is
while(expr)
statement
next statement
First, expr is evaluated. If it is nonzero(true), then
statement is executed and control is passed back to
the beginning of the while loop then the statement
is executed repeatedly until expr is zero (false).

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:

#include <stdio.h>
main() {
int i = 10;
while ( i > 0 ) {
printf("Hello %d\n", i );
i = i -1;
}
}
the body of the while loop is to be executed
repeatedly until the expression i>0 is eventually
zero.

Jheanel Estrada Technological Institute of the Philippines- Manila


The For Statement

◼ It is used to execute code iteratively.

the general form is:

for ( expr1; expr2;expr3)


statement
next statement

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:

for (j=0;j<5;j++)
printf("j”);

what will be the output?


01234

Jheanel Estrada Technological Institute of the Philippines- Manila


For Loop

◼ If you want to convert a for loop into a while


loop
expr1;
while (expr2) {
statement
expr3;
}
next statement

Jheanel Estrada Technological Institute of the Philippines- Manila


For Loop

◼ Convert this to a while loop.


for (j=0;j<5;j++)
printf("j”);

Jheanel Estrada Technological Institute of the Philippines- Manila


Exercise:

◼ What’s wrong with the code?

for(i= 0, i<n, i++)


x -= 1;

answer: Syntax error, it should be semicolon

Jheanel Estrada Technological Institute of the Philippines- Manila


Exercise:

◼ What happens if expr2 is missing?

for(i= 0; ; i++)
x-=1;

answer:
endless loop

Jheanel Estrada Technological Institute of the Philippines- Manila


The Do Statement

◼ It can be considered a variant of the while


statement
The general form:
do
statement
while(expr);
next statement

Jheanel Estrada Technological Institute of the Philippines- Manila


The Do Statement

#include <stdio.h>
main() {
int i = 10;
do{
printf("Hello %d\n", i );
i = i -1;
}
while ( i > 0 );
}
Jheanel Estrada Technological Institute of the Philippines- Manila
The break and continue statements

◼ These two special statements interrupt the


normal flow of control.
◼ The break statement causes an exit from
the switch statement.
◼ The continue statement causes the current
loop to stop and causes the next iteration
to begin.
◼ It may occur only inside the for, while and do
loops.
Jheanel Estrada Technological Institute of the Philippines- Manila
The Switch Statement

◼ It is a multiway conditional statement


generalizing the if-else statement.

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:
#include <stdio.h>
main()
{
int Grade = 'A';
switch( Grade ) {
case 'A' : printf( "Excellent\n" );
case 'B' : printf( "Good\n" );
case 'C' : printf( "OK\n" );
case 'D' : printf( "Mmmmm....\n" );
case 'F' : printf( "You must do better than this\n" );
default : printf( "What is your grade anyway?\n" );
}
}

Jheanel Estrada Technological Institute of the Philippines- Manila


Output:

Excellent
Good
OK
Mmmmm....
You must do better than this
What is your grade anyway?

Jheanel Estrada Technological Institute of the Philippines- Manila


Example:
#include <stdio.h>
main()
{
int Grade = 'B';
switch( Grade ) {
case 'A' : printf( "Excellent\n" );
break;
case 'B' : printf( "Good\n" );
break;
case 'C' : printf( "OK\n" );
break;
case 'D' : printf( "Mmmmm....\n" );
break;
case 'F' : printf( "You must do better than this\n" );
break;
default : printf( "What is your grade anyway?\n" );
break;
}
Jheanel Estrada Technological Institute of the Philippines- Manila
}
Output:

Good

question:
What is a default condition?
Answer:
If we have inputted something other than A,
B,C, D and F, for example L, the output will
be:
What is your grade anyway?
Jheanel Estrada Technological Institute of the Philippines- Manila
Exercise:
◼ Convert the switch statement into an if- else statement:

switch (c) {
case ‘a’:
case ‘A’:
count++;
break;
case ‘c’:
count --;
break;
case ‘D’;
dcount++;
default:
othercount--;
break;
} Jheanel Estrada Technological Institute of the Philippines- Manila
Answer:
if (c==‘a’ || c==‘A’) {
count++;
}
else if (c==‘c’) {
count--;
}
else if (c==‘D’) {
dcount++;
}
else {
othercount--;
}
Jheanel Estrada Technological Institute of the Philippines- Manila

You might also like