LESSON2 C Programming
LESSON2 C Programming
LESSON2 C Programming
C Flow Control
OBJECTIVES:
• Familiarize yourself with control
structures;
• Know how to use logical expressions in C
programming;
• Learn how to use if ... else statements;
• and appreciate the importance of C flow
controllers.
TRULALA (TRUE)
WIZ (FALSE)
GAME
TRULALA (TRUE) or WIZ (FALSE)
2. In the if statement…
If test expression is evaluated to
FALSE, statements inside the body
of if are executed.
TRULALA (TRUE) or WIZ (FALSE)
3. In the if statement…
If the test expression is evaluated
to TRUE, statements inside the
body of if are not executed.
TRULALA (TRUE) or WIZ (FALSE)
if (condition)
{
// code
}
Flowchart of
if statement in C
How if statement works?
The if statement evaluates the test expression
inside the parenthesis ().
• If the test expression is evaluated to TRUE,
statements inside the body of if are executed.
• If the test expression is evaluated to FALSE,
statements inside the body of if are not
executed.
if Statement
Let's see a simple example of
C language if statement.
Example 1: if statement
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number); // true if number is less than 0
if (number < 0)
{
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
OUTPUT 1:
Enter a number: -2
You entered -2.
The if statement is easy.
When the user enters -2, the test expression number<0 is
evaluated to true. Hence, You entered -2 is displayed on the
screen.
OUTPUT 2:
Enter a number: 5
The if statement is easy.
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output
return 0;
}
Possible Outputs
a b
vowel consonant
#include<stdio.h>
int main()
{
char ch;
printf(“enter an alphabet: \n”);
scanf(“%c”, &ch);
1st Number = 12
2nd Number = 25
3rd Number = 52
The 3rd Number is the greatest among three
LO.2. LOOP
1. for Loop
In programming, a loop is used to repeat a block of code until the
specified condition is met.
C programming has three types of loops:
• for loop
• while loop
• do...while loop
The syntax of the for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}
How for loop works?
• The initialization statement is executed only once.
• Then, the test expression is evaluated. If the test
expression is evaluated to false, the for loop is terminated.
• However, if the test expression is evaluated to true,
statements inside the body of the for loop are executed,
and the update expression is updated.
• Again the test expression is evaluated.
This process goes on until the test expression is false. When
the test expression is false, the loop terminates.
Flowchart of for loop
Example 1: for loop Output
// Print numbers from 1 to 10
#include <stdio.h> 1 2 3 4 5 6 7 8 9 10
int main() {
int i;
return 0;
}
2. while loop
The syntax of the while loop is:
while (testExpression) {
// the body of the loop
}
#include <stdio.h>
1
int main() {
int i = 1; 2
while (i <= 5) { 3
printf("%d\n", i);
++i;
4
}
return 0;
5
}
Here, we have initialized i to 1.
When i = 1, the test expression i <= 5 is true. Hence, the body of the while loop is executed. This
prints 1 on the screen and the value of i is increased to 2.
Now, i = 2, the test expression i <= 5 is again true. The body of the while loop is executed again. This
prints 2 on the screen and the value of i is increased to 3.
This process goes on until i becomes 6. Then, the test expression i <= 5 will be false and the loop
terminates.
3. do...while loop
• The do..while loop is similar to the while loop with one important
difference. The body of do...while loop is executed at least once.
Only then, the test expression is evaluated.
• The syntax of the do...while loop is:
do {
// the body of the loop
}
while (testExpression);
printf("Sum = %.2lf",sum);
return 0;
}
LO.3.
break;
• Output
// if the user enters a negative number, break the loop
if (number < 0.0) {
This program calculates the
break; sum of a maximum of 10
}
numbers. Why a maximum of
sum += number; // sum = sum + number; 10 numbers? It's because if the
}
user enters a negative number,
printf("Sum = %.2lf", sum); the break statement is
return 0;
executed. This will end the for
} loop, and the sum is displayed.
C switch Statement
The switch statement allows us to execute one code block
among many alternatives.
You can do the same thing with the if...else..if ladder. However,
the syntax of the switch statement is much easier to read and
write.
Syntax of switch...case
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
How does the switch statement work?
• The expression is evaluated once and compared with the values of
each case label.
• Ifthere is a match, the corresponding statements after the
matching label are executed. For example, if the value of the
expression is equal to constant2, statements after case
constant2: are executed until break is encountered.
• If there is no match, the default statements are executed.
Notes:
• If
we do not use the break statement, all statements after the
matching label are also executed.
• The default clause inside the switch statement is optional.
switch Statement Flowchart
Example: Simple Calculator Output
// Program to create a simple calculator
#include <stdio.h>
switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break; The - operator entered by the user is
case '-':
stored in the operation variable. And,
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
two operands 32.5 and 12.4 are stored
in variables n1 and n2 respectively.
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2); Since the operation is -, the control of
break;
the program jumps to
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
printf("%.1lf - %.1lf = %.1lf", n1, n2, n1-n2);
goto label;
... .. ...
... .. ...
label:
statement;
The label is an identifier. When the goto statement is
encountered, the control of the program jumps to label: and starts
executing the code
Example: goto Statement Ou
// Program to calculate the sum and average of positive numbers tput
// If the user enters a negative number, the sum and average are displayed.
1. Enter a number: 3
#include <stdio.h> 2. Enter a number: 4.3
int main() { 3. Enter a number: 9.3
4. Enter a number: -2.9
const int maxInput = 100;
int i; Sum = 16.60
double number, average, sum = 0.0; Average = 5.53
for (i = 1; i <= maxInput; ++i) {
printf("%d. Enter a number: ", i);
scanf("%lf", &number);
jump:
average = sum / (i - 1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}