0% found this document useful (0 votes)
4 views29 pages

Slides Seminar 3

Uploaded by

juan12manoll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views29 pages

Slides Seminar 3

Uploaded by

juan12manoll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Computer Engineering:

An Introduction to Programming

Seminar 3 – Control Statements (2)


Control Statements

• Branching statements
• if, if-else, nested if-else
• switch
• Conditional ternary operator (?:)

• Loop statements
• For
• While
• Do-while

• Jump statements (careful !!!!!)


• Break
• Continue
• Goto

2
Conditional statements
switch statement
switch (variable or expression)
{
case value 1:
statement 1;
statement 2;
break;
case value 2:
statement 1;
statement 2;
break;
...........
default:
statement 1;
statement 2;
break;
3 Seminario 1.- Programación
} estructurada
Conditional statements
switch statement
• Execution of switch statement begins by evaluating
the variable inside the switch parenthesis.
• The variable can be an integer or a character. (int or
char)
• The variable value is then compared with each case
value. The comparison operator „==‟ is implicitly
used to compare the variable with each case.
• There can be any number of „cases‟ inside a “switch
statement” block but there cannot be two „cases‟ with
the same value.
• Case labels always end with a colon ( : ). Each of
these cases is associated with a block.
4
Conditional statements
switch statement
• If the first case value is not equal the variable value,
the program control moves to the next case value
and so on.
• When a case value matches with a variable
value, the statements that belong to this case are
executed.
• if no case values are equal to the variable value, the
set of statements that follow default: will be
executed.
• The word break is used to break from a block of
curly braces. The switch block has two curly braces {
}. The keyword break causes program control to exit
5
from the switch block.
Conditional statements
switch statement
• If „break‟ is omitted at the end of each case, the
sentences in the following case (s) are executed,
until a „break‟ is found or if it does not exist, until the
end of the sentence is reached („}‟). This allows to
define the same set of sentences for several cases

6
Conditional statements
switch statement
#include <stdio.h>
void main()
{
int num;
printf("Hello user, Enter a number: 1 or 2");
scanf("%d",&num);
switch(num)
{
case 1:
printf("UNO");
break;
case 2:
printf("DOS");
break;
default:
printf("WRONG ENTRY");
}
7 Seminario 1.- Programación
} estructurada
Conditional statements
switch statement
Proposed exercise:

Write a C program that accepts a number between 1


and 7 and prints the corresponding day of the week.

8 Seminario 1.- Programación estructurada


Conditional statements
Conditional ternary operator
• Another way to express an if-else statement is by
introducing the ?: ternary operator.
• In a conditional expression, the ?: operator has only
one statement associated with the “if” and the “else”.

Example:
#include <stdio.h>
int main()
{
int n;
printf(“Enter a number\n”);
scanf(“%d”,&n);
(n%2==0)? printf(“Even\n”):printf(“Odd\n”);
return 0;
9 Seminario 1.- Programación
} estructurada
Loop statements

• In looping, a program executes the sequence of


statements many times until the stated condition
becomes false.
• A loop consists of two parts, a body of a loop and a
control statement.
• The control statement is a combination of some
conditions that direct the body of the loop to execute
until the specified condition becomes false.
• The control conditions must be well defined and
specified, since otherwise the loop will execute an
infinite number of times.
• An infinite loop is also called as an "Endless loop“.
10
Loop statements

• Depending upon the position of the condition or control


statement in a program, a loop is classified into two
types:

1. Entry-controlled loop: the condition is checked


before executing the body of the loop. It is also
called a pre-checking or pre-test loop.

1. Exit-controlled loop: the condition is checked


after executing the body of the loop. It is also
called a post-checking or post-test loop.

11
Loop statements

• C programming language provides us with


three types of loop constructs:

1. The while loop (entry-controlled loop)


2. The do-while loop (exit-controlled loop)
3. The for loop (entry-controlled loop)

12 Seminario 1.- Programación estructurada


Loop statements
while loop
• A while loop is the most straightforward looping
structure. The basic format of while loop is as follows:

while (condition)
{
statements
}

13
Loop statements
while loop

• Execution:
1. The condition is checked for TRUE first.
2. If it is TRUE then all statements inside curly braces
are executed.
3. After the body of the loop is executed, the program
control comes back to check if the condition has
changed or to check if it is still TRUE.
4. The statements inside braces are executed
repeatedly, as long as the condition is TRUE.
5. When the condition turns FALSE, program control
exits from while loop.
6. After exiting the loop, the control goes to the
14 statements which are immediately after the loop.
Loop statements
while loop

• The body of a loop can contain more than one


statement. If it contains only one statement, then the
curly braces are not compulsory.
• In while loop, if the condition is not true the first time,
then the body of the loop never will be executed.
• While loop is an entry-controlled loop.

