TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Octave

    Octave If Statement

    Overview

    In this tutorial, we will learn about Octave if statement. The if statement is a decision-making statement.

    If statement

    In the simplest form, the if statement syntax looks like this:

    if (if-condition)
        if-body
    endif

    or

    if (if-condition)
        if-body
    end

    The if-condition is an expression that controls the flow of the if block. The if-body is executed only if the condition is true. The condition expression in an if statement is true if its value is non-zero, and false if its value is zero.

    If – else statement

    if (condition)
         if-body
    else
         else-body
    endif

    or

    if (condition)
         then-body
    else
         else-body
    end

     

    We can also have the else body for the if statement.
    If condition expression evaluates to true, if-body is executed; otherwise, the else-body is executed.

    Sample Script

    % ======================================
    % Sample Octave Script
    % If statement demo
    % Octave Tutorials – www.TestingDocs.com
    % ======================================

    fprintf(‘Welcome.\n’);
    % Prompt user
    a = input(‘Enter value for a :’);
    b = input(‘Enter value for b :’);

    % if statement to check if a > b
    if(a > b)
    fprintf(‘a is greater than b. \n’);
    else
    fprintf(‘a is less than or equal to b. \n’);
    end
    fprintf(‘Good Bye!\n’);

     

    Octave If Else Statement Demo

    Output

    >> IfElseDemo

    Welcome.
    Enter value for a :9
    Enter value for b :16
    a is less than or equal to b.
    Good Bye!
    >>

    if-elseif-else ladder

    We can also have multiple decisions combined in a single if statement

    if (condition)
         if-body
    elseif (condition)
        elseif-body
    else
         else-body
    endif

     

    Octave If Statement

    Example

    % Sample Octave Script 
    % If Statement Demo
    % Octave Tutorials - www.TestingDocs.com
    marks = input('Enter marks:');
    
    % if-else ladder statement
    if( marks >= 90 && marks <= 100) 
     fprintf('Grade: A+ - Excellent :-) \n'); 
    elseif( marks >= 80 && marks < 90) 
     fprintf('Grade: A - Good \n'); 
    elseif( marks >= 70 && marks < 80) 
     fprintf('Grade: B+ - Above Average \n'); 
    elseif( marks >= 60 && marks < 70) 
     fprintf('Grade: B Average \n'); 
    elseif( marks >= 50 && marks < 60) 
     fprintf('Grade: C+ Need Improvement \n');
    elseif( marks >= 40 && marks < 50)
     fprintf('Grade: C Poor \n');
    else
     fprintf('Fail: No Grade :-( \n');
    end

    —

    Octave Tutorials

    Octave Tutorial on this website can be found at:
    https://www.testingdocs.com/octave-tutorial/

    More information on Octave can be found on the official website:
    https://www.gnu.org/software/octave/index

    Related Posts

    Octave Package List

    Octave /

    Install Octave Packages on Linux

    Octave /

    Octave User defined Function Example

    octave command line linux

    Octave /

    Octave Command line on Ubuntu

    Hello World in Octave

    Octave /

    Hello World in Octave

    Octave /

    Octave plot example of function y=x^2

    ‹ Declare Variables in Octave› Octave while Statement

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com