Review Conditional Statements.pdf

Full Transcript

Review: Conditional Statements In C#, there are several types of conditional statements that allow you to control the flow of your program based on certain conditions. Below are the main types of conditional statements: 1. if Statement. The if statement executes a block of code if a specified c...

Review: Conditional Statements In C#, there are several types of conditional statements that allow you to control the flow of your program based on certain conditions. Below are the main types of conditional statements: 1. if Statement. The if statement executes a block of code if a specified condition is true. if (condition) { // Code to execute if condition is true } int number = 10; if (number > 5) { Console.WriteLine("The number is greater than 5."); } Practice 01.1: Write an if statement to check if a number is even. If it is even, display the message "The number [number] is even." 2. if-else Statement. The if-else statement provides an alternative block of code that executes if the if condition is false. if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false } int number = 3; if (number > 5) { Console.WriteLine("The number is greater than 5."); } else { Console.WriteLine("The number is not greater than 5."); } Practice 01.2: Write an if-else statement to determine if a number is positive or negative. If the number is positive, display the message "The number [number] is positive." Otherwise, display the message "The number [number] is negative." 3. else if Ladder. The else if ladder allows you to test multiple conditions sequentially. If the first condition is false, the next one is tested, and so on. if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition2 is true } else { // Code to execute if none of the conditions are true } int number = 10; if (number < 5) { Console.WriteLine("The number is less than 5."); } else if (number >= 5 && number 5) ? "Greater than 5" : "5 or less"; Console.WriteLine(result); Practice 01.4: Write a C# program using a ternary operator to determine if a student has passed or failed an exam. The passing score is 50. Print "Passed" if the score is 50 or above, otherwise print "Failed". 5. switch Statement. The switch statement is used when you need to compare the value of a variable against a list of values. It’s more efficient and cleaner than using multiple if-else statements when dealing with many conditions. switch (variable) { case value1: // Code to execute if variable equals value1 break; case value2: // Code to execute if variable equals value2 break; // More cases as needed default: // Code to execute if none of the cases match break; } int day = 3; switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; default: Console.WriteLine("Other day"); break; } Practice 01.5: Create a program that allows the user to choose between computing the area or the perimeter of a circle. The user will be prompted to enter the radius of the circle. Based on the user's letter choice [‘A’,’P’, or ’X’], the program will compute and display the result. The options are: Compute Area (A): Calculate the area of the circle using the formula Area= 𝜋 × radius2 Compute Perimeter (P): Calculate the perimeter (circumference) of the circle using the formula Perimeter = 2 × 𝜋 × radius Exit (X): Exit the application.

Use Quizgecko on...
Browser
Browser