Control Statements In C
Lecturer: Shimirwa Aline Valerie
 Email: a.shimirwa@ines.ac.rw
 Tel: 0784931189
 Conditional statements in C
 A Program is usually not limited to a linear sequence of The if Statement
 instructions. In real life, a program usually needs to  A simple condition is expressed in the form:
 change the sequence of execution according to some
 conditions. if (condition)
 In C, there are many condition statements such as: statement;
  If statement
  If-else statement  It starts with the keyword if; , followed by a condition
  If-else-if ladder statement (a logical expression) enclosed within parenthesis,
  Nested if statement followed by the result statement. The resulting
  Switch Case statement
 statement is executed if the condition is evaluated as
 TRUE. Note that there is no semicolon (;) after the
 condition expression. Consider the following example:
 The if Statement cont..
 By running the code in this slide, If the value of the Example 2( if statement): getting the input from the
 variable “marks ” is greater than 50 , the message “You user:
 have passed the exam! ” is displayed on the screen;
 otherwise the statement is skipped and no message is
 displayed.
 Executing the program on the right side with different
 inputs will behave as follows:
  Case 1: Enter marks: 73
 You have passed the exam!
  Case 2: Enter marks: 34
  Case 3: Enter marks: 50
 In the second and third cases, the message “You have
 passed the exam! ” will not be displayed.
 The if Statement cont..
 More than one statement can be executed as a result of the Example 3: program
 condition by embedding set of statements in a block
 (between two braces {} ).
Example 3:
  Write a program which accepts a number (an amount
 of money to be paid by a customer in dollars). If the
 amount is greater than or equal to 1000 dollars, a 5%
 discount is given to the customer. Then display the
 final amount that the customer has to pay.
 First the program needs to check whether the given amount
 is greater than or equal to 1000; if it is the case a 5%
 discount should be given. Then the final amount needs to be
 displayed. All these are done only if the condition is TRUE.
 So instructions which compute discount and final amount
 should be executed as a block.
 The if Statement cont..
 Executing the program above with 1250.25 as the keyboard input
 display the following: Example 4: program
 Enter amount: 1250.25
 Discount: 62.51
 Total: 1187.74
 In the program above if the condition is TRUE, the set of
 statements inside the block are executed. If the condition is FALSE
 (i.e., if the amount is less than 1000) those statements will not be
 executed.
