Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 4.91 KB

06-Advanced-Conditional-Statements.md

File metadata and controls

53 lines (42 loc) · 4.91 KB

6. Advanced Conditional Statements

1. Lesson Summary

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.

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.

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.

2. Table of Contents

3. Lesson Video

YouTube Thumbnail

4. Lesson Topics

In this lesson we cover the following topics:

  • Complex Control-Flow
  • Nested Conditions
  • Logical Operators
  • Switch-Case
  • Multi-Label Switch-Case
  • Coding Exercises

5. Resources

Remember that coding is a skill, which should be practiced. To learn to code, you should write code every day for a long time. Watching tutorials is not enough. You should code!

Resources Link
Lesson Video YouTube
Lesson Content SoftUni

6. Practical Exercises

You will get access to automated exercises which will sharpen your coding skills. Become a member of the SoftUni Global Community and communicate with other students and mentors and get help for FREE. Please watch the video and solve the exercise problems. Writing code is the only way to master the skill of coding. Submit your code at the SoftUni Judge.

Resources Link
Problem Descriptions Access the Learning Materials
Submit Solutions for Evaluation Access the Learning Materials

7. Navigation

« Previous Lesson   |   Home   |   Next Lesson »