Slides Seminar 3
Slides Seminar 3
An Introduction to Programming
• Branching statements
• if, if-else, nested if-else
• switch
• Conditional ternary operator (?:)
• Loop statements
• For
• While
• Do-while
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:
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
11
Loop statements
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
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
{
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
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:
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:
int main()
{
int num=0;
while (num<10)
{
num++;
if (num==5)
continue;
printf("%d\n",num);
}
return 0;
29
}