switch-case Archives - SoftUni Global https://softuni.org/tag/switch-case/ Learn Programming and Start a Developer Job Fri, 13 May 2022 11:36:49 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.3 https://softuni.org/wp-content/uploads/2022/04/cropped-SoftUni-Global-Logo-Square-notext-32x32.png switch-case Archives - SoftUni Global https://softuni.org/tag/switch-case/ 32 32 Java Basics Tutorial – Part 6 – Advanced Conditional Statements https://softuni.org/code-lessons/java-basics-tutorial-part-6-advanced-conditional-statements/ https://softuni.org/code-lessons/java-basics-tutorial-part-6-advanced-conditional-statements/#respond Fri, 22 Oct 2021 11:06:00 +0000 https://softuni.org/?p=9984 In this part of the Java Basics Tutorial, we look at Advanced Conditional Statements.

The post Java Basics Tutorial – Part 6 – Advanced Conditional Statements appeared first on SoftUni Global.

]]>

There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use nested conditional statements. In them, you can have a statement inside another statement and so on.

A Switch Statement is usually more efficient than a set of nested ifs. When we have to choose which one to use, it’s based on readability and the expression that the statement is testing. We use a switch statement to test the value of a variable against a list of case values, while we use an if-else statement for taking a decision.

switch-case-computer

If-else statement evaluates integer, character, pointer, floating-point type, or boolean type. On the other hand, the switch statement evaluates only character or an integer datatype. It’s known to be hard to edit if-else statements since it’s tedious to trace where the correction is required. Many people agree that it’s much simpler to correct switch statements since they’re easy to trace.

There can be any number of case statements within a switch statement. Each case is followed by the value to be compared to and after that a colon. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. If that happens, the switch terminates, and the flow of control jumps to the next line following the switch statement. Not every case needs to contain a break. If no break appears, the flow of control will fall through until a break is reached. all the case statements will get executed as soon as the compiler finds a comparison to be true.

Guy-It-Problem

A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used to perform a task when none of the cases are true. No break is needed in the default case. The  switch statement works much faster than an equivalent if-else statement. During execution, instead of checking which case is matching, it only decides which case has to execute. It’s more readable compared to if-else statements.

Lesson Topics

In this video we review the following topics:
  • Complex Control-Flow
  • Nested Conditions
  • Logical Operators
  • Switch-Case
  • Multi-Label Switch-Case

Practical Exercises

Watch the video and solve the problemsTo better understand the material, do the coding exercises and implement the knowledge you acquired. Writing code is the only way to master the skill of code.

Submit your code in the SoftUni Judge System:

Exercises: Problem Descriptions

Lesson Slides

The post Java Basics Tutorial – Part 6 – Advanced Conditional Statements appeared first on SoftUni Global.

]]>
https://softuni.org/code-lessons/java-basics-tutorial-part-6-advanced-conditional-statements/feed/ 0