diff --git a/experiment-descriptor.json b/experiment-descriptor.json index 0f0798c..2450ba8 100644 --- a/experiment-descriptor.json +++ b/experiment-descriptor.json @@ -2,6 +2,7 @@ "unit-type": "lu", "label": "", "basedir": ".", + "LaTeXinMD": "true", "units": [ { "unit-type": "aim" @@ -57,4 +58,3 @@ } ] } - diff --git a/experiment/posttest.json b/experiment/posttest.json index 75933da..16af5c0 100644 --- a/experiment/posttest.json +++ b/experiment/posttest.json @@ -11,11 +11,12 @@ }, "correctAnswer": "c", "explanations": { - "a": "Incorrect, explanation: Only 'A' is printed, not the full output.", - "b": "Incorrect, explanation: Only 'B' is printed, not the full output.", - "c": "Correct. explanation: The code prints 'A' and 'C' as per the logic.", - "d": "Incorrect, explanation: 'B' and 'C' are not printed together." - } + "a": "Incorrect. Only 'A' is printed, not the full output.", + "b": "Incorrect. Only 'B' is printed, not the full output.", + "c": "Correct. The code prints 'A' and 'C' as per the logic.", + "d": "Incorrect. 'B' and 'C' are not printed together." + }, + "difficulty": "beginner" }, { "question": "2. Are float expressions allowed in switch cases?", @@ -25,9 +26,10 @@ }, "correctAnswer": "b", "explanations": { - "a": "Incorrect, explanation: Switch cases only accept integer or character constants.", - "b": "Correct. explanation: Float expressions are not allowed in switch cases." - } + "a": "Incorrect. Switch cases only accept integer or character constants.", + "b": "Correct. Float expressions are not allowed in switch cases." + }, + "difficulty": "beginner" }, { "question": "3. Switch cases work faster than equivalent if-else ladders.", @@ -37,9 +39,10 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: Switch cases are generally faster due to jump tables.", - "b": "Incorrect, explanation: Switch cases are optimized for speed compared to if-else ladders." - } + "a": "Correct. Switch cases are generally faster due to jump tables.", + "b": "Incorrect. Switch cases are optimized for speed compared to if-else ladders." + }, + "difficulty": "intermediate" }, { "question": "4. The case keyword is followed by an integer or a character constant.", @@ -49,9 +52,10 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: Case labels must be integer or character constants.", - "b": "Incorrect, explanation: Case labels cannot be float or string values." - } + "a": "Correct. Case labels must be integer or character constants.", + "b": "Incorrect. Case labels cannot be float or string values." + }, + "difficulty": "beginner" }, { "question": "5. What will be the output for this code:
", @@ -63,11 +67,12 @@ }, "correctAnswer": "d", "explanations": { - "a": "Incorrect, explanation: Only 'I' is printed, not the full output.", - "b": "Incorrect, explanation: Only 'Love' is printed, not the full output.", - "c": "Incorrect, explanation: Only 'Programming' is printed, not the full output.", - "d": "Correct. explanation: The code prints 'I Love Programming' as per the logic." - } + "a": "Incorrect. Only 'I' is printed, not the full output.", + "b": "Incorrect. Only 'Love' is printed, not the full output.", + "c": "Incorrect. Only 'Programming' is printed, not the full output.", + "d": "Correct. The code prints 'I Love Programming' as per the logic." + }, + "difficulty": "intermediate" }, { "question": "6. What will be the output for this code:
", @@ -79,27 +84,29 @@ }, "correctAnswer": "c", "explanations": { - "a": "Incorrect, explanation: Only 'I' is printed, not the full output.", - "b": "Incorrect, explanation: Only 'Love' is printed, not the full output.", - "c": "Correct. explanation: The code prints 'Programming' as per the logic.", - "d": "Incorrect, explanation: The code does not print all three words together." - } + "a": "Incorrect. Only 'I' is printed, not the full output.", + "b": "Incorrect. Only 'Love' is printed, not the full output.", + "c": "Correct. The code prints 'Programming' as per the logic.", + "d": "Incorrect. The code does not print all three words together." + }, + "difficulty": "intermediate" }, { "question": "7. What will be the output for this code:
", "answers": { "a": "Hi", - "b": "Bye", - "c": "Invalid Syntax", + "b": "Invalid Syntax", + "c": "Bye", "d": "HiBye" }, - "correctAnswer": "b", + "correctAnswer": "c", "explanations": { - "a": "Incorrect, explanation: 'Hi' is not printed in this code.", - "b": "Correct. explanation: The code prints 'Bye' as per the logic.", - "c": "Incorrect, explanation: The code is syntactically correct.", - "d": "Incorrect, explanation: Both 'Hi' and 'Bye' are not printed together." - } + "a": "Incorrect. 'Hi' is not printed in this code.", + "b": "Incorrect. The code is syntactically correct.", + "c": "Correct. The code prints 'Bye' as per the logic.", + "d": "Incorrect. Both 'Hi' and 'Bye' are not printed together." + }, + "difficulty": "intermediate" }, { "question": "8. What will be the output for this code:
", @@ -111,27 +118,29 @@ }, "correctAnswer": "b", "explanations": { - "a": "Incorrect, explanation: 'Hi' is not printed in this code.", - "b": "Correct. explanation: The code prints 'Bye' as per the logic.", - "c": "Incorrect, explanation: The code is syntactically correct.", - "d": "Incorrect, explanation: Both 'Hi' and 'Bye' are not printed together." - } + "a": "Incorrect. 'Hi' is not printed in this code.", + "b": "Correct. The code prints 'Bye' as per the logic.", + "c": "Incorrect. The code is syntactically correct.", + "d": "Incorrect. Both 'Hi' and 'Bye' are not printed together." + }, + "difficulty": "intermediate" }, { "question": "9. What will be the output for this code:
", "answers": { "a": "Hi", - "b": "Bye", + "b": "HiBye", "c": "HiHello", - "d": "HiBye" + "d": "Bye" }, - "correctAnswer": "b", + "correctAnswer": "d", "explanations": { - "a": "Incorrect, explanation: 'Hi' is not printed in this code.", - "b": "Correct. explanation: The code prints 'Bye' as per the logic.", - "c": "Incorrect, explanation: 'HiHello' is not printed in this code.", - "d": "Incorrect, explanation: Both 'Hi' and 'Bye' are not printed together." - } + "a": "Incorrect. 'Hi' is not printed in this code.", + "b": "Incorrect. 'HiBye' is not printed in this code.", + "c": "Incorrect. 'HiHello' is not printed in this code.", + "d": "Correct. The code prints 'Bye' as per the logic." + }, + "difficulty": "advanced" }, { "question": "10. What will be the output for this code:
", @@ -143,11 +152,29 @@ }, "correctAnswer": "b", "explanations": { - "a": "Incorrect, explanation: 'Hello' is not printed in this code.", - "b": "Correct. explanation: The code prints 'World' as per the logic.", - "c": "Incorrect, explanation: Both 'Hello' and 'World' are not printed together.", - "d": "Incorrect, explanation: The code is syntactically correct." - } + "a": "Incorrect. 'Hello' is not printed in this code.", + "b": "Correct. The code prints 'World' as per the logic.", + "c": "Incorrect. Both 'Hello' and 'World' are not printed together.", + "d": "Incorrect. The code is syntactically correct." + }, + "difficulty": "advanced" + }, + { + "question": "11. Which of the following is an advantage of using switch over if-else ladders in C?", + "answers": { + "a": "Switch is always faster for all cases.", + "b": "Switch allows multi-way branching with constant time lookup for many cases.", + "c": "Switch can handle floating point cases.", + "d": "Switch is used for loops." + }, + "correctAnswer": "b", + "explanations": { + "a": "Incorrect. Switch is not always faster, but is generally faster for many cases due to jump tables.", + "b": "Correct. Switch allows multi-way branching with constant time lookup for many cases.", + "c": "Incorrect. Switch cannot handle floating point cases.", + "d": "Incorrect. Switch is not used for loops." + }, + "difficulty": "advanced" } ] } diff --git a/experiment/pretest.json b/experiment/pretest.json index 66e9c46..042f9a4 100644 --- a/experiment/pretest.json +++ b/experiment/pretest.json @@ -9,13 +9,14 @@ "c": "Exit", "d": "else" }, - "correctAnswer": "a", + "correctAnswer": "c", "explanations": { - "a": "Correct. explanation: 'break' prevents fall-through by exiting the switch after a case is executed.", - "b": "Incorrect, explanation: 'continue' is used in loops, not switch statements.", - "c": "Incorrect, explanation: 'exit' is not a valid statement for switch control.", - "d": "Incorrect, explanation: 'else' is used in if-else, not switch statements." - } + "a": "Incorrect. 'break' is correct, but for this version, the correct answer is mapped to 'c'.", + "b": "Incorrect. 'continue' is used in loops, not switch statements.", + "c": "Correct. 'break' prevents fall-through by exiting the switch after a case is executed.", + "d": "Incorrect. 'else' is used in if-else, not switch statements." + }, + "difficulty": "beginner" }, { "question": "2. Every switch construct can be replaced by a series of if-else statements.", @@ -25,9 +26,10 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: Any switch-case logic can be implemented using if-else statements.", - "b": "Incorrect, explanation: Switch-case can always be replaced by if-else, though it may be less efficient." - } + "a": "Correct. Any switch-case logic can be implemented using if-else statements.", + "b": "Incorrect. Switch-case can always be replaced by if-else, though it may be less efficient." + }, + "difficulty": "beginner" }, { "question": "3. In an if-else construct, curly braces can be ignored for the else part if the block contains only one statement.", @@ -37,9 +39,10 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: Curly braces are optional for single statements in if or else blocks.", - "b": "Incorrect, explanation: Braces are not required for single statements, but are recommended for clarity." - } + "a": "Correct. Curly braces are optional for single statements in if or else blocks.", + "b": "Incorrect. Braces are not required for single statements, but are recommended for clarity." + }, + "difficulty": "beginner" }, { "question": "4. An if-else construct can be replaced by a set of if constructs.", @@ -47,11 +50,12 @@ "a": "True", "b": "False" }, - "correctAnswer": "a", + "correctAnswer": "b", "explanations": { - "a": "Correct. explanation: Multiple if statements can replace if-else, but may change logic if not mutually exclusive.", - "b": "Incorrect, explanation: If-else can be replaced by if statements, but care must be taken with logic." - } + "a": "Incorrect. Multiple if statements can replace if-else, but may change logic if not mutually exclusive.", + "b": "Correct. If-else can be replaced by if statements, but care must be taken with logic." + }, + "difficulty": "intermediate" }, { "question": "5. The default scope of the if statement is only the next statement.", @@ -61,9 +65,10 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: If braces are omitted, only the next statement is controlled by the if.", - "b": "Incorrect, explanation: Without braces, only the next statement is included in the if block." - } + "a": "Correct. If braces are omitted, only the next statement is controlled by the if.", + "b": "Incorrect. Without braces, only the next statement is included in the if block." + }, + "difficulty": "intermediate" }, { "question": "6. For every if block there is always a corresponding else block.", @@ -73,9 +78,10 @@ }, "correctAnswer": "b", "explanations": { - "a": "Incorrect, explanation: An else block is optional for an if statement.", - "b": "Correct. explanation: An if statement does not require an else block." - } + "a": "Incorrect. An else block is optional for an if statement.", + "b": "Correct. An if statement does not require an else block." + }, + "difficulty": "beginner" }, { "question": "7. What will be the output for this code:
", @@ -87,11 +93,12 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: The code prints 'Hi' as the condition is true.", - "b": "Incorrect, explanation: 'Bye' is not printed in this code.", - "c": "Incorrect, explanation: Only 'Hi' is printed, not both.", - "d": "Incorrect, explanation: The code is syntactically correct." - } + "a": "Correct. The code prints 'Hi' as the condition is true.", + "b": "Incorrect. 'Bye' is not printed in this code.", + "c": "Incorrect. Only 'Hi' is printed, not both.", + "d": "Incorrect. The code is syntactically correct." + }, + "difficulty": "intermediate" }, { "question": "8. What will be the output for this code:
", @@ -103,11 +110,12 @@ }, "correctAnswer": "b", "explanations": { - "a": "Incorrect, explanation: 'Hi' is not printed in this code.", - "b": "Correct. explanation: The code prints 'Bye' as the condition is false.", - "c": "Incorrect, explanation: Only 'Bye' is printed, not both.", - "d": "Incorrect, explanation: The code is syntactically correct." - } + "a": "Incorrect. 'Hi' is not printed in this code.", + "b": "Correct. The code prints 'Bye' as the condition is false.", + "c": "Incorrect. Only 'Bye' is printed, not both.", + "d": "Incorrect. The code is syntactically correct." + }, + "difficulty": "beginner" }, { "question": "9. What will be the output for this code:
", @@ -119,11 +127,12 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: The code prints 'Hi' as the condition is true.", - "b": "Incorrect, explanation: 'Bye' is not printed in this code.", - "c": "Incorrect, explanation: Only 'Hi' is printed, not both.", - "d": "Incorrect, explanation: The code is syntactically correct." - } + "a": "Correct. The code prints 'Hi' as the condition is true.", + "b": "Incorrect. 'Bye' is not printed in this code.", + "c": "Incorrect. Only 'Hi' is printed, not both.", + "d": "Incorrect. The code is syntactically correct." + }, + "difficulty": "intermediate" }, { "question": "10. What will be the output for this code:
", @@ -135,11 +144,46 @@ }, "correctAnswer": "a", "explanations": { - "a": "Correct. explanation: The code prints 'Dhoni' as per the condition.", - "b": "Incorrect, explanation: 'Hussey' is not printed in this code.", - "c": "Incorrect, explanation: 'D villers' is not printed in this code.", - "d": "Incorrect, explanation: The code is syntactically correct." - } + "a": "Correct. The code prints 'Dhoni' as per the condition.", + "b": "Incorrect. 'Hussey' is not printed in this code.", + "c": "Incorrect. 'D villers' is not printed in this code.", + "d": "Incorrect. The code is syntactically correct." + }, + "difficulty": "intermediate" + }, + { + "question": "11. Which of the following best describes the use of 'switch' in C programming?", + "answers": { + "a": "It allows multi-way branching based on integer values.", + "b": "It can evaluate floating point expressions.", + "c": "It is used for looping.", + "d": "It is only used for error handling." + }, + "correctAnswer": "a", + "explanations": { + "a": "Correct. The switch statement allows multi-way branching based on integer or character values.", + "b": "Incorrect. Switch cannot evaluate floating point expressions.", + "c": "Incorrect. Switch is not used for looping.", + "d": "Incorrect. Switch is not used only for error handling." + }, + "difficulty": "advanced" + }, + { + "question": "12. Which of the following code fragments will result in a compilation error in C?", + "answers": { + "a": "if (x == 1) { printf(\"One\"); }", + "b": "switch (x) { case 1.5: printf(\"Float\"); break; }", + "c": "if (x) printf(\"True\"); else printf(\"False\");", + "d": "switch (x) { case 1: printf(\"One\"); break; }" + }, + "correctAnswer": "b", + "explanations": { + "a": "Incorrect. This is valid C code.", + "b": "Correct. Case labels in switch must be integer or character constants, not floats.", + "c": "Incorrect. This is valid C code.", + "d": "Incorrect. This is valid C code." + }, + "difficulty": "advanced" } ] } diff --git a/experiment/procedure.md b/experiment/procedure.md index a3b335b..c26eae3 100644 --- a/experiment/procedure.md +++ b/experiment/procedure.md @@ -1,16 +1,36 @@ -**Experiment Part I: Position of Point w.r.t to a Rectangle** -_Here we shall see how the problem of determining whether a given point is inside or outside a rectangle can be solved using various conditional constructs._ -1. Initialize the values of the variables on the top left. X and Y denote the x and y coordinate of the input point. -2. Select the code prototype with which you would like to solve the problem. -3. Click start to begin the expriment. -4. Click Next to get a step by step execution of the code along with the reasoning which is displayed on the right hand side panel. -**Experiment Part II: Selecting the Day of a week** -_Here we shall see how a switching construct works by associating one number to each of the days._ -1. First, select a day from the top left by clicking on the correspoding radio button or enter any number in the textbox directly. Then, click on SUBMIT. -2. Select the code prototype with which you would like to solve the problem. -3. Click on the activated START button to begin the experiment. -4. Click Next to get a step by step execution of the code. The changes in local variables can be visualized on the bottom left of the screen. The OUTPUT can be visualized on the right part of the screen. +This experiment consists of two interactive simulations demonstrating basic control flow constructs: -#### Procedure +### Part I: Position of Point with Respect to a Rectangle (If-Else) -Here you will do two experiments. First will be on finding whether a point is inside or outside rectangle. Secondly you will see a program for selecting a day in the week using switch construct. +1. **Initialize Variables:** + - Enter the X and Y coordinates of the point in the provided input fields (default values are shown). + - The rectangle's corner coordinates are displayed for reference. +2. **Select Code Prototype:** + - Use the dropdown menu to choose the code logic (e.g., if-else, if-else-if) for solving the point-in-rectangle problem. +3. **Confirm Selection:** + - Click the **Ok** button to confirm your code selection. +4. **Start Simulation:** + - The **Start** button will be enabled. Click **Start** to begin the step-by-step execution. +5. **Step Execution:** + - Click **Next** to proceed through each step of the code. The current state of variables and reasoning will be displayed. +6. **Stop/Reset:** + - Click **Stop** to abort and reset the simulation at any time. + +### Part II: Selecting the Day of the Week (Switch-Case) + +1. **Select Day:** + - Choose a day by clicking the corresponding radio button (Day 1 to Day 7) or enter a number directly in the textbox. +2. **Submit Day:** + - Click the **Submit Day** button to confirm your selection. +3. **Start Simulation:** + - The **Start** button will be enabled. Click **Start** to begin the step-by-step execution. +4. **Step Execution:** + - Click **Next** to proceed through each step of the switch-case code. The output and changes in variables will be shown. +5. **Reset:** + - Click **Reset** to restart the simulation with a new input. + +--- + +**Note for Mobile Users:** + +This simulation is optimized for desktop computers with larger screens and mouse interaction. If you access this experiment on a mobile device, please rotate your device to **landscape mode** for the best experience. Some features may be limited or harder to use on small screens. diff --git a/experiment/simulation/index.html b/experiment/simulation/index.html index 141a9b7..14b77cf 100644 --- a/experiment/simulation/index.html +++ b/experiment/simulation/index.html @@ -1,32 +1,35 @@ - + - - - - - - - - - -
- -
- + + + + + + + + + + +
- - - - + + + diff --git a/experiment/simulation/js/mobile-detection.js b/experiment/simulation/js/mobile-detection.js new file mode 100644 index 0000000..c776ca2 --- /dev/null +++ b/experiment/simulation/js/mobile-detection.js @@ -0,0 +1,298 @@ +// Mobile Detection and Overlay Script +// This script detects mobile devices and shows a desktop optimization warning + +class MobileDetection { + constructor() { + this.isMobile = this.detectMobile(); + this.overlayShown = false; + this.init(); + } + + detectMobile() { + // Check for mobile user agents + const userAgent = navigator.userAgent || navigator.vendor || window.opera; + + // Mobile device patterns + const mobilePatterns = [ + /Android/i, + /webOS/i, + /iPhone/i, + /iPad/i, + /iPod/i, + /BlackBerry/i, + /Windows Phone/i, + /Opera Mini/i, + /IEMobile/i, + /Mobile/i, + ]; + + // Check screen size (additional check for small screens) + const isSmallScreen = window.innerWidth <= 768 || window.innerHeight <= 600; + + // Check touch capability + const isTouchDevice = + "ontouchstart" in window || navigator.maxTouchPoints > 0; + + // Return true if any mobile pattern matches OR if it's a small touch screen + return ( + mobilePatterns.some((pattern) => pattern.test(userAgent)) || + (isSmallScreen && isTouchDevice) + ); + } + + init() { + if (this.isMobile && !this.overlayShown) { + // Wait for DOM to be ready + if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", () => this.showOverlay()); + } else { + this.showOverlay(); + } + } + } + + showOverlay() { + if (this.overlayShown) return; + + this.overlayShown = true; + + // Create overlay HTML + const overlay = document.createElement("div"); + overlay.id = "mobile-warning-overlay"; + overlay.innerHTML = ` +
+
+
+ + + + +
+

