Operators, Loops & Conditionals

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 21:- Operators, Loops & Conditionals

 

Operators, loops, and conditionals are fundamental concepts in programming that allow you to control the flow of a program, perform calculations, and make decisions based on specific conditions. Let's explore each of these concepts:

Operators:

  • Arithmetic Operators: Perform basic arithmetic operations like addition (+), subtraction (-), multiplication (*), division (/), and modulo (%).
  • Relational Operators: Compare two values and return a Boolean result (true or false) based on the comparison. Examples: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).
  • Logical Operators: Combine Boolean expressions and return true or false. Examples: && (logical AND), || (logical OR), ! (logical NOT).
  • Assignment Operators: Assign values to variables. Examples: = (simple assignment), += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and assign), %= (modulo and assign).

Loops:

  • for Loop: Executes a block of code a specified number of times.
  • while Loop: Repeats a block of code as long as a specified condition is true.
  • do-while Loop: Similar to the while loop but guarantees that the code block executes at least once before checking the condition.
  • break Statement: Exits a loop prematurely.
  • continue Statement: Skips the rest of the loop body and proceeds to the next iteration.

Conditionals:

  • if Statement: Executes a block of code if a specified condition is true.
  • else Statement: Executes a block of code if the condition of the if statement is false.
  • else if Statement: Allows for multiple conditions to be tested in sequence.
  • Ternary Operator (conditional operator): A shorthand way of writing an if-else statement in a single line.
  • switch Statement: Evaluates an expression and executes code blocks based on the expression's value.

Example: C++ code demonstrating the use of operators, loops, and conditionals:

cppCopy code

#include <iostream> int main() {    // Operators    int num1 = 10, num2 = 5;    int sum = num1 + num2; // Addition    int difference = num1 - num2; // Subtraction    int product = num1 * num2; // Multiplication    int quotient = num1 / num2; // Division    int remainder = num1 % num2; // Modulo    // Loops    for (int i = 1; i <= 5; i++) {        std::cout << "Iteration " << i << std::endl;    }    int i = 1;    while (i <= 5) {        std::cout << "Iteration " << i << std::endl;        i++;    }    i = 1;    do {        std::cout << "Iteration " << i << std::endl;        i++;    } while (i <= 5);    // Conditionals    int age = 20;    if (age >= 18) {        std::cout << "You are an adult." << std::endl;    } else {        std::cout << "You are a minor." << std::endl;    }    int number = 10;    std::cout << (number % 2 == 0 ? "Even" : "Odd") << std::endl;    int choice = 2;    switch (choice) {        case 1:            std::cout << "Option 1 selected." << std::endl;            break;        case 2:            std::cout << "Option 2 selected." << std::endl;            break;        default:            std::cout << "Invalid option." << std::endl;    }    return 0; }

Remember, operators, loops, and conditionals are essential building blocks in programming, and mastering them is crucial for writing efficient and robust code.

4. Basics Of Programming- II

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?