Structured Programming YIBS Lesson 4
Structured Programming YIBS Lesson 4
If the condi on is true, then execute the statement. If we do not provide the curly braces a er the
if(condi on), then by default the if statement will consider the first statement immediately below to
be inside its block.
Example
4.1.2 if-else
The if statement alone tells us that if a condi on is true it will execute a block of statements and if the
condi on is false it won’t. But what if we want to do something else when the condi on is false? Here
comes the C else statement. We can use the else statement with the if statement to execute a block
of code when the condi on is false. The if-else statement consists of two blocks, one for false
expression and one for true expression.
Syntax
N. MPATCHIE HENSCHEL 21
STRUCTURED PROGRAMMING
Example
Example
N. MPATCHIE HENSCHEL 22
STRUCTURED PROGRAMMING
4.1.4 If else-if
The if else-if statements are used when the user has to decide among mul ple op ons. The C if
statements are executed from the top down. As soon as one of the condi ons controlling the if is true,
the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If
none of the condi ons is true, then the final else statement will be executed. If else-if is similar to the
switch statement.
Syntax
Example
N. MPATCHIE HENSCHEL 23
STRUCTURED PROGRAMMING
Note: The switch expression should evaluate to either integer or character. It cannot evaluate any
other data type. The keyword break is used to exit the switch statement once a case is executed.
Without break, the program will con nue execu ng the subsequent cases (this is called “fall through”).
The keyword default is executed if none of the case values match the expression.
Example
4.1.7 Assignment
a. Write a C program that asks for a number from a user and says whether it is posi ve or nega ve.
b. Write a C program that asks for a number from a user and says whether it is even or odd.
c. Write a C program that asks for 3 numbers from a user and says which is the greatest.
d. Write a C program that asks for 3 numbers from a user and says which is the greatest.
e. Write a C program which checks the value of a day and prints the corresponding day of the week
using if-else or switch. If the value is not between 1 and 7, it prints “Invalid day!”
f. Write a C program which asks for an operator (+,-,*,/) from a user and performs an opera on
based on two numbers given by the user and the chosen operator.
N. MPATCHIE HENSCHEL 24
STRUCTURED PROGRAMMING
In for loop, a loop variable is used to control the loop. Firstly, we ini alize the loop variable with some
value, then check its test condi on. If the statement is true then control will move to the body and
the body of for loop will be executed. Steps will be repeated ll the exit condi on becomes true. If the
test condi on will be false then it will stop.
Ini aliza on Expression: In this expression, we assign a loop variable or loop counter to some
value. for example: int i=1;
Test Expression: In this expression, test condi ons are performed. If the condi on evaluates
to true then the loop body will be executed and then an update of the loop variable is done.
If the test expression becomes false then the control will exit from the loop. for example, i<=9;
Update Expression: A er execu on of the loop body loop variable is updated by some value
it could be incremented, decremented, mul plied, or divided by any value.
Example
N. MPATCHIE HENSCHEL 25
STRUCTURED PROGRAMMING
b. while loop
While loop does not depend upon the number of itera ons. In for loop the number of itera ons was
previously known to us but in the While loop, the execu on is terminated on the basis of the test
condi on. If the test condi on will become false then it will break from the while loop else body will
be executed.
Syntax
Example
N. MPATCHIE HENSCHEL 26
STRUCTURED PROGRAMMING
Example
Loop control statements in C programming are used to change execu on from its normal sequence.
Name Descrip on
the break statement is used to terminate the loop statement. It transfers the
break;
execu on to the statement immediately following the loop or switch.
con nue statement forces the program control to execute the next itera on
con nue; of the loop. As a result, the code inside the loop following the con nue
statement will be skipped and the next itera on of the loop will begin.
labelName: goto statement is a jump statement that transfers the control flow of a
statement; program to a labeled statement within the scope of the same func on,
goto labelName; breaking the normal flow of a program.
N. MPATCHIE HENSCHEL 27
STRUCTURED PROGRAMMING
4.2.5 Assignment
a. Write a C program that prints all integer numbers from 1 ll a specified number.
b. Write a C program that prints the sum of all integer numbers from 1 ll a specified number.
c. Write a C program that counts to 100 by tens.
d. Write a C program that only prints even numbers between 0 and 100 (inclusive).
e. Write a C program that only prints odd numbers between 0 and 100 (inclusive).
f. Write a C program that prints the powers of a specified number.
g. Write a C program which prints the mul plica on table of a specified number.
h. Write a C program which prints the mul plica on table from 1 ll a specified number.
N. MPATCHIE HENSCHEL 28