Example 4:
 Modify Program in example 3 so that it displays the message “No
 discount…” if the amount is less than 1000.
 To do that, another if clause is required to check whether the amount
 is less than 1000. The second if clause can be used before or after
 the first (existing) if clause. Program here has been modified from
 the previous to address this.
 The if-else Statement
 The if-else structure takes the form:
 In many programs it is required to perform some action
  if (condition)
 when a condition is TRUE and another action when it is
 FALSE. In such cases, the if clause can be used to check the true-statements;
 TRUE condition and act upon it. However it does not act  else
 upon the FALSE condition.
 Therefore the expression resulting the FALSE condition false-statements;
 needs to be reorganized. In example 4 when we wanted to  When the condition is evaluated, one of the two
 identify the case where the amount is not greater than statements will be executed and then the program resumes
 1000 (the FALSE case) we were checking whether it is less its original flow. Blocks make it possible to use many
 statements rather than just one.
 than 1000 <1000 ).
 The C language allows us to use the if-else statement in
 such scenarios. You can include both the cases (TRUE and
 FALSE) using the if-else structure.
 The if-else Statement cont..
 Example :
 Write a C program that asks the users their age, and
 based on the condition, the program will display the
 message to them whether they are allowed to get
 married or not. The program is on the right side of
 the slide.
 The if-else-if ladder Statement
 In certain cases multiple conditions are to be detected. In The construct in this slide is referred as the if-
 such cases the conditions and their associated statements can else-if ladder. The different conditions are
 be arranged in a construct that takes the form: evaluated starting from the top of the ladder and
 if (condition-1) whenever a condition is evaluated as TRUE, the
 statement-1; corresponding statement(s) are executed and the
 rest of the construct is skipped.
 else if (condition-2)
 statement-2;
 else if (condition-3)
 statement-3;
 ...
 else
 statement-n;
 The if-else-if ladder Statement
 cont..
 Example:
 Write a program to display the student’s grade based
 on the following :
 If the Marks Grade
 >=75 A
 > 50 and <75 B
 > 25 and <50 C
 < 25 F
 In this case multiple conditions are to be checked. Marks
 obtained by a student can only be in one of the ranges.
 Therefore if-else-if ladder can be used to implement
 following program. The program for the example above is
 shown in the figure.
 conditional operator (?:)
 conditional operator is also known as a ternary
 operator. It takes three operands. For example:
 Conditional operator is closely related
 with if..else statement.
 C programming Syntax of conditional operator:
 Here, if x > y then printf("x is
 greater") else printf("y is greater").
 If the condition is true then expression1 is executed
 else expression2 is executed.
 conditional operator (?:)
 Example:
 Write a C program to check whether the student is
 passed or failed using conditional operator. The
 answer is on the right side of this slide.
 The output of the code:
 Explanation
  The program checks the condition mark >=50, if it is
 true "Passed“, else it prints "Failed".
  This program can be simply done with the help
 of if...else statement.
 Switch Case Statement
 • Let’s take a simple example to understand the working of
 a switch case statement in C program.
 Before we see how a switch case statement works in
 a C program, let’s checkout the syntax of it:
 Explanation of the code in this slide: In switch we
 gave an expression, you can give variable also. we Output of the above code:
 gave num+2, where num value is 2 and after addition
 the expression resulted 4. Since there is no case
 defined with value 4 the default case is executed.
 Switch Case Statement
 • Let’s take a simple example to understand the working of
 a switch case statement in C program.
 Before we see how a switch case statement works in
 a C program, let’s checkout the syntax of it:
 Explanation of the code in this slide: In switch we
 gave an expression, you can give variable also. we Output of the above code:
 gave num+2, where num value is 2 and after addition
 the expression resulted 4. Since there is no case
 defined with value 4 the default case is executed.
 Break statement in Switch Case
 Explanation of the code in this slide: we passed a
 Before we discuss more about break statement, variable to switch, the value of the variable is 2 so the
 guess the output of this C program. control jumped to the case 2, However there are no
 such statements in the program which could break the
 flow after the execution of case 2. That’s the reason
 why after case 2, all the subsequent cases and default
 statements got executed. How to avoid this situation?
 We can use break statement to break the flow of
 control after every case block.
 Break statement in Switch Case
 Break statements are useful when you want your
 program-flow to come out of the switch body.
 Whenever a break statement is encountered in the
 switch body, the control comes out of the switch case
 statement.
 Output of the above program:
 Break statement in Switch Case
 cont..
Example of Switch Case with break:
 We are taking the program that we have seen above
 but this time we are using break statement.
 Output of the program:
 Why didn’t we use break statement after default?
 The control would itself come out of the switch after
 default that’s why we didn’t use it, however if you
 want to use the break after default then you can use it,
 there is no harm in doing that.
 Few Important points regarding
 Switch Case
1. Case doesn’t always need to have order 1, 2, 3 and so
 on. They can have any integer value after case
 keyword. Also, case doesn’t need to be in an
 ascending order always, you can specify them in any
 order as per the need of the program.
2. You can also use characters in switch case. The code
 on the right side is an example.
 Output of the program in this slide:
 Program To Create A Simple
 Calculator
Example:
Write a C program to create a simple calculator to perform
addition, subtraction, multiplication and division using
switch case statement. The code for this is on the right side
of the slide.
 If-else-if vs switch case
 cont..
 You can use an if-else-if statement when you have
 multiple conditions to check, and each condition may
 lead to a different set of instructions.
 It is also useful when you want to test a range of values
 or conditions that can’t be expressed with a single
 expression.
 For example, suppose you want to determine the grade
 of a student based on their marks. You can use an if-
 else-if statement to check the range of marks and assign
 a grade accordingly.
 If-else-if vs switch case
 cont..
 On the other hand, you can use a switch-case
 statement when you have a single expression
 to evaluate against multiple possible values. It
 is often more concise and readable than if-
 else-if statements when dealing with multiple
 options.
 For example, suppose you want to determine
 the day of the week based on its number. You
 can use a switch-case statement to check the
 number and assign a day name accordingly.
 In general, if-else-if is more flexible and can
 handle a wider range of conditions, while
 switch-case is more concise and easier to read
 when dealing with a large number of options.
 Ultimately, the choice between the two
 depends on the specific requirements of the
 situation at hand.
 Introduction to Loops
 in C
 Loops cause program to execute the certain block of Types Of Loops
 code repeatedly until some conditions are satisfied,
 i.e., loops are used in performing repetitive work in There are three types of loops used in the C
 programming.
 language.
 Why do we use loops in C language?
 1. While loop
 The loop simplifies the complex problems into the
 2. Do while loop
 easy ones.
 3. For loop
 It enables us to alter the flow of the program so that
 instead of writing the same code again and again, we
 can repeat the same code for a finite number of times.
 For example, if we need to print the first 10 natural
 numbers then, instead of using the printf statement 10
 times, we can print inside a loop which runs up to 10
 iterations.
 While Loop
 The while loop in C is used in the scenario where we Flowchart Of the while loop
 don't know the number of iterations in advance. The
 block of statements in while loop are executed until
 the condition specified in the while loop is satisfied. It
 is also called a pre-tested loop.
 The syntax of while loop in C language is given
 below:
 While Loop cont..
 Example 1:write a C program that prints 1 to 4 numbers on
 the screen. The program is shown here.
 Output of the program:
 step1: The variable count is initialized with value 1 and then
 it has been tested for the condition.
 step2: If the condition returns true then the statements inside
 the body of while loop are executed else control comes out
 of the loop.
 step3: The value of count is incremented using ++ operator
 then it has been tested again for the loop condition.
 While Loop cont..
 Guess the output of the while loop in program
 here.
 The program is an example of infinite while loop.
 Since the value of the variable var is same (there is no
 ++ or -- operator used on this variable, inside the
 body of loop) the condition var<=2 will be true
 forever and the loop would never terminate.
 Other examples of infinite
 while loop
 Example 1:
 Example 2:
 The above code has Infinite loop because variable var will
 always have value >=5 so the loop would never end.
 The above code has Infinite loop because var
 value will keep decreasing because of -- operator,
 hence it will always be <= 10.
 Use of Logical operators in
 while loop
 Just like relational operators (<, >, >=, <=, !=, ==), below we are using two logical operators NOT (!)
 we can also use logical operators in while loop. The and AND(&&):
 following scenarios are valid :
 You can use AND(&&) operator, which means both
 the conditions should be true. For example:
 below we are using two logical operators NOT (!)
 and OR(||):
 You can use OR(||) operator, this loop will run until
 both conditions return false. For example:
Example of while loop using logical
 operator
 In this example we are testing multiple
 conditions using logical operator inside
 while loop.
 The output of the program here is:
More Examples of using while loop
 Example1: write a C program to print all even numbers
 from 1 to n using while loop.
More Examples of using while loop
 cont..
 Example2: C program to find sum of natural numbers
 from 1 to n using while loop.
More Examples of using while loop
 cont..
 Example 3: C program to print multiplication table of
 a given number. For example:
  Input
 Input num: 5
  Output
 do-while loop
 The do-while loop continues until a given condition
 satisfies. It is also called post tested loop. It is used
 when it is necessary to execute the loop at least once .
 The syntax of do-while loop in c language is given
 below:
 The typical output of the above code:
 A real-life example of when we might use a do-
 while loop could be to ask a user to enter a
 password, and keep prompting them until they enter
 the correct password.
 For loop
 The for loop is used in the case where we need to execute
 some part of the code until the given condition is satisfied.
 The for loop is also called a pre-tested loop.
 It is better to use for loop if the number of iteration is known
 in advance.
 The syntax of for loop in c language is given below:
 Real life scenario of when to use a for loop in c:
 Suppose you are writing a program in C to calculate the sum
 of the first 10 numbers (1 + 2 + 3 + ... + 10). Instead of
 writing out each number manually, you can use a for loop to
 The output of the above code:
 iterate through each number and add them up. Here's what
 the code might look like:
 C break and continue
 In C programming, break and continue are two Below figure demonstrates How break statement
 works:
 keywords that are used to control the flow of a loop.
 Break statement
 break statement is used to immediately exit the loop.
 It is typically used when a condition is met that
 requires the loop to terminate early.
 The break statement is almost always used with if
 statement inside the loop.
Examples on how to use break
 statement
 Example 1: write a C Program to calculate the sum of
 10 numbers from the user. If a negative number is
 entered, the loop terminates.
 Output of the code:
 Continue statement
 Below figure demonstrates How break statement
 continue statement, is used to skip the current works:
 iteration of the loop and move on to the next iteration.
 It is typically used when a certain condition is met that
 requires skipping a specific iteration.
 The continue statement is also always used with the if
 statement.
 Examples on how to use
 continue statement
 Example 1: write a C Program to calculate the sum of 10
 numbers from the user . and negative numbers are skipped
 from the calculation
 Output of the code:
 In this program, when the user enters a positive number, the
 sum is calculated using sum += number; statement.
 When the user enters a negative number,
 the continue statement is executed and it skips the negative
 number from the calculation.
 Nested loops In C
 Below is the syntax of the nested loop:
 A nested loop is a loop inside another loop. In other
 words, it's a loop that is contained within the body of
 another loop. The inner loop will execute completely
 for each iteration of the outer loop.
 Nested loops are commonly used when you need to
 perform a task that requires multiple iterations. For
 example, if you have a two-dimensional array, you
 might use a nested loop to iterate over all the
 elements in the array. The outer loop would iterate
 over the rows, and the inner loop would iterate over
 the columns.
 Outer_loop and Inner_loop are the valid loops that
 can be a 'for' loop, 'while' loop or 'do-while' loop.
 The outer loop controls the number of times the inner
 loop will execute.
 Nested For loops In C
 Nest for loop means that we have for loops inside Syntax for nested for loop:
 another loop.
 Example for Nested For loops
 Explanation of the code
 First, the 'i' variable is initialized to 1 and then program control passes
 to the i<=n.
 The program control checks whether the condition 'i<=n' is true or not.
 If the condition is true, then the program control passes to the inner
 loop.
 The inner loop will get executed as long as the condition is true.
 After the execution of the inner loop, the control moves back to the
 update of the outer loop, i.e., i++.
 After incrementing the value of the loop counter, the condition is
 checked again, i.e., i<=n.
 If the condition is true, then the inner loop will be executed again.
 This process will continue as long as the condition of the outer loop is
 true.
 Out put of the code:
 Example for Nested nested loops
 Explanation of the code
This program uses two while loops to print out the values
of i and j. The outer while loop runs from i = 1 to i = 3,
while the inner while loop runs from j = 1 to j = 3 for each
value of i. Inside the inner loop, printf() statement prints
out the values of i and j.
 Out put of the code here is:
 Nested while loops In C
 The nested while loop means any type of Syntax for nested for loop:
 loop which is defined inside the 'while' loop.
 While loop Exercises
1. Write a program in c that displays numbers from 1000
 to 100 using while loop.
2. Write a program that displays all the integers from 8
 to 23 (inclusive) using while loop.
3. Using while loop, write a program that asks the user
 to type 10 integers and displays their sum.
4. Write a program that asks the user to type 10 integers
 and displays the smallest of these integers.
 for loop Exercises
1. Write a C program to print the 6. Write a C program that uses nested loops to
 multiplication table of a given number up to print out a different number of stars on each line
 10. The expected output:
2. Write a C program to find the factorial of a
 given number.
3. Write a C program to find the sum of all
 even numbers between 1 and 100.
4. Write a C program to check whether a
 given number is prime or not.
5. Write a C program to print the Fibonacci
 series up to a given number.