This document discusses nested if-else statements, the switch statement, and the break statement in programming. It provides examples of using nested if-else statements to perform arithmetic operations and determine vowels/grades. The switch statement is described as a substitute for nested if-else statements with multiple choices. Examples are given to test for divisibility and perform arithmetic using switch statements. The break statement is used to exit switch structures. The key difference between nested if-else and switch is that switch compares a value against multiple constant cases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
10 views10 pages
7 - Conditional Statement
This document discusses nested if-else statements, the switch statement, and the break statement in programming. It provides examples of using nested if-else statements to perform arithmetic operations and determine vowels/grades. The switch statement is described as a substitute for nested if-else statements with multiple choices. Examples are given to test for divisibility and perform arithmetic using switch statements. The break statement is used to exit switch structures. The key difference between nested if-else and switch is that switch compares a value against multiple constant cases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10
Lecture-07
Introduction to programming Objectives ❏Nested If-Else Statement ❏ The Switch Statement ❏ The break ❏ The Difference b/w if-else and switch ❏ ❏
Ghalib Univeristy: By Farman Ali 1
The “Nested if-else” Structure When an “if-else” structure is placed in another “if-else” structure, it is called “nested if-else” structure. It is used to select one along multiple selections. Flowchart of Nested if-else structure
Ghalib Univeristy: By Farman Ali 2
The “Nested if-else” Structure The general syntax is If(condition-1) Statement-1; else if(condition-2) Statement-2; else if(condition-3) Statement-3; ----------------------- ----------------------- else if(condition-m) Statement-m; else Statement-n; Ghalib Univeristy: By Farman Ali 3 Programs Write a program to perform simple arithmetic operation by using “nested if-else” structure.
Write a Program to find that whether the entered
character is in vowel(aeiou) character or other character.
Write a program that find the grade of Students base on
average marks using nested if-else statements.
Ghalib Univeristy: By Farman Ali 4
The “Switch” Statement switch statement is the substitute of “Nested if-else” statements. used when multiple choices are given and one choice is to be selected. “Nested if-else” Structure becomes complicated in multiple choices. “switch” statement is used in such situation. The “switch” statement evaluates an expression and returns a value. One of the choices or cases in the “switch” statement is executed depending upon the value returned by the expression. The returned value is compared with the constant values given in the cases. If the returned value matches the case value, the statements under that case are executed. Ghalib Univeristy: By Farman Ali 5 The “Switch” Statement The general syntax of switch is: switch(expression) { case const-1: Statements; break; case const-2 Statements; break; case const-3: Statements; break; --------------------- --------------------- case const-n Statements; break; default: Statements; }
Ghalib Univeristy: By Farman Ali 6
The break Statement: break statement is used to exit form the body of the switch structure or the loop structure. In the switch statement, the break statement is normally used at the end of statement in each case. If it is not used then the statements of other cases that come after the matching case will also be executed
Ghalib Univeristy: By Farman Ali 7
Programs Write a program to input an integer value. Test the value if the value is divisible by 2, then print the message “Divisible by 2” otherwise “Not divisible by 2” by using switch statement.
Write a program to display a menu to perform
various functions. Use switch statement.
Write a program to perform simple arithmetic
operation by using switch statement. Ghalib Univeristy: By Farman Ali 8 The Difference b/w “nested if- else” & “switch” Statements Both “nested if-else” and “switch” statement are used for multiple selection.