Unit 3
Unit 3
Structured Design-1
Mr. Pathan Bilalkhan R.
Assistant Professor
Computer Science & Engineering
CHAPTER-3
Conditional Flow Statements, Iterative Statements, Jumping
Statements
Contents
if (test Expression)
{
// codes inside the body of if
}
else
{
// codes inside the body of else
}
Example: if...else statement
// Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
1.3 if...else if....else Statement
● The if...else statement executes two different codes
depending upon whether the test expression is true or false.
Sometimes, a choice has to be made from more than 2
possibilities.
{
// codes
}
Flowchart of For Loop
Example: for loop
// Program to calculate the sum of first n for(count = 1; count <= n; ++count)
natural numbers
{
// Positive integers 1,2,3...n are known as
natural numbers sum =sum+count;
}
while (testExpression)
{
//codes
}
Example: while loop
/ Program to find factorial of // loop terminates when number is less than
a number or equal to 0
while (number > 0) {
// For a positive integer n,
factorial = 1*2*3...n // factorial = factorial*number;
factorial *= number;
--number;
#include<stdio.h>
}
int main(){
int number; printf("Factorial= %lld", factorial);
long factorial; return 0;
printf("Enter an integer: ");
scanf("%d",&number);
factorial =1;
2.3 do...while loop
► The do..while loop is similar to the while loop with one important
difference.
do
{
// codes
}
while (testExpression);
Example: do...while loop
// Program to add numbers until user enters zero #include
<stdio.h>
int main() {
double number, sum = 0;
// loop body is executed at least once
do{
printf("Enter a number: ");
scanf("%lf", &number); sum
+= number;
}while(number != 0.0);
printf("Sum %.2lf",sum); return 0;}
2.4 Nestedloops (Con..)
► Syntax while while(condition)
loop {
while(condition)
{
statement(s);
}
statement(s);
}
3. Break And Continue Statement
► What is BREAK meant?
► What is CONTINUE meant?
Syntax of if...else if....else statement.
► The break statement terminates the loop immediately when it is
encountered.
break;
How break statement works?
Flowchart Of Break Statement
Example: break statement
3.2 Continue Statement
► The continue statement skips some statements inside the
loop.
► The continue statement is used with decision making stateme such
as if...else.
► Syntax of continue Statement
continue;
Flowchart of Continue Statement
How Continue Statement Works?
Example: continue statement
// Program to calculate sum of // If user enters negative number,
maximum of 10 numbers loop is terminated
// Negative numbers are if(number < 0.0) {
skipped
from calculation continue;
}
# include <stdio.h> // sum = sum + number;
int main(){ sum += number;
int i; }
printf("Sum = %.2lf",sum);
double number, sum = 0.0;
return 0;
for(i=1; i <= 10; ++i)
}
{ printf("Enter a n%d:
",i);
scanf("%lf",&number);
3.2. Switch
Statement
► The if...else if…else statement allows you to execute a block code among many
alternatives. If you are checking on the value of a single variable in if...else if…
else statement, it is better to use switch statement.
► The switch statement is often faster than nested if...else (not always).
easy to understand.
Syntax of switch...case
switch (n){
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to
constant2; break;
….
default:
// code to be executed if n doesn't match any constant
}
Switch Statement Flowchart
Example: switch Statement
// Program to create a simple calculator
// Performs addition, subtraction, multiplication or division
depending the input from user
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator); printf("Enter
two operands: ");
scanf("%lf %lf",&firstNumber,
&secondNumber);
Syntax of if...else if....else statement.
switch(operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber); break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber); break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber); break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber); break;
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
What is Label?
► The label is an identifier. When goto statement is encountered, control of the
program
jumps to label: and starts executing the code.
Example: goto Statement
// Program to calculate the sum and average of maximum of 5
number
// If user enters negative number, the sum and average of previously
entered positive number is displayed
# include <stdio.h>
int main(){