15
Loop statements
while loop
Example:

#include<stdio.h>

int main()
{
int num=1; // initializing the control variable
while (num<=10) // condition on the control variable
{
printf("%d\n",num);
num++; // control variable update
}
return 0;
16 }
Loop statements
do-while loop

• do-while loop is similar to the while loop except that the


condition is always executed after the body of the loop.
• The basic format of while loop is as follows

do
{
statements
} while (condition); // notice the semi-colon (;)

17
Loop statements
do-while loop
• Execution:
1. The set of statements inside braces are executed first.
2. The condition inside while parenthesis is checked only
after finishing the first time execution of statements inside
braces. If the condition is TRUE, then statements are
executed again.
3. This process continues as long as the condition is TRUE.
4. Program control exits the loop once the condition turns
FALSE.
• If the body of the loop contains only one statement, then
the curly braces are not compulsory.
• In the do-while loop, the body of a loop is always
executed at least once
18 • do-while loop is an exit-controlled loop.
Loop statements
do-while loop
Example:

#include<stdio.h>

int main()
{
int num=1; // initializing the control variable
do {
printf("%d\n",num);
num++; // control variable update
} while (num<=10) ; // condition on the control variable

return 0;
19 }
Loop statements
do-while loop
An example of a typical use of do-while:

#include <stdio.h>
int main()
{
int n;
char ch;
do {
printf(“Enter a number\n”);
scanf(“%d”,&n);
(n%2==0)? printf(“Even\n”):printf(“Odd\n”);
printf(“Do you want to enter another number? y/n \n”);
scanf(“ %c”, &ch);
} while (ch!=„n‟);
return 0;
20
}
Loop statements
for loop

• A for loop is a more efficient loop structure in 'C'


programming.
• The general structure of for loop is as follows:

for (initialization statements; condition; iteration statements)


{
statements;
}

21
Loop statements
for loop
• Execution:
1. At first, the for loop executes the statements given as
initialization statements. This initialization is performed
only once.
2. Then the condition statement is evaluated.
3. If conditions are TRUE, then the block of statements
inside the curly braces is executed.
4. After the curly braces statements are fully exected, the
control moves to the "iteration" statements.
5. After executing iteration statements, control comes back
to condition statements.
6. Condition statements are evaluated again for TRUE or
FALSE. If TRUE, the curly brace statements are
executed.
22 7. This process continues until the condition turns FALSE.
Loop statements
for loop
• The for loop is an entry-controlled loop.
• Usually, the for loop is used when an instruction is to be
executed for a predefined number of times:
#include<stdio.h>

int main()
{
int num;
for (num=1;num<=10;num++) //common mistake: num=10
printf("%d\n",num);
return 0;
}
• Note that the three parts: the initialization of the control
variable, the control variable update and the condition on the
23
control variable are within the parenthesis and separated by ;
Loop statements
for loop
• Atypical uses:

1. The loop can be controlled by one or several variables.


2. There can be an expression with multiple conditions.
3. If the loop control variable has a previous value, we
can ignore the initialization.

i=2;j=3;
for ( ; (i<10)&&(j>10) ; i=i+2, j=j-2)
printf(“%d %d\n”,i,j);

24
Loop statements
which loop to select
• Analyze the problem and check whether it requires an
entry-controlled or exit-controlled loop.
• If an entry-controlled is required, use a while or for loop.
• If an exit-controlled is required, use a do-while loop.
• If the number of iterations is predefined, use a for loop

25
Loop statements

Proposed exercise:

Write a program that calculates the average of N


positive numbers read from keyboard. The program will
end when the user writes a negative or zero number.

Write the program using all the loop statements.

Which loop fits best?

26 Seminario 1.- Programación estructurada


Loop statements
#include <stdio.h>
int main()
{
float average;
int n, add=0, i=0, x=0;
printf(“Enter a number\n ”);
scanf(“%d”, &n);
while (n>0)
{
add=add + n;
i++;
printf(“Enter a number\n ”);
scanf(“%d”, &n);
x=1;
}
average=(x==1)? (float)add/i: 0;
printf(“the average is %f\n”, average);
return 0;
27
}
Jump statements
break
• Break is used to exit from a block of curly braces.
• Break in a loop is used to stop it immediately, but we must
always stop it through the condition expression.
• We should use the break statement only in the switch
statement.

printf ("Insert a number\n");


scanf ("%d", &num);
while (num > 0)
{
if (num == 3)
break;
printf ("%d\n", num);
printf ("Insert a number\n");
scanf ("%d", &num);
28
}
Jump statements
continue
• Continue is used to jump to the next iteration but remain in the
loop:

int main()
{
int num=0;
while (num<10)
{
num++;
if (num==5)
continue;
printf("%d\n",num);
}
return 0;
29
}

You might also like