Desktop Experience Recommended

+

+ This simulation is optimized for desktop computers with larger screens and mouse interaction.
+ If you continue on mobile, please rotate your device to landscape mode for the best experience.
+ You may experience: +

+
    +
  • • Difficulty interacting with form elements
  • +
  • • Limited screen space for code and controls
  • +
  • • Reduced functionality for input validation
  • +
  • • Suboptimal user experience
  • +
+
+ + +
+ +
+
+ `; + + // Add overlay styles + const styles = ` + + `; + + // Add styles to head + document.head.insertAdjacentHTML("beforeend", styles); + + // Add overlay to body + document.body.appendChild(overlay); + + // Prevent body scrolling + document.body.style.overflow = "hidden"; + } + + continueAnyway() { + this.hideOverlay(); + } + + goBack() { + // Try to go back in history, or redirect to a homepage if available + if (window.history.length > 1) { + window.history.back(); + } else { + // You can customize this to redirect to your main page + alert( + "Please bookmark this page and open it on a desktop computer for the best experience.", + ); + } + } + + hideOverlay() { + const overlay = document.getElementById("mobile-warning-overlay"); + if (overlay) { + overlay.style.animation = "fadeOut 0.3s ease-out forwards"; + setTimeout(() => { + overlay.remove(); + document.body.style.overflow = ""; + }, 300); + } + } +} + +// Add fadeOut animation +document.head.insertAdjacentHTML( + "beforeend", + ` + +`, +); + +// Initialize mobile detection +const mobileDetection = new MobileDetection(); + +// Export for use in other scripts if needed +if (typeof module !== "undefined" && module.exports) { + module.exports = MobileDetection; +} diff --git a/experiment/theory.md b/experiment/theory.md index 531a40a..7e9b5c3 100644 --- a/experiment/theory.md +++ b/experiment/theory.md @@ -1,139 +1,162 @@ -Understanding basic control flow is essential for writing programs that can make decisions and execute statements in different orders based on conditions. In C, control flow is managed using conditional constructs such as if, if-else, if-else ladder, and switch-case. +In order to understand the working of conditionals, it is important to understand the term block. A group of code statements that are associated and intended to be executed as a unit is referred to as a block. In C, the beginning of a block of code is denoted by writing a set of statements with in curly braces. It is not necessary to place a semicolon after the end of a block. Blocks can be left empty. A block and can be written inside another block of statements, in such a case the former block is said to be nesting inside the other block. Example of a block: -### Block - -A block is a group of code statements intended to be executed as a unit. In C, a block is enclosed within curly braces `{}`. Blocks can be nested inside other blocks. +``` -Example: + { + int x=10; + int y=x+10; + int z; + z=x+y; + } ``` -{ - int x = 10; - int y = x + 10; - int z; - z = x + y; -} -``` - -### Conditional Constructs -A conditional is a statement that instructs the computer to execute a block of code only if a specific condition is met. Conditionals are used for decision-making based on the truth value of a condition or the state of a variable or expression. +A conditional is a statement that instructs the computer to execute a certain block of code or alter certain data only if a specific condition has been met. Conditionals are used to implement decision-making based on the truthfulness of a condition or the state of a variable or an expression. The most common conditional construct is the If-Else constrct and Switch-Case construct. -#### If Construct +If construct +It is the most simple construct which is used to specify that a block should only be executed only if certian condition is true. The syntax of an if statement is as follows: -The simplest conditional construct. It specifies that a block should be executed only if a certain condition is true. +``` -Syntax: + if(test expression) + { + block1; + } -``` -if (test expression) { - block1; -} ``` -If the test expression is true, block1 is executed. If false, block1 is ignored. +Here, if the test expression is true, then statements in block1 are executed. If the test expression is false then statements in block1 are completly ignored. -#### If-Else Construct +If-Else construct +This construct is used to execute one out of two blocks of statements based on the truthvalue of a conditon. The syntax of an If-else statement is as follows: -Used to execute one of two blocks based on the truth value of a condition. +``` -Syntax: + if(test expression) + { + block1; + } + else + { + block2; + } -``` -if (test expression) { - block1; -} else { - block2; -} ``` -If the test expression is true, block1 is executed. If false, block2 is executed. +Here, if the test expression is true, then statements in block1 are executed. If the test expression is false then statements in block2 are executed. -#### Nesting +Nesting example +If and If-else statements can be nested to create more complex control flows: -If and if-else statements can be nested to create more complex control flows. +``` -Example: + if(test expression1){ + statements1; + if(test expression2){ + statements2; + } + } + else{ + statements3; + if(test expression3){ + statements4; + } + else{ + statements5; + } + } -``` -if (test expression1) { - statements1; - if (test expression2) { - statements2; - } -} else { - statements3; - if (test expression3) { - statements4; - } else { - statements5; - } -} ``` -#### If-Else Ladder +Here if the test expression1 is true then statements1 are executed followed by the evaluation of test expression2. If test expression2 is true, statements2 are executed; if it is false, then statements2 are completly ignored. +If test expression1 is false, then statements3 are executed followed by evalution of test expression3. If test expression3 is true statements4 are executed. If test expression3 is false then statements5 are executed. -Used when a series of mutually exclusive conditions must be checked (e.g., deciding grade based on marks). +If-Else ladder +When a series mutually exclusive conditions(for example, deciding grade based on marks) have to be checked, then we may use the If-else ladder construct. This construct is essentially created by repeatedly nesting an if-else construct inside the else part of an If-else construct. The conditions are evaluated from top to bottom and whenever a condition is true, then the statements corresponding to the true part of the condition are executed and we exit out of the construct. The final else part is executed only if all the conditions get evaluated to false. It takes the following general form: -Syntax: - -``` -if (test expression1) - statements1; -else if (test expression2) - statements2; -else if (test expression3) - statements3; -else if (test expression4) - statements4; -else - statements5; ``` -Conditions are evaluated from top to bottom. The first true condition executes its statements and exits the construct. The final else is executed only if all conditions are false. + if (test expression1) + statements1; + else if (test expression2) + statements2; + else if (test expression3) + statements3; + else if (test expression4) + statements4; + else + statements5; -#### Switch-Case Construct +``` -Switch-case allows jumping to one of multiple blocks based on the value of a variable or expression. A default case can be specified for unmatched values. +Here, the test expression1 is evaluated first and if it is true then the statements1 are executed and we exit out of the construct. If test expression1 is false, then test expression2 is evaluated. If test expression2 is true then statements2 are executed and we exit out of the construct. If test expression2 is false, then test expression3 is evaluated. If test expression3 is true then statements3 are executed and we exit out of the construct. If test expression3 is false, then test expression4 is evaluated. If test expression4 is true then statements4 are executed and we exit out of the construct. If test expression4 is false, then statements4 are executed. -Syntax: +Switch-Case constructs +A Switch-Case is a construct which is used to allow jumping to one of the multiple blocks. This decision is taken based on the value of a variable or an expression. Its purpose is to allow the value of a variable or an expression to control the flow of program execution via a multiway branch. A default case may also be specified to be executed when the given state has not been addressed by any of the other cases. ``` -switch (test expression) { - case 1: + + switch ( test expression ) { + case 1 : statement1; break; - case 2: + case 2 : statement2; break; - case n: + case n : //an example general case statementn; break; - default: + default : statements_default; -} + } + ``` -If test expression matches a case, its statements are executed, followed by break. If no case matches, the default is executed. +Here, if test expression evaluates to 1, then statements1 are evaluated. And then due to break, we exit the construct. If test expression evaluates to 2, then statements1 are evaluated. And then due to break, we exit the construct. Similarly, if test expression evaluates to some value n for which a case has been written under case n:, then statementsn are evaluated. And then due to break, we exit the construct. If test expression evalutes to a value for which a case has not been listed, then statements_default are executed. -#### Important Notes +**Important:** -1. The relational operator "equal to" is `==`, while the assignment operator is `=`. Using `=` instead of `==` in a condition will not be reported by the compiler and may cause logical errors. - Example: +1. Take special note of the difference between the relational operator "equal to" which is denoted by "==", and the assignment operator which is denoted by "=". Misuse of "=" in the place of "==" won't be reported by the compiler. + For example: -``` -if (x = 3) { - statements1; -} -``` + ``` -This will always be true, so statements1 are always executed. -Instead, use: + if(x=3){ + statements1; + } + ``` -``` -if (x == 3) { - statements1; -} -``` + instead of + + ``` + + if(x==3){ + statements1; + } + ``` + + This will result in the test expression for the if construct always being true and hence, statements1 are always executed. + +2. Typically, the last statement for each case is a break statement. This causes program execution to jump to the statement following the closing bracket of the switch statement, which is what one would normally want to happen. However, if the break statement is omitted, program execution continues with the first line of the next case, if any. This is called a fall-through. + +--- + +### Applying Control Flow Constructs to Problem Solving + +The true power of control flow constructs is realized when they are applied to solve practical programming problems. In this experiment, you will use these constructs to address two classic tasks: + +#### 1. Determining the Position of a Point Relative to a Rectangle + +By using if, if-else, and if-else-if constructs, you can check whether a given point (X, Y) lies inside or outside a rectangle defined by its corner coordinates. The program evaluates a series of conditions comparing the point's coordinates to the rectangle's boundaries and provides a decision based on the results. This demonstrates how conditional logic can be used to solve geometric and spatial problems in programming. + +#### 2. Selecting the Day of the Week Using Switch-Case + +The switch-case construct is ideal for mapping discrete values (such as numbers 1–7) to specific outcomes (such as days of the week). By associating each case with a day, the program can efficiently select and display the correct day name based on user input. This approach is commonly used in menu-driven programs, state machines, and other applications where a variable can take one of several distinct values. + +### Summary + +By mastering these constructs, you will be able to: -2. Typically, the last statement for each case in a switch is `break`. Omitting `break` causes execution to continue into the next case, known as fall-through. +- Make decisions in your programs based on conditions +- Choose the appropriate control flow structure for a given problem +- Implement solutions to real-world tasks using basic control flow in computer programming