PageSourceSearch

https://noir-lang.org/docs/assets/js/3dcafce4.8efaf9cb.js

js noir-lang.org collected 2026-10-03 22:53:01 UTC 7,972 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[2400],{91597(e,n,o){o.r(n),o.d(n,{assets:()=>a,contentTitle:()=>l,default:()=>h,frontMatter:()=>t,metadata:()=>s,toc:()=>c});const s=JSON.parse('{"id":"language/control_flow","title":"Control Flow","description":"Learn how to use loops and if expressions in the Noir programming language. Discover the syntax and examples for for loops and if-else statements.","source":"@site/versioned_docs/version-v1.0.0-rc.0/language/control_flow.md","sourceDirName":"language","slug":"/language/control_flow","permalink":"/docs/v1.0.0-rc.0/language/control_flow","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.0/language/control_flow.md","tags":[],"version":"v1.0.0-rc.0","frontMatter":{"title":"Control Flow","description":"Learn how to use loops and if expressions in the Noir programming language. Discover the syntax and examples for for loops and if-else statements.","keywords":["Noir programming language","loops","for loop","if-else statements","Rust syntax"]},"sidebar":"sidebar","previous":{"title":"Functions","permalink":"/docs/v1.0.0-rc.0/language/functions"},"next":{"title":"Logical Operations","permalink":"/docs/v1.0.0-rc.0/language/ops"}}');var r=o(74848),i=o(28453);const t={title:"Control Flow",description:"Learn how to use loops and if expressions in the Noir programming language. Discover the syntax and examples for for loops and if-else statements.",keywords:["Noir programming language","loops","for loop","if-else statements","Rust syntax"]},l=void 0,a={},c=[{value:"If Expressions",id:"if-expressions",level:2},{value:"For loops",id:"for-loops",level:2},{value:"Iterating over arrays and vectors",id:"iterating-over-arrays-and-vectors",level:3},{value:"Break and Continue",id:"break-and-continue",level:3},{value:"Loops",id:"loops",level:2},{value:"While loops",id:"while-loops",level:2}];function d(e){const n={code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",...(0,i.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.h2,{id:"if-expressions",children:"If Expressions"}),"\n",(0,r.jsxs)(n.p,{children:["Noir supports ",(0,r.jsx)(n.code,{children:"if-else"})," statements. The syntax is most similar to Rust's where it is not required\nfor the statement's conditional to be surrounded by parentheses."]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"let a = 0;\nlet mut x: u32 = 0;\n\nif a == 0 {\n    if a != 0 {\n        x = 6;\n    } else {\n        x = 2;\n    }\n} else {\n    x = 5;\n    assert(x == 5);\n}\nassert(x == 2);\n"})}),"\n",(0,r.jsx)(n.h2,{id:"for-loops",children:"For loops"}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"for"})," loops allow you to repeat a block of code multiple times."]}),"\n",(0,r.jsx)(n.p,{children:"The following block of code between the braces is run 10 times."}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"for i in 0..10 {\n    // do something\n}\n"})}),"\n",(0,r.jsxs)(n.p,{children:["Alternatively, ",(0,r.jsx)(n.code,{children:"start..=end"})," can be used for a range that is inclusive on both ends:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"// Exclusive range: i takes values 0, 1, 2, ..., 9\nfor i in 0..10 {\n    println(i);\n}\n\n// Inclusive range: i takes values 0, 1, 2, ..., 10\nfor i in 0..=10 {\n    println(i);\n}\n"})}),"\n",(0,r.jsxs)(n.p,{children:["The index for loops is of type ",(0,r.jsx)(n.code,{children:"u64"}),"."]}),"\n",(0,r.jsx)(n.h3,{id:"iterating-over-arrays-and-vectors",children:"Iterating over arrays and vectors"}),"\n",(0,r.jsxs)(n.p,{children:["You can iterate directly over the elements of an array or vector using ",(0,r.jsx)(n.code,{children:"for element in collection"}),":"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"let arr = [10, 20, 30];\nfor element in arr {\n    println(element);\n}\n"})}),"\n",(0,r.jsx)(n.p,{children:"This also works with vectors:"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"let vec = @[1, 2, 3];\nfor element in vec {\n    println(element);\n}\n"})}),"\n",(0,r.jsx)(n.h3,{id:"break-and-continue",children:"Break and Continue"}),"\n",(0,r.jsxs)(n.p,{children:["In unconstrained code, ",(0,r.jsx)(n.code,{children:"break"})," and ",(0,r.jsx)(n.code,{children:"continue"})," are also allowed in ",(0,r.jsx)(n.code,{children:"for"})," and ",(0,r.jsx)(n.code,{children:"loop"})," loops. These are only allowed\nin unconstrained code since normal constrained code requires that Noir knows exactly how many iterations\na loop may have. ",(0,r.jsx)(n.code,{children:"break"})," and ",(0,r.jsx)(n.code,{children:"continue"})," can be used like so:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:'for i in 0 .. 10 {\n    println("Iteration start")\n\n    if i == 2 {\n        continue;\n    }\n\n    if i == 5 {\n        break;\n    }\n\n    println(i);\n}\nprintln("Loop end")\n'})}),"\n",(0,r.jsxs)(n.p,{children:["When used, ",(0,r.jsx)(n.code,{children:"break"})," will end the current loop early and jump to the statement after the for loop. In the example\nabove, the ",(0,r.jsx)(n.code,{children:"break"})," will stop the loop and jump to the ",(0,r.jsx)(n.code,{children:'println("Loop end")'}),"."]}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"continue"})," will stop the current iteration of the loop, and jump to the start of the next iteration. In the example\nabove, ",(0,r.jsx)(n.code,{children:"continue"})," will jump to ",(0,r.jsx)(n.code,{children:'println("Iteration start")'})," when used. Note that the loop continues as normal after this.\nThe iteration variable ",(0,r.jsx)(n.code,{children:"i"})," is still increased by one as normal when ",(0,r.jsx)(n.code,{children:"continue"})," is used."]}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"break"})," and ",(0,r.jsx)(n.code,{children:"continue"})," cannot currently be used to jump out of more than a single loop at a time."]}),"\n",(0,r.jsx)(n.h2,{id:"loops",children:"Loops"}),"\n",(0,r.jsxs)(n.p,{children:["In unconstrained code, ",(0,r.jsx)(n.code,{children:"loop"})," is allowed for loops that end with a ",(0,r.jsx)(n.code,{children:"break"}),".\nA ",(0,r.jsx)(n.code,{children:"loop"})," must contain at least one ",(0,r.jsx)(n.code,{children:"break"})," statement that is reachable during execution.\nThis is only allowed in unconstrained code since normal constrained code requires that Noir knows exactly how many iterations\na loop may have."]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"let mut i = 10;\nloop {\n    println(i);\n    i -= 1;\n\n    if i == 0 {\n        break;\n    }\n}\n"})}),"\n",(0,r.jsx)(n.h2,{id:"while-loops",children:"While loops"}),"\n",(0,r.jsxs)(n.p,{children:["In unconstrained code, ",(0,r.jsx)(n.code,{children:"while"})," is allowed for loops that end when a given condition is met.\nThis is only allowed in unconstrained code since normal constrained code requires that Noir knows exactly how many iterations\na loop may have."]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"let mut i = 0\nwhile i < 10 {\n    println(i);\n    i += 2;\n}\n"})})]})}function h(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(d,{...e})}):d(e)}},28453(e,n,o){o.d(n,{R:()=>t,x:()=>l});var s=o(96540);const r={},i=s.createContext(r);function t(e){const n=s.useContext(i);return s.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:t(e.components),s.createElement(i.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.