conditional statements Archives - SoftUni Global https://softuni.org/tag/conditional-statements/ Learn Programming and Start a Developer Job Fri, 13 May 2022 11:37:27 +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 conditional statements Archives - SoftUni Global https://softuni.org/tag/conditional-statements/ 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
Java Basics Tutorial – Part 5 – Conditional Statements https://softuni.org/code-lessons/java-basics-tutorial-part-5-conditional-statements/ https://softuni.org/code-lessons/java-basics-tutorial-part-5-conditional-statements/#respond Thu, 21 Oct 2021 11:06:00 +0000 https://softuni.org/?p=9921 In this lesson of the Java Bascics Tutorial, we take a look at Conditional Statements.

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

]]>

 

conditional-statement-picA conditional statement is a piece of code that checks the given condition, and when it is true, the code related to it is performed. It is used to make decisions based on the conditions. Conditional statements execute when there is no condition around the statements. If you put a condition for a block of statements, the execution flow may change based on the result evaluated by the condition. This process is called decision-making.

Conditional statements begin with the keyword if followed by parentheses. An expression is placed inside the parentheses, then evaluated when the conditional statement is reached. 

The parentheses are followed by a block, which is defined inside opening { and closing } curly brackets. The source code inside the block executes if the expression inside the parentheses evaluates to true. If the expression in the conditional statement evaluates to true, the execution of the program progresses to the block defined by the conditional statement.

On the other hand, if the expression evaluates to false, the execution moves on to the statement after the closing curly bracket of the current conditional statement. Using the else command, we create an alternative option for when the conditional expression evaluates to false. In the case of multiple conditions, we can use the else if  command. else if is like else, but with an additional condition. It follows the if-condition, and they may be multiple.switch-case-computer

To sum it up, conditional statements are something that you must learn if you want to continue developing as a programmer. They s are specified by a set of statements having boolean expressions which, are evaluated to a boolean value true or false. Conditional statements help us to make a decision based on certain conditions.

Lesson Topics

In this video we review the following topics:
  • Logical Expressions
  • Conditional Statements
  • Series of Checks
  • Variable Scope
  • Debugging

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 5 – Conditional Statements appeared first on SoftUni Global.

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