Case Logic Structure Nested Loop Indicator and Recursion

  • Slides: 26
Download presentation
Case Logic Structure, Nested Loop, Indicator and Recursion

Case Logic Structure, Nested Loop, Indicator and Recursion

Overview • • Learning Objectives Learning Outcomes Case Logic Structures Nested Loop Indicators Recursion

Overview • • Learning Objectives Learning Outcomes Case Logic Structures Nested Loop Indicators Recursion Example Summary

Learning Objectives • To explain case logic structures • To apply case logic structures

Learning Objectives • To explain case logic structures • To apply case logic structures • To explain indicators and recursion

Learning Outcomes • • The Students are able to: To explain case logic structures

Learning Outcomes • • The Students are able to: To explain case logic structures To apply indicators and recursion

Case Logic Structure • Sometimes a solution to a problem requires the computer to

Case Logic Structure • Sometimes a solution to a problem requires the computer to select one action from a set of actions. • This kind of solution can be designed through the decision logic structure, but the case logic structure is more efficient for this purpose. • The case logic structure is a special variation of the decision structure, although it is different enough to present here separately.

Syntax of Case Logic Structure

Syntax of Case Logic Structure

Flowchart of Case Logic Structure

Flowchart of Case Logic Structure

Alternate Flowchart of Case Logic Structure

Alternate Flowchart of Case Logic Structure

Case Logic Structure Using Nested if

Case Logic Structure Using Nested if

Example 1: Payroll Problem

Example 1: Payroll Problem

Example 2: Area Problem • Calculate area as per user choice If choice ==

Example 2: Area Problem • Calculate area as per user choice If choice == 1: Area of circle If choice == 2: Area of triangle If choice == 3: Area of Square If choice == 4: Print error message “Invalid choice”

Example 2: Area Problem 1) Start 2) Case of choice == 1: read r

Example 2: Area Problem 1) Start 2) Case of choice == 1: read r area = 3. 142 * r*r print “Area: ”, area choice == 2: read b, h area = 0. 5*b*h print “Area: ”, area choice == 3: read s area = s*s print “Area: ”, area Otherwise: print “Invalid Choice” Endof. Case 3) Stop

Example 2: Area Problem choice=1 Start Caseof Choice choice =2 choice=3 Read r Read

Example 2: Area Problem choice=1 Start Caseof Choice choice =2 choice=3 Read r Read b, h Read s area=3. 142*r*r area=0. . 5*b*h area = s*s Print area Stop choice=4 Print “Invalid No”

Example 3 • Develop the algorithm and flowchart for a module that calculates the

Example 3 • Develop the algorithm and flowchart for a module that calculates the retirement deduction rate according to the following codes: • R = 10% • P = 8% • G = 5%

Nested Loop • Loop inside loop is called the nested loop

Nested Loop • Loop inside loop is called the nested loop

 • Nested while loop

• Nested while loop

 • Nested for loop

• Nested for loop

Indicators • Indicators are logical variables that a programmer sets within a program to

Indicators • Indicators are logical variables that a programmer sets within a program to change the processing path or to control when the processing of a loop should end. They are sometimes called flags, switches, or trip values • The user has no knowledge of these indicators during processing. These indicators are used to detect changes in the output line, in the processing of data, and so forth.

Recursion • Recursion occurs when a module or a function calls itself. The condition

Recursion • Recursion occurs when a module or a function calls itself. The condition that ends the loop must be within the module. • Recursive procedures can be replaced by conventional loop structures; •

Factorial of Number

Factorial of Number

Factorial of Number

Factorial of Number

Example • Print factorial of 1 to n numbers • Print fibonacci series from

Example • Print factorial of 1 to n numbers • Print fibonacci series from 1 to n

Summary • • Case Logic Structure Example of Case Logic Structure Indicator Recursion Function

Summary • • Case Logic Structure Example of Case Logic Structure Indicator Recursion Function

Important Questions • Define indicator • Define recursion • Explain case logic structure. Explain

Important Questions • Define indicator • Define recursion • Explain case logic structure. Explain with example.